Echo Primary Category

#67293
  • Resolved Alex
    Rank Math pro

    Hi, I have a custom taxonomy (geolocation) for a CPT (casino_review). How can I echo the primary taxonomy term in this case please?

    I saw this code on a couple of other threads, it would be great to know which part is which so I can adapt and use it:

    $primary_cat_id = get_post_meta( $product->id, 'rank_math_primary_category', true );
    if ( $primary_cat_id ) {
        $product_cat = get_term( $primary_cat_id, 'product_cat' );
        $markup['brand'] = $product_cat->name;
    }

    Thanks!

Viewing 15 replies - 1 through 15 (of 17 total)
  • Alberto
    Rank Math business

    Hello,

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

    With the “echo”, where do you mean you would like to display it? Since the code you shared is to modify the brand in the schema, so to help you to adapt it we would need to know where you want to use that primary term.

    Looking forward to helping you. Thank you.

    ​​​​​​​

    Perhaps the code is not so helpful after all, it was just the closest thing I could find.

    Basically I want to echo the primary taxonomy in my page template, and ideally use it in a php $variable as well.

    Hello,

    This line:

    
    $primary_cat_id = get_post_meta( $product->id, 'rank_math_primary_category', true );
    

    is the one used to get the product page primary category and assigns it to a variable named $primary_cat_id. You can use or echo this variable as you page primary category.

    Hope that helps. If you have any further question(s), please let us know. Thank you.

    Thank you. So what would the code look like in my use case, where the primary taxonomy is on a CPT called ‘casino_review’ and the taxonomy name is ‘geolocation’.

    I assume that I’m right in thinking that this option is generated by Rank Math?

    Screenshot: https://drive.google.com/file/d/1AH5YKa5kfkrQ6rGsApfQ-8c5wWXH3Djo/view?usp=sharing

    • This reply was modified 3 years, 10 months ago by Alex.
    • This reply was modified 3 years, 10 months ago by Alex.

    Hello,

    Yes, the option you showed in the screenshot is added by the Rank Math plugin. You can use the code my colleague provided to get the Primary term id and based on the term id you can get the term object by using get_term_by() function. Here is an example:

    
    $primary_term_id = get_post_meta( $product->id, 'rank_math_primary_geolocation', true );
    $primary_term = get_term_by( 'id', $primary_term_id, 'geolocation' );
    echo $primary_term->name;
    

    I hope that helps.

    The code didn’t work unfortunately (returned nothing), so I tried to adapt it:

    $post_id = get_the_ID();
    $post_meta = get_post_meta($post_id, 'rank_math_primary_geolocation', false);
    $primary_term = get_term_by( 'id', $post_meta, 'geolocation' );
    echo $primary_term->name;

    $primary_term returns nothing, but $post_meta returns ‘array’.

    Any further ideas would be appreciated.

    Alberto
    Rank Math business

    Hello,

    It returns the array due to the “false” added in the code, just do this modification:

    $post_id = get_the_ID();
    $post_meta = get_post_meta($post_id, 'rank_math_primary_geolocation', true);
    $primary_term = get_term_by( 'id', $post_meta, 'geolocation' );
    echo $primary_term->name;

    Looking forward to help you.

    Unfortunately, still doesn’t work. However I’m now using an alternative solution.

    Thanks for trying 🙂

    Alberto
    Rank Math business

    Hello,

    It is fine, I am happy you were able to find a solution. Do you want us to close this thread? We will be here to help you with any other questions/issues you might have in the future.

    Looking forward to help you.

    Feel free to close it 🙂

    Alberto
    Rank Math business

    Hello Alex,

    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.

    Puneet
    Rank Math free

    Hi, i use <?php $primary_cat_id = get_post_meta($post->ID, 'rank_math_primary_category', false ); ?><?php print_r( $primary_cat_id ); ?>
    but it’s print Category ID, not category name.
    For example our primary category Name is Example and it’s Id is 1 then according this code output will 1 instead Example.
    So how to fix this code?

    Alex
    Rank Math pro

    Does this work?

    <?php $primary_cat_id = get_post_meta($post->title, 'rank_math_primary_category', false ); ?>
    
    <?php $primary_cat_id->taxonomy; ?>
    Puneet
    Rank Math free

    Not working. nothing prints.

    Alex
    Rank Math pro

    Correction:

    <?php $primary_cat_id = get_post_meta($post->title, 'rank_math_primary_category', false ); ?>
    
    <?php echo $primary_cat_id->taxonomy; ?>
Viewing 15 replies - 1 through 15 (of 17 total)

The ticket ‘Echo Primary Category’ is closed to new replies.