Support » Plugin: Rank Math SEO – Best SEO Plugin For WordPress To Increase Your SEO Traffic » Display only Primary Category in product meta

  • Resolved alexis888

    (@alexis888)


    Hello.
    Is there a way to display only the primary category in the meta data of a product on a single product page, in the product archive page and “related product” loop too ?

    I have each product on my store that belongs to only one category ( Merch, Records, Print or Goodies) and sometimes to the Preorder category as well (which is never set as primary category). I never want the “Preorder” category term to be displayed in meta data.

    Any idea how I could do that ?

    I’m working on a local environnement atm so here is a pic to show what I want to remove: https://ibb.co/znkwH41
    Thanks !

Viewing 12 replies - 1 through 12 (of 12 total)
  • Plugin Author Rank Math

    (@rankmath)

    Hello @alexis888

    Thank you for contacting the support.

    When the Primary category is selected, Rank Math reorders the terms using get_the_terms filter. So if you are using get_the_terms()function then the first category this function returns would be the Primary category. You won’t have to do anything else.

    If you don’t want to use that function, then you can use get_post_meta() function to get the primary category ID by Post ID:
    $primary_category_id = get_post_meta( $post_id, 'rank_math_primary_category', true );

    Hope that helps. If there’s anything, please let us know.

    Thread Starter alexis888

    (@alexis888)

    Hello, thanks for your answer.
    I understand the logic of it but I’m really unable to tweak the code of my existing theme (OceanWP) to make it work :/

    The function displaying product categories in Product meta on a single product page is :

    <?php
    echo wc_get_product_category_list( $product->get_id(), ', ', '
    <span class="posted_in">' . _n( 'Category:', 'Categories:', 
    count( $product->get_category_ids() ), 'woocommerce' ) . ' ', '</span>' ); 
    ?>
    

    I couldn’t manage to find the function displaying the prodcut categories in the loop on the product Archive page though.

    Here is a link to a dev environement of the store. : http://flipside.sc2gaal6509.universe.wf/

    Any way you could help me tweak the code around to manage to solve my problem ?
    Thank you very much

    • This reply was modified 1 year, 8 months ago by alexis888.
    Plugin Author Rank Math

    (@rankmath)

    Hello @alexis888

    The wc_get_product_list() function uses get_the_term_list() function which in turn uses get_the_terms().

    So you won’t have to do anything. The first term in which the function returns would be your primary term.

    wc_get_product_list function:
    https://docs.woocommerce.com/wc-apidocs/source-function-wc_get_product_category_list.html#1077

    get_the_term_list function:
    https://core.trac.wordpress.org/browser/tags/5.4/src/wp-includes/category-template.php#L1281

    Hope that helps. Thank you.

    Thread Starter alexis888

    (@alexis888)

    Hello,
    thanks for your answer.
    However I’m sorry but I don’t get how to solve my problem despite your explanations. I’m not an advanced developper.

    I striped down the parameters of the function to understand how it works and came down to this (85 being the id of the product i’m testing on)

    <?php echo wc_get_product_category_list( 85, '', '' , '' ); ?>

    but I’m still getting both category terms displayed, not only the Primarty Category Term.
    I understand that’s because wc_get_product_category_list() uses get_the_term_list()function which in turn uses get_the_terms(), yey I don’t manage to get the result I want.

    The status of this post is not resolved as I still have my problem, even though it’s updated as such with each of your answers.

    Thanks

    Plugin Author Rank Math

    (@rankmath)

    Hello @alexis888

    That’s what we mentioned in the previous reply.

    The wc_get_product_category_list will return all the terms which are assigned to a product. The first term which the function returns will be the primary term.

    $terms = wc_get_product_category_list( $arguments );
    $terms[0]; // This will be the Pirmary term.

    Hope that helps. Thank you.

    Thread Starter alexis888

    (@alexis888)

    Hello.

    With this

    $terms = wc_get_product_category_list( $product->get_id(), ', ', 
    '<span class="posted_in">' . 
    _n( '', '', count( $product->get_category_ids() ), 'woocommerce' ) . 
    ' ', '</span>' );
    
    echo $terms[0];.

    I’m getting < as a result which is the first caracter of the <span>

    Same thing if I used my simplified version to test :
    <?php echo wc_get_product_category_list( 85, '', '' , '' ); ?>

    I’m getting < as a result which is the first caracter of the <a> links created

    If I var_dump($terms[0]);
    I’m getting
    C:\wamp64\www\flipside\wp-content\themes\oceanwp-child-theme-master\woocommerce\single-product\meta.php:38:string '<' (length=1)

    If I var_dump($terms);
    I’m getting

    C:\wamp64\www\flipside\wp-content\themes\oceanwp-child-theme-master\woocommerce\single-product\meta.php:38:string '<span class="posted_in"> <a href="http://localhost/flipside/print/" rel="tag">Print</a>, <a href="http://localhost/flipside/preorder/" rel="tag">Preorder</a></span>' (length=164)
    
    Plugin Author Rank Math

    (@rankmath)

    Hello @alexis888

    Here is a complete code:

    add_filter( 'term_links-product_cat', function( $links ) {
    	return [ $links[0] ];
    });

    Please add this code to your theme’s functions.php file.

    You can further filter it, so say for e.g. if you want to show the primary category only on page with ID 1, then you can use is_page( 1 ) condition.

    Hope that helps. Thank you.

    Thread Starter alexis888

    (@alexis888)

    Wonderful, it works like a charm. Thank you very much !

    Plugin Author Rank Math

    (@rankmath)

    Hello,

    Glad that helped.

    Please feel free to reach out to us again in case you need any other assistance.

    We are here to help. Take care!

    Hi @rankmath

    Please can you help with the complete code, I currently am only getting the < as my output as well in my setup, where i would like to get the Primary category as the output.

    But I am not sure how this
    $terms = wc_get_product_category_list( $arguments );
    $terms[0]; // This will be the Pirmary term.

    transitions to this
    add_filter( ‘term_links-product_cat’, function( $links ) {
    return [ $links[0] ];
    });

    Would appreciate if i could get the complete block of code please.

    Kind regards

    • This reply was modified 1 year, 2 months ago by temak.
    Plugin Author Rank Math

    (@rankmath)

    Hello @temak

    Can you please open a new support topic with us here?
    https://wordpress.org/support/plugin/seo-by-rank-math/#new-topic-0

    Looking forward to helping you. Thank you.

    Hi @rankmath

    Oh nevermind, I was able to sort it out using another method with PHP.

    Thanks for the help.

    Regards.

Viewing 12 replies - 1 through 12 (of 12 total)
  • The topic ‘Display only Primary Category in product meta’ is closed to new replies.