Search code examples
phpwordpresswoocommercecategoriesshortcode

How can I display the category name with a shortcode with wordpress and woocommerce?


I'm trying to fill the category description/text and have the category name change based on the category page you are on.

For example this category description: You are watching [category].

If you are on the category page with the name drinks, it should change the description/text to: You are watching drinks.

I tried to use the solution for retrieving the category name on this site, but that uses the product to show the category it belongs with. I'm trying to get the category name without any product, just the name of the category to fill the shortcode.


Solution

  • You should be able to use single_term_title() in a custom shortcode:

    function myprefix_category() {
    
        return single_term_title( '', false );
    
    }
    add_shortcode( 'category', 'myprefix_category' );
    

    If you want it to say the same thing each time you can add the text as a prefix:

    single_term_title( 'You are watching ', false );