Breadcrumbs: Category not being displayed with CPTs

#46624
Viewing 4 replies - 1 through 4 (of 4 total)
  • Hello,

    Thank you for contacting Rank Math today.

    This seems to be an issue with the Add to Categories plugin. The categories don’t seem to show up even with Rank math deactivated. Please get in touch with the Add to Categories Team for them to help you with this.

    Looking forward to helping you. Thank you.

    ​​​​​​

    Alberto
    Rank Math business

    Hello,

    Thank you for contacting Rank Math and sorry for any inconvenience that might have been caused due to that.

    That plugin is not compatible by default with Rank Math (due to it adds categories to pages, which is not the usual WordPress behaviour), but I have coded for you a little snippet to make it compatible.

    Just add it at the end of the functions.php file in your active theme:

    /**
     * Allow Breadcrumbs compatibility with the 'Add Category to Pages' plugin
     *
     * @param array       $crumbs The crumbs array.
     * @param Breadcrumbs $this   Current breadcrumb object.
     */
    add_filter( 'rank_math/frontend/breadcrumb/items', function( $crumbs, $class ) {
    	$ret = $crumbs;
    	if(is_page()){
    		$categories = get_the_category();
    		
    		if (!empty($categories)) {
        		$mainCategory = array(
    				"0"=>esc_html($categories[0]->name),
    				"1"=>esc_url(get_category_link($categories[0]->cat_ID)),
    				"hide_in_scheme"=>false
    			);  
    			
    			array_splice($ret, 1, 0, array($mainCategory)); 
    		}
    	}
    	return $ret;
    }, 10, 2);

    Looking forward to helping you. Thank you.

    ​​​​​​​

    Constantin
    Rank Math business

    Amazing, thank you @Alberto! This just saved me days of work because unfortunately it isn’t easy to change the post type with Oxygen based pages. You should add this to your developer FAQ 🙂

    Todd
    Rank Math free

    Hello,

    We are super happy that this resolved your issue. If you have any other questions in the future, know that we are here to help you.

    If you don’t mind me asking, could you please leave us a review (if you haven’t already) on https://wordpress.org/support/plugin/seo-by-rank-math/reviews/#new-post about your overall experience with Rank Math? We appreciate your time and patience.

    If you do have another question in the future, please feel free to create a new forum topic, and it will be our pleasure to assist you again.

    Thank you.

Viewing 4 replies - 1 through 4 (of 4 total)

The ticket ‘Breadcrumbs: Category not being displayed with CPTs’ is closed to new replies.