Tuesday, May 30, 2023
The Great Tit
Updated: 30. 5. 2023, Added: 30. 5. 2023

How to get RankMath primary WooCommerce product category

WooCommerce is a popular e-commerce platform for WordPress that allows users to create and manage online stores. RankMath is a widely used SEO plugin for WordPress, offering various features to optimize websites for search engines. One of the valuable functionalities of RankMath is the ability to set a primary category for each product or post in WordPress. In this article, we will explore how to get the primary product category set by RankMath with PHP.

By default, WooCommerce does not provide a built-in way to designate a primary category for products. However, RankMath extends the functionality of WooCommerce. The primary category can be beneficial for SEO purposes and organizing products in a structured manner. To utilize this feature effectively, it is necessary to understand how to get and display the primary category on product pages with PHP.

Code snippet for WordPress Integration:

To retrieve the primary category assigned to the product in WooCommerce using the RankMath plugin, you can utilize the following code snippet:

<?php
    $primary_cat_id = get_post_meta( get_the_ID(), 'rank_math_primary_product_cat', true );
    $category_id = $primary_cat_id; // Replace with the category ID

    // Retrieve category object
    $category = get_term($category_id, 'product_cat');

    if (!empty($category)) {
        $category_link = get_term_link($category);
        $category_name = $category->name;

        // Output the category link and name
        echo '<a href="' . $category_link . '"><strong>' . $category_name . '</strong></a>';
    } else {
        // Handle the case when no primary category is set
    }
?>

The code above retrieves the primary category ID using the get_post_meta() function with the meta key 'rank_math_primary_product_cat' (you can use rank_math_primary_cat key for post categories). It then assigns the ID to the variable $category_id. After that, the code retrieves the category object using the get_term() function by passing the category ID and the taxonomy slug 'product_cat' (change to category if you want to use it for posts). If a valid category is found, it obtains the category link and name, and outputs them as an HTML link.

Shortcode integration:

o create a shortcode from the code provided, follow the steps below:

  1. Open the functions.php file of your WordPress theme or create a custom plugin.
  2. Add the following code to define the shortcode:
function retrieve_primary_category_shortcode() {
    ob_start();
    
    $primary_cat_id = get_post_meta( get_the_ID(), 'rank_math_primary_product_cat', true );
    $category_id = $primary_cat_id; // Replace with the category ID

    // Retrieve category object
    $category = get_term($category_id, 'product_cat');

    if (!empty($category)) {
        $category_link = get_term_link($category);
        $category_name = $category->name;

        // Output the category link and name
        echo '<a class="link--base" href="' . $category_link . '"><strong>' . $category_name . '</strong></a>';
    } else {
        // Handle the case when no primary category is set
    }
    
    return ob_get_clean();
}
add_shortcode( 'primary_category', 'retrieve_primary_category_shortcode' );
  1. Save the file or plugin and upload it to your WordPress site.

You can now use the [primary_category] shortcode in your WordPress editor or any text widget to display the primary category of a product on product page (!). When the shortcode is being processed, it will retrieve the Rank Math's primary product category and output the category link and name.

Example usage: [primary_category]

This shortcode provides a convenient way to display the primary category without directly modifying the template files.

Conclusion:

With the RankMath plugin, WooCommerce users can take advantage of the primary category feature to enhance their SEO efforts and organize their products effectively. By utilizing the provided code snippet, you can retrieve the primary category of a product and display it on the product pages of your WooCommerce store.

This allows visitors to easily navigate through main categories of products and improves the overall user experience.

Did I help you? Fuel our WordPress journey
Beep this article to your friends!

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram