call blog primary category

#279290
  • Resolved Luka Novakovic
    Rank Math free

    Hi,

    I would like to call the primary category of my blog posts through Rankmath.

    Previously with Yoast, I used this code to do it:

    // Add Shortcode
    function get_yoast_primary_cat() {

    Fill in your custom taxonomy here
    $yourTaxonomy = ‘category’;

    // SHOW YOAST PRIMARY CATEGORY, OR FIRST CATEGORY
    $category = get_the_terms( $postId, $yourTaxonomy );
    $useCatLink = true;

    // If post has a category assigned.
    if ($category){
    $category_display = ”;
    $category_link = ”;
    if ( class_exists(‘WPSEO_Primary_Term’) )
    {

    // Show the post’s ‘Primary’ category, if this Yoast feature is available, & one is set
    $wpseo_primary_term = new WPSEO_Primary_Term( ‘event_cat’, get_the_id() );
    $wpseo_primary_term = $wpseo_primary_term->get_primary_term();
    $term = get_term( $wpseo_primary_term );
    if (is_wp_error($term)) {

    // Default to first category (not Yoast) if an error is returned
    $category_display = $category[0]->name;
    $category_link = get_bloginfo(‘url’) . ‘/’ . ‘event-category/’ . $term->slug;
    } else {

    // Yoast Primary category
    $category_display = $term->name;
    $category_link = get_term_link( $term->term_id );
    }
    }
    else {

    // Default, display the first category in WP’s list of assigned categories
    $category_display = $category[0]->name;
    $category_link = get_term_link( $category[0]->term_id );
    }

    // Display category
    if ( !empty($category_display) ){
    if ( $useCatLink == true && !empty($category_link) ){
    echo ‘<span class=”post-category”>’;
    echo ‘‘.$category_display.’‘;
    echo ‘</span>’;
    } else {
    echo ‘<span class=”post-category”>’.$category_display.'</span>’;
    }
    }

    }

    }
    add_shortcode( ‘yoast-primary-category’, ‘get_yoast_primary_cat’ );

Viewing 3 replies - 1 through 3 (of 3 total)
  • Nigel
    Rank Math business

    Hello,

    Thank you for contacting us for help customising your code snippet to work with Rank Math.

    The new shortcode will be [rank-math-primary-category-shortcode]. The following code snippet is what what your function will look like:

    
    // Add Shortcode to display primary category
    function get_rank_math_primary_cat() {
        $yourTaxonomy = 'category';
        $category = get_the_terms( $postId, $yourTaxonomy );
        
        // In Rank Math Primary category is automatical arranged to first category in WP's list of assigned categories
        $category_display = $category[0]->name;
        $category_link = get_term_link( $category[0]->term_id );
        
        
        // Display category
        if ( !empty($category_display) && !empty($category_link) ){ 
            echo '<span class="post-category">'.$category_display.'</span>'; //Replace 'echo' with 'return' for easier control inside templates
        }   
            
    }
    add_shortcode( 'rank-math-primary-category-shortcode', 'get_rank_math_primary_cat' );
    

    I hope that helps. If you have questions, ask away. We are here to help.

    Hi Nigel,

    Thanks for helping, the code is working, but not sure about this part:

    // In Rank Math Primary category is automatical arranged to first category in WP’s list of assigned categories
    $category_display = $category[0]->name;
    $category_link = get_term_link( $category[0]->term_id );

    With this automated code, I can display on the FrontEnd just the first category in a row. Not the primary_term category. Is there a way to change this code to display a category from the PRIMARY_TERM dropdown? I tried to change something in the code but it doesn’t result in how I expected. Please help ASAP?

    Best,
    Luka

    Hello,

    The filter code my colleague provided displays the primary term that you have manually selected in your post as you can see in this screencast: https://i.rankmath.com/zFEVo1

    Hope this helps you with your situation and please don’t hesitate to contact us if you need our assistance with anything else.

    Hello,

    Since we did not hear back from you for 15 days, we are assuming that you found the solution. We are closing this support ticket.

    If you still need assistance or any other help, please feel free to open a new support ticket, and we will be more than happy to assist.

    Thank you.

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

The ticket ‘call blog primary category’ is closed to new replies.