• Im having a trouble creating a short code, that gives me the ability to show a list of all my categories along with the number of posts in them.

    Ex:
    Travelling (2)
    Fashion (3)
    Food (0)

    The listed categories should also work as links to the posts regarding that category.
    As i understand after a lot of google’ing, what i need is a shortcode that does the same as the wp_list_categories function

    <ul>
    <?php wp_list_categories('orderby=name&show_count=1'); ?>
    </ul>

    The reason why i need it to be a shortcode, is that i have to use it in a text widget – unfortunately the category-widget is not suitable for my purpose.

Viewing 3 replies - 1 through 3 (of 3 total)
  • This should get you started:

    // Shortcode to return wp_list_categories()
    // Usage: [mam_list_categories]
    function mam_list_categories_func() {
       $cat_list =  wp_list_categories('orderby=name&show_count=1&echo=0');
       return "<ul>$cat_list</ul>";
    }
    add_shortcode('mam_list_categories','mam_list_categories_func');
    Thread Starter j4ze

    (@j4ze)

    Thank you so much for your help, I’ve figured out some code that works for me, but however I’m still not fully satisfied.
    This is my code:

    function list_categories_func($atts){
    
    $cat_list = wp_list_categories('orderby=name&show_count=1&echo=0');
    
    return $cat_list . '</ul>';
    }
    add_shortcode('list-categories', 'list_categories_func');

    And it returns this list:
    – Categories
    – Category 1 (x posts)

    You can see it on my site, left widget menu: https://thatgirlcanez.com [NSFW]

    I want to remove the category-title “Categories” since i already have a button showing that. And then i want all of my categories to show – even if they don’t have posts, so it would look like this:
    -Category 1 (2)
    -Category 2 (1)
    -Category 3 (0)
    and so on.

    Add this:
    ‘orderby=name&show_count=1&echo=0&title_li=&hide_empty=0

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Making a shortcode?’ is closed to new replies.