如何在wordpress中按短代码列出类别?(How to list categories by shortcode in wordpress?)

无法在这个问题上找到答案,即使这很容易。 我想通过短代码显示当前帖子的内联类别,并用逗号分隔。 我在下面尝试过这个。

function genre( $atts, $content = null ) { $categories = the_category(', '); return '<div id="genre"><b>Genre: </b>' . $categories . '</div>'; } add_shortcode("genre", "genre");

这将返回Genre:

function genre( $atts, $content = null ) { $categories = get_the_category(', '); return '<div id="genre"><b>Genre: </b>' . $categories . '</div>'; } add_shortcode("genre", "genre");

这将返回Genre: Array

Could not find answer on this question, even this could be easy. I want to display inline categories of current post by shortcode and divided by comma. I tried it like this below.

function genre( $atts, $content = null ) { $categories = the_category(', '); return '<div id="genre"><b>Genre: </b>' . $categories . '</div>'; } add_shortcode("genre", "genre");

This returns Genre:

function genre( $atts, $content = null ) { $categories = get_the_category(', '); return '<div id="genre"><b>Genre: </b>' . $categories . '</div>'; } add_shortcode("genre", "genre");

This returns Genre: Array

最满意答案

function genre( $atts, $content = null ) { global $post; $categories = get_the_category_list( ', ', '', $post->ID ); return '<div id="genre"><b>Genre: </b>' . $categories . '</div>'; } add_shortcode("genre", "genre");

资料来源: http : //wordpress.org/support/topic/how-to-list-categories-by-shortcode

function genre( $atts, $content = null ) { global $post; $categories = get_the_category_list( ', ', '', $post->ID ); return '<div id="genre"><b>Genre: </b>' . $categories . '</div>'; } add_shortcode("genre", "genre");

Source: http://wordpress.org/support/topic/how-to-list-categories-by-shortcode

更多推荐