skip to Main Content

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.

2

Answers


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

    function myprefix_get_wc_category {
        return single_term_title( '', false );
    }
    add_shortcode( 'category', 'myprefix_get_wc_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 );
    
    Login or Signup to reply.
  2. You can use this shortcode with prefix attribute.

    add this code in your theme’s functions.php file then add the shortcode in your archive.php file.

    example :- do_shortcode('[category prefix="You are watching "]');

    Or you can leave blank if you don’t need the prefix like do_shortcode('[category]');.

    function zillion_cat_shortcode( $atts ) {
        $atts = shortcode_atts(
            array(
                'prefix' => '',
            ), $atts, 'category' );
     
        $prefix = $atts['prefix'] != '' ? $atts['prefix'] : ''; 
        echo single_term_title( $prefix , false );
    }
    add_shortcode( 'category', 'zillion_cat_shortcode' );
    

    Reference :- https://developer.wordpress.org/reference/functions/single_term_title/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search