Dyvel
Dyvel

Reputation: 987

How to display Woocommerce category description with DIVI theme

I'm using DIVI theme and have created a custom category layout. However, I can't seem to find a way to add the category description to the layout. DIVI doesn't provide any answers, and I've searched here as well. Can I use some tags to get it?

The code below almost works - I do get the text, but not linebreaks, even though they are present if I do a echo '<pre>'; print_r($cat); echo '</pre>';

add_shortcode('cat_desc', 'cat_desc_shortcode');
function cat_desc_shortcode() {
    
    global $wp_query;
    $cat = $wp_query->get_queried_object();
        
    if( $cat == null ) return;

    $output = '<div class="page-description"> '.$cat->description.' </div> ';
    return $output;
}

Thanks

Upvotes: 1

Views: 2456

Answers (1)

Dyvel
Dyvel

Reputation: 987

Got it working now. For others in the same situation, add this code to your functions.php file in your (child) theme.

add_shortcode('cat_desc', 'cat_desc_shortcode');
function cat_desc_shortcode() {
    
    global $wp_query;
    $cat = $wp_query->get_queried_object();
    
    if( $cat == null ) return;

    $output = nl2br($cat->description);
    return $output;
}

Then use the shortcode [cat_desc] in your layout, where you would like the category description to appear. This at least works for me.

Upvotes: 4

Related Questions