Community Forums › Forums › Archived Forums › General Discussion › Including the Parent/Child Custom Post Relationship in Breadcrumbs – Genesis
Tagged: breadcrumb, CPT
- This topic has 5 replies, 4 voices, and was last updated 4 years, 11 months ago by perlastudio.
-
AuthorPosts
-
October 12, 2019 at 7:25 am #493970perlastudioParticipant
Hello,
I created a custom post type for products. The breadcrumbs work great on these posts except for the child posts don't show the parent product.
Current: Home > Products > Child Product
Desired: Home > Products > Parent Product > Child ProductMy settings:
add_filter( 'genesis_breadcrumb_args', 'sp_breadcrumb_args' ); function sp_breadcrumb_args( $args ) { $args['home'] = 'Home'; $args['sep'] = ' / '; $args['list_sep'] = ', '; // Genesis 1.5 and later $args['prefix'] = '<div class="breadcrumb">'; $args['suffix'] = '</div>'; $args['heirarchial_attachments'] = true; // Genesis 1.5 and later $args['heirarchial_categories'] = true; // Genesis 1.5 and later $args['display'] = true; $args['labels']['prefix'] = 'You are here: '; $args['labels']['author'] = 'Archives for '; $args['labels']['category'] = 'Archives for '; // Genesis 1.6 and later $args['labels']['tag'] = 'Archives for '; $args['labels']['date'] = 'Archives for '; $args['labels']['search'] = 'Search for '; $args['labels']['tax'] = 'Archives for '; $args['labels']['post_type'] = 'Archives for '; $args['labels']['404'] = 'Not found: '; // Genesis 1.5 and later return $args; }
/* CPT PRODUCTS */ add_action( 'init', 'products_init' ); function products_init() { $labels = array( 'name' => 'Products', 'singular_name' => 'Products', 'singular_label' => 'Products', 'add_new' => 'Add Product', 'add_new_item' => 'Add Product', 'edit' => 'Edit', 'edit_item' => 'Edit Product', 'new_item' => 'New Product', 'view' => 'View Product', 'view_item' => 'View Product', 'search_items' => 'Search Product', 'not_found' => 'Nie znaleziono pasuj?…cych wpisów. Spróbuj ponownie.', 'not_found_in_trash' => 'Nie znaleziono pasuj?…cych wpisów. Spróbuj ponownie.', ); $args = array( 'labels' => $labels, 'public' => true, 'show_ui' => true, 'capability_type' => 'page', 'has_archive' => true, 'hierarchical' => true, 'rewrite' => array( 'slug' => 'products' ), 'supports' => array( 'title', 'editor', 'custom-fields', 'thumbnail', 'page-attributes', 'genesis-seo', 'genesis-layouts', 'genesis-simple-sidebars', 'genesis-cpt-archives-settings' ) ); register_post_type( 'products', $args ); }
Please, help me 🙁
Best regards,
Tomasz PerlińskiOctober 12, 2019 at 6:51 pm #493973AnitaCKeymasterOctober 13, 2019 at 5:02 am #493976perlastudioParticipantAnita,
Thanks for article... It was helpful...
I wrote code:
/********************************** * BREADCRUMB * **********************************/ function cpt_rolety_zewnetrzne_breadcrumb( $crumb, $args ) { // Only modify the breadcrumb if in the 'rolety_zewnetrzne' post type if( 'rolety_zewnetrzne' !== get_post_type() ) return $crumb; $parentID = wp_get_post_parent_id($post->ID); if($parentID == 0) return $crumb; $parent = get_post($parentID); if(!empty($parent)) { // Build the breadcrumb $crumb = '<a href="' . get_post_type_archive_link( 'rolety_zewnetrzne' ) . '">Rolety zewnętrzne</a>' . $args['sep'] . '<a href="'.get_the_permalink($parent->ID).'">'.$parent->post_title.'</a>' . $args['sep'] . get_the_title(); } return $crumb; } add_filter( 'genesis_single_crumb', 'cpt_rolety_zewnetrzne_breadcrumb', 10, 2 );
Function working, but links in breadcrumb showing without tag "span" and "class".
Current:
<a href="http://xxx/xxx/xxx/">Link Breadcrumb</a>
Desired:
<span class="breadcrumb-link-wrap"> <a class="breadcrumb-link" href="http://xxx/xxx/xxx/"> <span class="breadcrumb-link-text-wrap">Link Breadcrumb</span> </a> </span>
how to fix it?
Thanks for help.
Best regards,
Tomasz PerlińskiOctober 13, 2019 at 5:19 am #493977October 13, 2019 at 9:57 am #493981JiveDigParticipantYou’re building the link HTML already in your $crumb variable, so you can add the extra spans/markup there.
Lead developer of Mai Theme
October 15, 2019 at 2:45 am #494005perlastudioParticipantJiveDig,
thanks for help!
This code working:
/********************************** * BREADCRUMB FOR CPT* **********************************/ function cpt_rolety_zewnetrzne_breadcrumb( $crumb, $args ) { // Set register CPT $registerCustomPostType = 'rolety_zewnetrzne'; // Only modify the breadcrumb if in the 'rolety_zewnetrzne' post type if( $registerCustomPostType !== get_post_type() ) return $crumb; $postType = get_post_type_object(get_post_type()); $parentID = wp_get_post_parent_id($post->ID); if($parentID == 0) return $crumb; $parent = get_post($parentID); $link1 = '<span class="breadcrumb-link-wrap"><a class="breadcrumb-link" href="' . get_post_type_archive_link( $registerCustomPostType ) . '"><span class="breadcrumb-link-text-wrap">' . $postType->labels->singular_name . '</span></a></span>'; $link2 = '<span class="breadcrumb-link-wrap"><a class="breadcrumb-link" href="'.get_the_permalink($parent->ID) . '"><span class="breadcrumb-link-text-wrap">'.$parent->post_title.'</span></a></span>'; if(!empty($parent)) { // Build the breadcrumb $crumb = $link1 . $args['sep'] . $link2 . $args['sep'] . get_the_title(); } return $crumb; } add_filter( 'genesis_single_crumb', 'cpt_rolety_zewnetrzne_breadcrumb', 10, 2 );
-
AuthorPosts
- The topic ‘Including the Parent/Child Custom Post Relationship in Breadcrumbs – Genesis’ is closed to new replies.