generated from pascalmartineau/wp-skeleton
feat: Virtual page redirect, breadcrumb & menu items
All checks were successful
Deploy WordPress and Nuxt / deploy (push) Successful in 1m5s
All checks were successful
Deploy WordPress and Nuxt / deploy (push) Successful in 1m5s
This commit is contained in:
@@ -49,102 +49,77 @@ function ccat_register_breadcrumb_graphql_fields() {
|
||||
|
||||
// Helper: Get breadcrumbs for a given node
|
||||
function ccat_get_breadcrumbs( $node ) {
|
||||
if ( $node instanceof \WPGraphQL\Model\Post ) {
|
||||
return ccat_get_post_breadcrumbs( $node->databaseId );
|
||||
}
|
||||
if ( $node instanceof \WPGraphQL\Model\Term ) {
|
||||
return ccat_get_term_breadcrumbs( $node->databaseId );
|
||||
}
|
||||
return array();
|
||||
}
|
||||
|
||||
// Helper: Get breadcrumbs for a given post ID
|
||||
function ccat_get_post_breadcrumbs( $post_id ) {
|
||||
$breadcrumbs = array();
|
||||
if ( ! $node ) {
|
||||
|
||||
$is_front_page = get_option( 'show_on_front' ) === 'page' && (int) get_option( 'page_on_front' ) === $post_id;
|
||||
if ( $is_front_page ) {
|
||||
return $breadcrumbs;
|
||||
}
|
||||
if ( $node instanceof \WPGraphQL\Model\Post ) {
|
||||
$post = get_post( $node->databaseId );
|
||||
if ( $post ) {
|
||||
$breadcrumbs = ccat_get_post_breadcrumbs( $post );
|
||||
$breadcrumbs[] = array(
|
||||
'label' => get_the_title( $post->ID ),
|
||||
'to' => null,
|
||||
);
|
||||
}
|
||||
} elseif ( $node instanceof \WPGraphQL\Model\Term ) {
|
||||
$term = get_term( $node->databaseId );
|
||||
if ( $term && ! is_wp_error( $term ) ) {
|
||||
$breadcrumbs = ccat_get_term_breadcrumbs( $term );
|
||||
$breadcrumbs[] = array(
|
||||
'label' => $term->name,
|
||||
'to' => null,
|
||||
);
|
||||
}
|
||||
}
|
||||
return $breadcrumbs;
|
||||
}
|
||||
|
||||
// Helper: Get breadcrumbs for a given post
|
||||
function ccat_get_post_breadcrumbs( WP_Post $post ) {
|
||||
$breadcrumbs = array();
|
||||
$is_front_page = get_option( 'show_on_front' ) === 'page' && (int) get_option( 'page_on_front' ) === $post->ID;
|
||||
if ( $is_front_page ) {
|
||||
return $breadcrumbs; // No breadcrumbs for the front page
|
||||
} else {
|
||||
$breadcrumbs[] = array(
|
||||
'label' => 'Accueil',
|
||||
'to' => '/',
|
||||
);
|
||||
|
||||
$post_type = get_post_type( $post_id );
|
||||
$ancestor_ids = get_post_ancestors( $post_type === 'page' ? $post_id : get_option( "page_for_$post_type" ) );
|
||||
$ancestor_ids = array_reverse( $ancestor_ids );
|
||||
foreach ( $ancestor_ids as $ancestor_id ) {
|
||||
$breadcrumbs[] = array(
|
||||
'label' => 'Accueil',
|
||||
'to' => '/',
|
||||
'label' => get_the_title( $ancestor_id ),
|
||||
'to' => ccat_is_virtual_page( $ancestor_id ) ? null : str_replace( home_url(), '', get_permalink( $ancestor_id ) ),
|
||||
);
|
||||
}
|
||||
$post_type = get_post_type( $post->ID );
|
||||
switch ( $post_type ) {
|
||||
case 'page':
|
||||
$ancestors = get_post_ancestors( $post->ID );
|
||||
if ( ! empty( $ancestors ) ) {
|
||||
$ancestors = array_reverse( $ancestors );
|
||||
foreach ( $ancestors as $ancestor_id ) {
|
||||
$breadcrumbs[] = array(
|
||||
'label' => get_the_title( $ancestor_id ),
|
||||
'to' => str_replace( home_url(), '', get_permalink( $ancestor_id ) ),
|
||||
);
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
if ( ! empty( $post_id = get_option( "page_for_$post_type" ) ) ) {
|
||||
// Get ancestors of the archive page
|
||||
$ancestors = get_post_ancestors( $post_id );
|
||||
if ( ! empty( $ancestors ) ) {
|
||||
$ancestors = array_reverse( $ancestors );
|
||||
foreach ( $ancestors as $ancestor_id ) {
|
||||
$breadcrumbs[] = array(
|
||||
'label' => get_the_title( $ancestor_id ),
|
||||
'to' => str_replace( home_url(), '', get_permalink( $ancestor_id ) ),
|
||||
);
|
||||
}
|
||||
}
|
||||
// Add the archive page itself
|
||||
$breadcrumbs[] = array(
|
||||
'label' => get_the_title( $post_id ),
|
||||
'to' => str_replace( home_url(), '', get_permalink( $post_id ) ),
|
||||
);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
$breadcrumbs[] = array(
|
||||
'label' => get_the_title( $post_id ),
|
||||
'to' => null,
|
||||
);
|
||||
return $breadcrumbs;
|
||||
}
|
||||
|
||||
// Helper: Get breadcrumbs for a given term
|
||||
function ccat_get_term_breadcrumbs( WP_Term $term ) {
|
||||
$breadcrumbs = array();
|
||||
$taxonomy = $term->taxonomy;
|
||||
if ( $term->parent ) {
|
||||
$ancestors = get_ancestors( $term->term_id, $taxonomy );
|
||||
if ( ! empty( $ancestors ) ) {
|
||||
$ancestors = array_reverse( $ancestors );
|
||||
foreach ( $ancestors as $ancestor_id ) {
|
||||
$ancestor_term = get_term( $ancestor_id, $taxonomy );
|
||||
if ( $ancestor_term && ! is_wp_error( $ancestor_term ) ) {
|
||||
$breadcrumbs[] = array(
|
||||
'label' => $ancestor_term->name,
|
||||
'to' => str_replace( home_url(), '', get_term_link( $ancestor_term ) ),
|
||||
);
|
||||
}
|
||||
}
|
||||
// Helper: Get breadcrumbs for a given term ID
|
||||
function ccat_get_term_breadcrumbs( $term_id ) {
|
||||
$breadcrumbs = array(
|
||||
array(
|
||||
'label' => 'Accueil',
|
||||
'to' => '/',
|
||||
),
|
||||
);
|
||||
|
||||
$term = get_term( $term_id );
|
||||
if ( is_wp_error( $term ) || ! $term ) {
|
||||
return $breadcrumbs;
|
||||
}
|
||||
|
||||
$ancestor_ids = get_ancestors( $term->term_id, $term->taxonomy );
|
||||
$ancestor_ids = array_reverse( $ancestor_ids );
|
||||
foreach ( $ancestor_ids as $ancestor_id ) {
|
||||
$ancestor_term = get_term( $ancestor_id, $term->taxonomy );
|
||||
if ( is_wp_error( $ancestor_term ) || ! $ancestor_term ) {
|
||||
continue;
|
||||
}
|
||||
$breadcrumbs[] = array(
|
||||
'label' => $ancestor_term->name,
|
||||
'to' => str_replace( home_url(), '', get_term_link( $ancestor_term ) ),
|
||||
);
|
||||
}
|
||||
|
||||
$breadcrumbs[] = array(
|
||||
'label' => $term->name,
|
||||
'to' => null,
|
||||
);
|
||||
|
||||
return $breadcrumbs;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user