Wordpress shortcode for wp_list_categories()
I'm trying to list all the categories in a page. I tried doing it but I'm not that professional, and I kept getting error on functio开发者_开发百科ns.php.
thanks for your help
That´s true. If you use the shortcode in a widget/sidebar, the category-list will appear in weird places. To prevent this behavior, just add echo=0 to wp_list_categories(). For the example above this would be:
wp_list_categories("echo=0&orderby=name&include=3,5,9,16")
Look at the Wordpress docs for wp_list_categories: Template Tags/wp list categories « WordPress Codex
There are lots of parameters, and a basic example from the page linked above is:
To sort categories alphabetically and include only the categories with IDs of 16, 3, 9 and 5, you could write the following code:
<ul>
<?php wp_list_categories('orderby=name&include=3,5,9,16'); ?>
</ul>
That gives you the basic function. To turn that into a shortcode, do this in the theme's functions.php file:
function my_wp_list_categories_shortcode() {
return '<ul>' . wp_list_categories('orderby=name&include=3,5,9,16') . '<ul>' ; }
add_shortcode( 'list_my_categories_with_this_shortcode', 'my_wp_list_categories_shortcode' );
The shortcode you add via the WordPress editor is [list_my_categories_with_this_shortcode]
精彩评论