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 2 replies - 16 through 17 (of 17 total)
  • Puneet
    Rank Math free

    I have tried how and found the following results:-

    1. Code that you provide

    <?php $primary_cat_id = get_post_meta($post->title, ‘rank_math_primary_category’, false ); ?><?php echo $primary_cat_id->taxonomy; ?>

    Output – Blank (Nothing Prints)

    2. Modified by me

    <?php $primary_cat_id = get_post_meta($post->title, ‘rank_math_primary_category’, true ); ?>
    <?php print_r( $primary_cat_id->taxonomy ); ?>

    Output – Blank (Nothing Prints)

    3. <span style=”color:green”>Code</span> Customized code for print primary category

    <?php $category = get_the_category(); $category[0]->cat_name; $category_link = get_category_link( $category[0] ); ?>
    <?php echo $category[0]->cat_name; ?>

    Output Primary Category Name with URL

    Hello,

    Thank you for sharing the modified code. We are sure others will find it helpful.

    If you need help with anything else, please open a new support ticket here so we can help.

    We are always here for assistance.

Viewing 2 replies - 16 through 17 (of 17 total)

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