How to create a shortcode to display a category description?

Try this. Add the code below to your functions.php file –

add_shortcode('cat_description', 'my_cat_description_shortcode');
function my_cat_description_shortcode($atts){

    $a = shortcode_atts( array(
        'id' => 0,
    ), $atts );

    return category_description($a['id']);

}

Should you wish to call the shortcode from a template (unnecessary really unless you add more to the shortcode) you can use this code –

<?php echo do_shortcode('[cat_description id="' . $category_id . '"]'); ?>

Here is some recommended reading for you –

Leave a Comment