• 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 15 replies - 1 through 15 (of 23 total)
  • Hello Indy,

    I will try a bit, first it seems there are various “issues” with this code.
    First one, is logic is good, but you base code upon string location (strpos) whereas you should, to me, look at cat ids (so numbers).

    Displaying it in breadcrumb should come later i think (only my opinion) in the rolling of code (thus using strings).

    I try to come with a suggestion in 5 minutes.

    <?php
    
    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">' . echo $catlink .'<span class="trail-end"></span></div></div>';
    }
    
    ?>

    This code will probably bug, but your one was not displaying result.

    Inside the html tags of $breadcrumb line, you must use at some point either echo() or print(), otherwise it will be forever blank ($catlink is processed, but not displayed in text in page as HTML form.

    Hope it helps,

    Thread Starter indyparker

    (@indyparker)

    Hi Digico,

    Many thanks for your reply.

    The code you have sent in your message is the same?

    I cannot see any difference?

    Please explain.

    Indy

    To fully debug it and be sure before $php variables are full of data, try this variation please:

    <?php
    
    echo "
    <pre>".var_dump($GLOBALS)."</pre>
    ";
    
    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">' . echo $catlink .'<span class="trail-end"></span></div></div>';
    }
    
    ?>

    It will show you a table of variables before calling the loop, and if all $something php $functions are filled with data or empty before the loop.

    Thread Starter indyparker

    (@indyparker)

    Hi Digico,

    Actually, the code displays fine.

    Please see here – the category breadcrumb output at the top, above the post title:

    https://goo.gl/mQDEx7

    Cinema >> Entertainment

    Here is an example of it displaying it wrong:

    https://goo.gl/yXOSXF

    Where it says:

    Bollywood >> Cinema

    Regards,
    Indy

    Thread Starter indyparker

    (@indyparker)

    So, the correction should be:

    Cinema >> Bollywood

    because

    Cinema is parent of Bollywood.

    Ok, my bad, both have exactly the same wp_loop sure?

    If that so it’s I think related to your ltrim, ltrim is very strict, so it’s possible its trims more than the >> separator… then bug!

    Because you add strstr and ltrim in same function of same string, it’s surely cuts string too much to a point where it can revert it. At least my guess if all data is ok with globals.

    Yes but you shouldn’t use string functions for that, you used Post->ID then you should use Cat>ID or similar too, not string detection too.

    If you need another taxonomy you might use tags too, for each post, no?

    Thread Starter indyparker

    (@indyparker)

    Not sure .. this is why I need help.

    I just need:

    Parent >> Child

    displayed, if the post has both categories ticked in the WP Editor.

    And not

    Child >> Parent

    What happens if you strip ltrim for your current code line? I’m quite sure it will work quite ok.

    How to explain? Normally the wp_query is done before you push any button. Trying to display them while data is not yet queried in database, or melting several ones will results in no solution (aka blank page because of error, or just no useable result like your breadcrumb).

    I don’t give up, but i can say custom breadcrumb is far from easy.

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