Support » Developing with WordPress » adding parent category to breadcrumb

  • hi

    how can I add parent category to this function?

    it’s for the breadcrumb

    <?php
    function the_breadcrumb() {
        if (!is_home()) {
            echo '<a href="';
            echo get_option('home');
            echo '">';
            bloginfo('name');
            echo "</a> >> ";
    
            if (is_category() || is_single()) {
                the_category('title_li=');
                if (is_single()) {
                    echo " >> ";
                    the_title();
                }
            } elseif (is_page()) {
                echo the_title();
            }
        }
    }
    ?>
    • This topic was modified 8 months, 3 weeks ago by  Steve Stern.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Hello,

    There are many Breadcrumb plugin which you can use which are light-weight and can help you to achieve this, even though if you wish to achieve this via code below is the reference for the same.

    $category = get_the_category(); 
    
    $category_parent_id = $category[0]->category_parent;
    if ( $category_parent_id != 0 ) {
        $category_parent = get_term( $category_parent_id, 'category' );
        echo '<li>'.$category_parent->name.'<li class="separator"><i class="x-icon-angle-right" data-x-icon=""></i></li><li>'.$category[0]->name.'</li>';
    } else {
        echo '<li>'.$category[0]->name.'</li>';
    }          
    
                if (is_single()) {
                    echo '<li class="separator"><i class="x-icon-angle-right" data-x-icon=""></i></li><li>';
                    the_title();
                    echo '</li>';
                }
    

    I have not tried it so let me know if this helps.

    Thanks.

    Hi there,

    I am looking for the same thing… I was wondering if someone could be so kind to possibly explain this/provide a specific way to achieve it? I want to add the “parent category” into my breadcrumbs on POSTS. Currently I am getting the “category” but not the “parent category”. This is the code I’m using:

    add_filter('x_breadcrumbs_data', 'post_parent_trail', 9999999, 2);
    
    	function post_parent_trail ($crumbs, $args) {
    		if ( is_singular('post') ) {
    
    			$category = get_the_category( get_the_ID() );
    
    				if ( count( $category ) > 0 ) {
    
    					$cat_array = array();
    					$cat_array[1]['label'] = $category[0]->name;
    					$cat_array[1]['url'] = get_category_link( $category[0]->term_id );
    
    					array_splice($crumbs, 2, 0, $cat_array);
    				}
    		}
    	return $crumbs;  
    }
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘adding parent category to breadcrumb’ is closed to new replies.