WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] help fix breadcrum function for custom post types (9 posts)

  1. Jonathan
    Member
    Posted 1 year ago #

    Hello,

    Several months ago I posted asking for help fixing my breadcrum function to work with custom post types. I didn't get the answers I was hoping for, but I believe I have found something that might help a more capable coder determine the answer for me.

    Here is my breadcrumb code:

    // Breadcrumbs
    function limelight_breadcrumbs()
    {
        $delimiter = ' » ';
        //text for the 'Home' link
        $name = __("Home", "limelight");
        $currentBefore = ' <span class="current">';
        $currentAfter = '</span> ';
        if (!is_home() && !is_front_page() && is_post_type('post') || is_paged()) {
            echo '<nav id="breadcrumbs">';
            global $post;
            $home = get_bloginfo('url');
            echo '<a href="' . $home . '">' . $name . '</a> ' . $delimiter . '';
            if (is_category()) {
                global $wp_query;
                $cat_obj = $wp_query->get_queried_object();
                $thisCat = $cat_obj->term_id;
                $thisCat = get_category($thisCat);
                $parentCat = get_category($thisCat->parent);
                if ($thisCat->parent != 0) {
                    echo(get_category_parents($parentCat, true, '' . $delimiter . ''));
                }
                echo $currentBefore . single_cat_title() . $currentAfter;
            } else if (is_day()) {
                echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> ' . $delimiter . '';
                echo '<a href="' . get_month_link(get_the_time('Y'), get_the_time('m')) . '">' . get_the_time('F') . '</a> ' . $delimiter . ' ';
                echo $currentBefore . get_the_time('d') . $currentAfter;
            } else if (is_month()) {
                echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> ' . $delimiter . '';
                echo $currentBefore . get_the_time('F') . $currentAfter;
            } else if (is_year()) {
                echo $currentBefore . get_the_time('Y') . $currentAfter;
            } else if (is_attachment()) {
                echo $currentBefore;
                the_title();
                $currentAfter;
            } if (is_single() && is_post_type('post')){
                $cat = get_the_category();
                $cat = $cat[0];
                echo get_category_parents($cat, true, ' ' . $delimiter . '');
                echo $currentBefore;
                the_title();
                echo $currentAfter;
            } else if (is_page() && !$post->post_parent) {
                echo $currentBefore;
                the_title();
                echo $currentAfter;
            } else if (is_page() && $post->post_parent) {
                $parent_id = $post->post_parent;
                $breadcrumbs = array();
                while ($parent_id) {
                    $page = get_page($parent_id);
                    $breadcrumbs[] = '<a href="' . get_permalink($page->ID) . '">' . get_the_title($page->ID) . '</a>';
                    $parent_id = $page->post_parent;
                }
                $breadcrumbs = array_reverse($breadcrumbs);
                foreach($breadcrumbs as $crumb)
                echo $crumb . ' ' . $delimiter . ' ';
                echo $currentBefore;
                the_title();
                echo $currentAfter;
            } else if (is_search()) {
                echo $currentBefore . __('Search Results For:','limelight') . ' ' . get_search_query() . $currentAfter;
            } else if (is_tag()) {
                echo $currentBefore . single_tag_title() . $currentAfter;
            } else if (is_author()) {
                global $author;
                $userdata = get_userdata($author);
                echo $currentBefore . $userdata->display_name . $currentAfter;
            } else if (is_404()) {
                echo $currentBefore . '404 Not Found' . $currentAfter;
            }
            if (get_query_var('paged')) {
                if (is_home() || is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author()) {
                    echo  $currentBefore;
                }
                echo __('Page','limelight') . ' ' . get_query_var('paged');
                if (is_home() || is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author()) {
                    echo $currentAfter;
                }
            }
            echo '</nav>';
        }
    }

    And here is the breadcrumb code from woocommerce, which works flawlessly with custom post types. My problem is that I need a fucntion that works site wide, not just on shop pages, and works on sites that are not using woocommerce. Can someone help me integrate the custom post supporting functionality of the woocommerce breadcrumbs into my breadcrumb function? My attempt so far have failed.

    <?php
    /**
     * Shop breadcrumb
     *
     * @author 		WooThemes
     * @package 	WooCommerce/Templates
     * @version     1.6.4
     */
    
    global $post, $wp_query;
    
    if( ! $home )
    	$home = _x( 'Home', 'breadcrumb', 'woocommerce' );
    
    $home_link = home_url();
    
    if ( get_option('woocommerce_prepend_shop_page_to_urls') == "yes" && woocommerce_get_page_id( 'shop' ) && get_option( 'page_on_front' ) !== woocommerce_get_page_id( 'shop' ) )
    	$prepend =  $before . '<a href="' . get_permalink( woocommerce_get_page_id('shop') ) . '">' . get_the_title( woocommerce_get_page_id('shop') ) . '</a> ' . $after . $delimiter;
    else
    	$prepend = '';
    
    if ( ( ! is_home() && ! is_front_page() && ! ( is_post_type_archive() && get_option( 'page_on_front' ) == woocommerce_get_page_id( 'shop' ) ) ) || is_paged() ) {
    
    	echo $wrap_before . $before  . '<a class="home" href="' . $home_link . '">' . $home . '</a> '  . $after . $delimiter ;
    
    	if ( is_category() ) {
    
    		$cat_obj = $wp_query->get_queried_object();
    		$this_category = get_category( $cat_obj->term_id );
    
    		if ( $this_category->parent != 0 ) {
    			$parent_category = get_category( $this_category->parent );
    			echo get_category_parents($parent_category, TRUE, $delimiter );
    		}
    
    		echo $before . single_cat_title( '', false ) . $after;
    
    	} elseif ( is_tax('product_cat') ) {
    
    		echo $prepend;
    		$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
    
    		$parents = array();
    		$parent = $term->parent;
    		while ( $parent ) {
    			$parents[] = $parent;
    			$new_parent = get_term_by( 'id', $parent, get_query_var( 'taxonomy' ) );
    			$parent = $new_parent->parent;
    		}
    
    		if ( ! empty( $parents ) ) {
    			$parents = array_reverse( $parents );
    			foreach ( $parents as $parent ) {
    				$item = get_term_by( 'id', $parent, get_query_var( 'taxonomy' ));
    				echo $before .  '<a href="' . get_term_link( $item->slug, 'product_cat' ) . '">' . $item->name . '</a>' . $after . $delimiter;
    			}
    		}
    
    		$queried_object = $wp_query->get_queried_object();
    		echo $before . $queried_object->name . $after;
    
    	} elseif ( is_tax('product_tag') ) {
    
    		$queried_object = $wp_query->get_queried_object();
    		echo $prepend . $before . __('Products tagged &ldquo;', 'woocommerce') . $queried_object->name . '&rdquo;' . $after;
    
    	} elseif ( is_day() ) {
    
    		echo $before . '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a>' . $after . $delimiter;
    		echo $before . '<a href="' . get_month_link(get_the_time('Y'),get_the_time('m')) . '">' . get_the_time('F') . '</a>' . $after . $delimiter;
    		echo $before . get_the_time('d') . $after;
    
    	} elseif ( is_month() ) {
    
    		echo $before . '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a>' . $after . $delimiter;
    		echo $before . get_the_time('F') . $after;
    
    	} elseif ( is_year() ) {
    
    		echo $before . get_the_time('Y') . $after;
    
    	} elseif ( is_post_type_archive('product') && get_option('page_on_front') !== woocommerce_get_page_id('shop') ) {
    
    		$_name = woocommerce_get_page_id( 'shop' ) ? get_the_title( woocommerce_get_page_id( 'shop' ) ) : ucwords( get_option( 'woocommerce_shop_slug' ) );
    
    		if ( is_search() ) {
    
    			echo $before . '<a href="' . get_post_type_archive_link('product') . '">' . $_name . '</a>' . $delimiter . __('Search results for &ldquo;', 'woocommerce') . get_search_query() . '&rdquo;' . $after;
    
    		} elseif ( is_paged() ) {
    
    			echo $before . '<a href="' . get_post_type_archive_link('product') . '">' . $_name . '</a>' . $after;
    
    		} else {
    
    			echo $before . $_name . $after;
    
    		}
    
    	} elseif ( is_single() && !is_attachment() ) {
    
    		if ( get_post_type() == 'product' ) {
    
    			echo $prepend;
    
    			if ( $terms = wp_get_object_terms( $post->ID, 'product_cat' ) ) {
    				$term = current( $terms );
    				$parents = array();
    				$parent = $term->parent;
    
    				while ( $parent ) {
    					$parents[] = $parent;
    					$new_parent = get_term_by( 'id', $parent, 'product_cat' );
    					$parent = $new_parent->parent;
    				}
    
    				if ( ! empty( $parents ) ) {
    					$parents = array_reverse($parents);
    					foreach ( $parents as $parent ) {
    						$item = get_term_by( 'id', $parent, 'product_cat');
    						echo $before . '<a href="' . get_term_link( $item->slug, 'product_cat' ) . '">' . $item->name . '</a>' . $after . $delimiter;
    					}
    				}
    
    				echo $before . '<a href="' . get_term_link( $term->slug, 'product_cat' ) . '">' . $term->name . '</a>' . $after . $delimiter;
    
    			}
    
    			echo $before . get_the_title() . $after;
    
    		} elseif ( get_post_type() != 'post' ) {
    
    			$post_type = get_post_type_object( get_post_type() );
    			$slug = $post_type->rewrite;
    				echo $before . '<a href="' . get_post_type_archive_link( get_post_type() ) . '">' . $post_type->labels->singular_name . '</a>' . $after . $delimiter;
    			echo $before . get_the_title() . $after;
    
    		} else {
    
    			$cat = current( get_the_category() );
    			echo get_category_parents( $cat, true, $delimiter );
    			echo $before . get_the_title() . $after;
    
    		}
    
    	} elseif ( is_404() ) {
    
    		echo $before . __( 'Error 404', 'woocommerce' ) . $after;
    
    	} elseif ( ! is_single() && ! is_page() && get_post_type() != 'post' ) {
    
    		$post_type = get_post_type_object( get_post_type() );
    
    		if ( $post_type )
    			echo $before . $post_type->labels->singular_name . $after;
    
    	} elseif ( is_attachment() ) {
    
    		$parent = get_post( $post->post_parent );
    		$cat = get_the_category( $parent->ID );
    		$cat = $cat[0];
    		echo get_category_parents( $cat, true, '' . $delimiter );
    		echo $before . '<a href="' . get_permalink( $parent ) . '">' . $parent->post_title . '</a>' . $after . $delimiter;
    		echo $before . get_the_title() . $after;
    
    	} elseif ( is_page() && !$post->post_parent ) {
    
    		echo $before . get_the_title() . $after;
    
    	} elseif ( is_page() && $post->post_parent ) {
    
    		$parent_id  = $post->post_parent;
    		$breadcrumbs = array();
    
    		while ( $parent_id ) {
    			$page = get_page( $parent_id );
    			$breadcrumbs[] = '<a href="' . get_permalink($page->ID) . '">' . get_the_title( $page->ID ) . '</a>';
    			$parent_id  = $page->post_parent;
    		}
    
    		$breadcrumbs = array_reverse( $breadcrumbs );
    
    		foreach ( $breadcrumbs as $crumb )
    			echo $crumb . '' . $delimiter;
    
    		echo $before . get_the_title() . $after;
    
    	} elseif ( is_search() ) {
    
    		echo $before . __( 'Search results for &ldquo;', 'woocommerce' ) . get_search_query() . '&rdquo;' . $after;
    
    	} elseif ( is_tag() ) {
    
    			echo $before . __( 'Posts tagged &ldquo;', 'woocommerce' ) . single_tag_title('', false) . '&rdquo;' . $after;
    
    	} elseif ( is_author() ) {
    
    		$userdata = get_userdata($author);
    		echo $before . __( 'Author:', 'woocommerce' ) . ' ' . $userdata->display_name . $after;
    
    	}
    
    	if ( get_query_var( 'paged' ) )
    		echo ' (' . __( 'Page', 'woocommerce' ) . ' ' . get_query_var( 'paged' ) . ')';
    
    	echo $wrap_after;
    
    }
  2. indybook
    Member
    Posted 1 year ago #

    Hello jonathantimar,

    your function works perfectly fine site wide for any post type as soon as you change is_post_type for get_post_type() == $mypost_type and make the code check if a post has any category assigned to it before echoing its parent category - line ... echo get_category_parents($cat, .....

    or I just got the question wrong. what exactly didn't work?

  3. Jonathan
    Member
    Posted 1 year ago #

    Hello indybook,

    You idea does stop the function from "breaking" however it does not produce proper breadcrumbs either.

    For anyone interested, I did manage to mangle up some code and produce a working function:

    // WooCommerce Compatable Breadcrumbs
    function limelight_breadcrumbs()
    {
    global $post, $wp_query;
    if ( ! $home ) $home = __( 'Home', 'limelight' );
    $home_link = home_url();
    $delimiter = ' &raquo; ';
    $currentBefore = '<span class="current">';
    $currentAfter = '</span>';
    $wrap_before = ' <nav id="breadcrumbs">';
    $wrap_after = '</nav> ';
    
    if ( get_option('woocommerce_prepend_shop_page_to_urls') == "yes" && woocommerce_get_page_id( 'shop' ) && 	get_option( 'page_on_front' ) !== woocommerce_get_page_id( 'shop' ) )
    $prepend = '<a href="' . get_permalink( woocommerce_get_page_id('shop') ) . '">' . get_the_title( woocommerce_get_page_id('shop') ) . '</a> ' . $delimiter;
    else $prepend = '';
    
    if ( ( ! is_home() && ! is_front_page() && ! ( is_post_type_archive() && get_option( 'page_on_front' ) == woocommerce_get_page_id( 'shop' ) ) ) || is_paged() ) {
    
    	echo $wrap_before . '<a class="home" href="' . $home_link . '">' . $home . '</a> ' . $delimiter ;
    
    	if ( is_category() ) {
    
    		$cat_obj = $wp_query->get_queried_object();
    		$this_category = get_category( $cat_obj->term_id );
    
    		if ( $this_category->parent != 0 ) {
    			$parent_category = get_category( $this_category->parent );
    			echo get_category_parents($parent_category, TRUE, $delimiter );
    		}
    
    		echo $currentBefore . single_cat_title( '', false ) . $currentAfter;
    
    	} elseif ( is_tax('product_cat') ) {
    
    		echo $prepend;
    		$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
    
    		$parents = array();
    		$parent = $term->parent;
    		while ( $parent ) {
    			$parents[] = $parent;
    			$new_parent = get_term_by( 'id', $parent, get_query_var( 'taxonomy' ) );
    			$parent = $new_parent->parent;
    		}
    
    		if ( ! empty( $parents ) ) {
    			$parents = array_reverse( $parents );
    			foreach ( $parents as $parent ) {
    				$item = get_term_by( 'id', $parent, get_query_var( 'taxonomy' ));
    				echo '<a href="' . get_term_link( $item->slug, 'product_cat' ) . '">' . $item->name . '</a>' . $delimiter;
    			}
    		}
    
    		$queried_object = $wp_query->get_queried_object();
    		echo $currentBefore . $queried_object->name . $currentAfter;
    
    	} elseif ( is_tax('product_tag') ) {
    
    		$queried_object = $wp_query->get_queried_object();
    		echo $prepend . $currentBefore . __('Products tagged &ldquo;', 'limelight') . $queried_object->name . '&rdquo;' . $currentAfter;
    
    } elseif ( is_search() ) {
    
    		echo $currentBefore . __( 'Search results for &ldquo;', 'limelight' ) . get_search_query() . '&rdquo;' . $currentAfter;
    
    	} elseif ( is_day() ) {
    
    		echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a>' . $delimiter;
    		echo '<a href="' . get_month_link(get_the_time('Y'),get_the_time('m')) . '">' . get_the_time('F') . '</a>' . $delimiter;
    		echo $currentBefore . get_the_time('d') . $currentAfter;
    
    	} elseif ( is_month() ) {
    
    		echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a>' . $delimiter;
    		echo $currentBefore . get_the_time('F') . $currentAfter;
    
    	} elseif ( is_year() ) {
    
    		echo $currentBefore . get_the_time('Y') . $currentAfter;
    
    	} elseif ( is_post_type_archive('product') && get_option('page_on_front') !== woocommerce_get_page_id('shop') ) {
    
    		$_name = woocommerce_get_page_id( 'shop' ) ? get_the_title( woocommerce_get_page_id( 'shop' ) ) : ucwords( get_option( 'woocommerce_shop_slug' ) );
    
    		if ( is_paged() ) {
    
    			echo $currentBefore . '<a href="' . get_post_type_archive_link('product') . '">' . $_name . '</a>' . $currentAfter;
    
    		} else {
    
    			echo $currentBefore . $_name . $currentAfter;
    
    		}
    
    	} elseif ( is_single() && !is_attachment() ) {
    
    		if ( get_post_type() == 'product' ) {
    
    			echo $prepend;
    
    			if ( $terms = wp_get_object_terms( $post->ID, 'product_cat' ) ) {
    				$term = current( $terms );
    				$parents = array();
    				$parent = $term->parent;
    
    				while ( $parent ) {
    					$parents[] = $parent;
    					$new_parent = get_term_by( 'id', $parent, 'product_cat' );
    					$parent = $new_parent->parent;
    				}
    
    				if ( ! empty( $parents ) ) {
    					$parents = array_reverse($parents);
    					foreach ( $parents as $parent ) {
    						$item = get_term_by( 'id', $parent, 'product_cat');
    						echo '<a href="' . get_term_link( $item->slug, 'product_cat' ) . '">' . $item->name . '</a>' . $delimiter;
    					}
    				}
    
    				echo '<a href="' . get_term_link( $term->slug, 'product_cat' ) . '">' . $term->name . '</a>' . $delimiter;
    
    			}
    
    			echo $currentBefore . get_the_title() . $currentAfter;
    
    		} elseif ( get_post_type() != 'post' ) {
    
    			$post_type = get_post_type_object( get_post_type() );
    			$slug = $post_type->rewrite;
    				echo $currentBefore . '<a href="' . get_post_type_archive_link( get_post_type() ) . '">' . $post_type->labels->singular_name . '</a>' . $currentAfter . $delimiter;
    			echo $currentBefore . get_the_title() . $currentAfter;
    
    		} else {
    
    			$cat = current( get_the_category() );
    			echo get_category_parents( $cat, true, $delimiter );
    			echo $currentBefore . get_the_title() . $currentAfter;
    
    		}
    
    	} elseif ( is_404() ) {
    
    		echo $currentBefore . __( 'Error 404', 'limelight' ) . $currentAfter;
    
    	} elseif ( ! is_single() && ! is_page() && get_post_type() != 'post' ) {
    
    		$post_type = get_post_type_object( get_post_type() );
    
    		if ( $post_type )
    			echo $currentBefore . $post_type->labels->singular_name . $currentAfter;
    
    	} elseif ( is_attachment() ) {
    
    		$parent = get_post( $post->post_parent );
    		$cat = get_the_category( $parent->ID );
    		$cat = $cat[0];
    		echo get_category_parents( $cat, true, '' . $delimiter );
    		echo $currentBefore . '<a href="' . get_permalink( $parent ) . '">' . $parent->post_title . '</a>' . $currentAfter . $delimiter;
    		echo $currentBefore . get_the_title() . $currentAfter;
    
    	} elseif ( is_page() && !$post->post_parent ) {
    
    		echo $currentBefore . get_the_title() . $currentAfter;
    
    	} elseif ( is_page() && $post->post_parent ) {
    
    		$parent_id  = $post->post_parent;
    		$breadcrumbs = array();
    
    		while ( $parent_id ) {
    			$page = get_page( $parent_id );
    			$breadcrumbs[] = '<a href="' . get_permalink($page->ID) . '">' . get_the_title( $page->ID ) . '</a>';
    			$parent_id  = $page->post_parent;
    		}
    
    		$breadcrumbs = array_reverse( $breadcrumbs );
    
    		foreach ( $breadcrumbs as $crumb )
    			echo $crumb . '' . $delimiter;
    
    		echo $currentBefore . get_the_title() . $currentAfter;
    
    	} elseif ( is_tag() ) {
    
    			echo $currentBefore . __( 'Posts tagged &ldquo;', 'limelight' ) . single_tag_title('', false) . '&rdquo;' . $currentAfter;
    
    	} elseif ( is_author() ) {
                    global $author;
    		$userdata = get_userdata($author);
    		echo $currentBefore . __( 'Author:', 'limelight' ) . ' ' . $userdata->display_name . $currentAfter;
    
    	}
    
    	if ( get_query_var( 'paged' ) )
    		echo ' (' . __( 'Page', 'limelight' ) . ' ' . get_query_var( 'paged' ) . ')';
    
    	echo $wrap_after;
    
    }
    }
  4. indybook
    Member
    Posted 1 year ago #

    ) Of course it does produce breadcrumbs, pretty proper.
    The mangle is good anyway. just check for the condition of *get_post_type != 'post'. there it will generate a category archive link no matter if the page actually exists

  5. Jonathan
    Member
    Posted 1 year ago #

    Indybrook, can you clarify how you would implements what you were suggesting? Perhahps show the actual code, I am having difficulty peicing it together.

    My breadcrumb code is working great, however I have now discovered that it breaks with woocommerce is not installed, so when i when to use it on another site...

  6. indybook
    Member
    Posted 1 year ago #

    Hi jonathantimar.
    it will break. the function uses woocommerce setting options as it looks. if no woo..oo.. endless plugin name... is installed there are no settings for it, right?

    try this. I'm not sure i'm being thorough enough and what things the site might have besides the lack of woocommerce. or what is the goal exactly. but this one works. let know if it won't behave for you

    function limelight_breadcrumbs()	{
    			$delimiter = ' ยป ';
    			$name = __("Home");
    			$currentBefore = ' <span class="current">';
    			$currentAfter = '</span> ';
    			$type=get_post_type();
    			if (!is_home() && !is_front_page() && get_post_type() == $type || is_paged()) {
    
    				echo '<nav id="breadcrumbs">';
    				global $post;
    				$home = get_bloginfo('url');
    				echo '<a href="' . $home . '">' . $name . '</a> ' . $delimiter . '';
    				if (is_category()) {
    					global $wp_query;
    					$cat_obj = $wp_query->get_queried_object();
    					$thisCat = $cat_obj->term_id;
    					$thisCat = get_category($thisCat);
    					$parentCat = get_category($thisCat->parent);
    					if ($thisCat->parent != 0) {
    						echo(get_category_parents($parentCat, true, '' . $delimiter . ''));
    					}
    					echo $currentBefore . single_cat_title() . $currentAfter;
    				}
    				else if (is_day()) {
    					echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> ' . $delimiter . '';
    					echo '<a href="' . get_month_link(get_the_time('Y'), get_the_time('m')) . '">' . get_the_time('F') . '</a> ' . $delimiter . ' ';
    					echo $currentBefore . get_the_time('d') . $currentAfter;
    				} else if (is_month()) {
    					echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> ' . $delimiter . '';
    					echo $currentBefore . get_the_time('F') . $currentAfter;
    				} else if (is_year()) {
    					echo $currentBefore . get_the_time('Y') . $currentAfter;
    				} else if (is_attachment()) {
    					echo $currentBefore;
    					the_title();
    					$currentAfter;
    				} if (is_single() && get_post_type() == $type ){
    					$cat = get_the_category();
    					$cat = $cat[0];
    					if ($cat !==NULL) {
    						echo get_category_parents($cat, true, ' ' . $delimiter . '');
    					}
    					echo $currentBefore;
    					the_title();
    					echo $currentAfter;
    				} else if (is_page() && !$post->post_parent) {
    					echo $currentBefore;
    					the_title();
    					echo $currentAfter;
    				} else if (is_page() && $post->post_parent) {
    					$parent_id = $post->post_parent;
    					$breadcrumbs = array();
    					while ($parent_id) {
    						$page = get_page($parent_id);
    						$breadcrumbs[] = '<a href="' . get_permalink($page->ID) . '">' . get_the_title($page->ID) . '</a>';
    						$parent_id = $page->post_parent;
    					}
    					$breadcrumbs = array_reverse($breadcrumbs);
    					foreach($breadcrumbs as $crumb)
    					echo $crumb . ' ' . $delimiter . ' ';
    					echo $currentBefore;
    					the_title();
    					echo $currentAfter;
    				} else if (is_search()) {
    					echo $currentBefore . __('Search Results For:','limelight') . ' ' . get_search_query() . $currentAfter;
    				} else if (is_tag()) {
    					echo $currentBefore . single_tag_title() . $currentAfter;
    				} else if (is_author()) {
    					global $author;
    					$userdata = get_userdata($author);
    					echo $currentBefore . $userdata->display_name . $currentAfter;
    				} else if (is_404()) {
    					echo $currentBefore . '404 Not Found' . $currentAfter;
    				}
    				if (get_query_var('paged')) {
    					if (is_home() || is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author()) {
    						echo  $currentBefore;
    					}
    					echo __('Page','limelight') . ' ' . get_query_var('paged');
    					if (is_home() || is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author()) {
    						echo $currentAfter;
    					}
    				}
    				echo '</nav>';
    			}
    		}
  7. Jonathan
    Member
    Posted 1 year ago #

    Hello indybrook,

    Thank you so much for helping me with this.

    That code does work, in so much as it does not break on custom post types, howver it also does not display the heiarchy of them either.

    So if I am on a custom post type, rather than displaying Home > post type root > post type category > post, it just displays a simple one level crumb.

    Bigger problem, is that if the page being displayed is a custom post type roote, or archive, then there is no crumb displayed at all, just home > and then a blank.

    I have been trying to decipher the woocommerce breadcrumb code and apply it in a generic way that will work on any custom post type, but it's beyond my skill level I am afraid.

  8. indybook
    Member
    Posted 1 year ago #

    hi Jonathan.
    by 'post type root' you mean 'parent category' or 'parent post'?
    the category hierarchy shows up fine for me both on single page and archive.
    is there 'taxonomies' and 'hierarchical' keys at your custom post type register function?

  9. cj3wilso
    Member
    Posted 1 year ago #

    Not displaying any parent categories for my custom post type either. I have registered categories for this post type. Though the category name is unique (mycat_category), hierarchical is true.

    Is it impossible to go through custom post types dynamically? Most queries I see use the name of the category to go through custom post type.. but I would rather use the Category ID so I can reuse the code!

Topic Closed

This topic has been closed to new replies.

About this Topic