5

I am trying to create a category list page that displays only the category titles with corresponding links.

It should not be PHP directly in the page. So how can I include a listing of categories on a page?

I am very new to WordPress and have searched many websites and found only how to list posts.

Please suggest a way I might be able to display categories.

7

To display a list of categories on your page by just putting something into the content area, you need a shortcode.

You can create a shortcode by using add_shortcode. This defines the tag and the function to call when that shortcode is used.

Here's a basic example that creates a shortcode [my_cat_list]:

/**
 * This creates the [my_cat_list] shortcode and calls the
 * my_list_categories_shortcode() function.
 */
add_shortcode( 'my_cat_list', 'my_list_categories_shortcode' );

/**
 * this function outputs your category list where you
 * use the [my_cat_list] shortcode.
 */
function my_list_categories_shortcode() {
    $args = array( 'echo'=>false );
    return wp_list_categories( $args ); 
}

Adding this snippet to your theme's functions.php file will create the shortcode.

Placing the shortcode [my_cat_list] into a post or page will then display a list of categories with links to them.

The example uses wp_list_categories() in the shortcode function to display a list of categories. The example just relies on the defaults for the function, but there are quite a few options for the way the list is output. See the documentation in the codex for wp_list_categories for a full explanation of all of the defaults and options and what they do.

  • In which file i can add shortcode ? – Ann Mary Apr 1 '15 at 4:24
  • That information is in the answer above, right after the code snippet. The snippet can go in your theme functions file, then you can use the shortcode in any WP post or page. – butlerblog Apr 1 '15 at 4:45
  • 1
    Your comment is both correct and incorrect, @shady sherif. It doesn't have anything to do with gutenberg. A shortcode should really never "echo" its result, but rather it should return it. That's not a change - it's always been that way. I'm not sure what I was thinking originally (this was originally posted in 2015), but regardless, thanks for mentioning it - I've corrected it accordingly. – butlerblog Mar 17 at 20:56
  • 1
    @butlerblog I thought that it was working before because of the normal editor doesn't care if you print or return. But now the Gutenberg doesn't allow you to add a shortcode which doesn't return. Thanks for the fix – shady sherif Mar 18 at 7:09
  • 1
    @jsmod - it depends on the use case. If it needs to be portable (like a plugin), then maybe use a shortcode. If it's more customizing your specific instance and you're in a child theme, then the template file. There isn't a "right" answer - use the the option that makes the most sense for your specific use case. – butlerblog Mar 18 at 13:12

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.