• Hi,

    I am trying to create a simple breadcrumb of all the categories the post belongs to.

    At most, they are 2 levels deep i.e.

    Parent >> Child

    Otherwise, in other cases, is just the parent category. So, never more than two categories assigned to a post – Parent, Child or Category -> Subcategory.

    Using the following code, it works, but the problem is it does not always show the expected result:

    Parent >> Child

    BUT:

    Child >> Parent

    If the name of the subcategory is alphabetically before the parent category.

    if ( is_single() ) {
    $separator = ' &raquo ';
    $postID = $post->ID;
    $catlink = get_the_category_list($separator,'multiple', $postID);
    
    		//find >> in the string $catlink
    		 $exists = strpos($catlink, $separator);
    
    		if ($exists === false) {
        		$catlink = $catlink . $separator;
    		}
    		else
    		{
       		 	$catlink = strstr($catlink, $separator);
    			$catlink = ltrim($catlink, $separator);
    		} 
    
    	$breadcrumb = '<div class="breadcrumb"><div class="breadcrumb-trail">' . $catlink .'<span class="trail-end"></span></div></div>';
    }

    Can someone please help me get the right code/functions to get the correct result?

    Your help is much appreciated.

    Regards,
    Indy

Viewing 8 replies - 16 through 23 (of 23 total)
Viewing 8 replies - 16 through 23 (of 23 total)
  • The topic ‘Category breadcrumb for a post’ is closed to new replies.