categories - Create a category list page

admin2025-06-02  1

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.

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.

Share Improve this question edited Mar 31, 2015 at 23:10 fuxia 107k39 gold badges255 silver badges461 bronze badges asked Mar 31, 2015 at 13:33 AnuAnu 1831 gold badge1 silver badge4 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 10

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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748797832a313802.html

最新回复(0)