1

I'm at a loss on how to create a simple shortcode out of the following code:

 <?php echo category_description( $category_id ); ?> 

Looking to create something in the line of:

[cat_description id="category_id"]

Any help would be appreciated. Please note I don't have any experience with creating shortcodes and the tutorials I've tried really didn't cover what I'm trying to do, I think :)

The purpose for this shortcode is to be able to display the description of certain Post and Product (WooCommerce) Categories inside a Page or Post.

PHP code source: http://codex.wordpress.org/Function_Reference/category_description

  • I've added an answer as this seems like a very basic question, but for the future I'd advise that you post what you have tried before coming here within your question. Even if you fail, users on this site are much more likely to help you if they can see that you've at least tried to help yourself :) – David Gard Jan 23 '15 at 11:17
4

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 -

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.