admin管理员组文章数量:1325510
I am trying to display the categories name on my page but it's not displaying. I am using the below code and I added the shortcode gridCategories
on my page. I am getting only array
function createGridCategories(){
$categories = get_categories( array(
'taxonomy' => 'category',
'orderby' => 'name',
'parent' => 0,
'hide_empty' => 0, // change to 1 to hide categores not having a single post
) );
var_dump($categories);
return $categories;
}
add_shortcode( 'gridCategories', 'createGridCategories');
I tried this also
$categories = get_the_category();
var_dump($categories);
I added a shortcode like this in textblock
But still, I am not getting any output on my page. Is there any issue with my code?
I am trying to display the categories name on my page but it's not displaying. I am using the below code and I added the shortcode gridCategories
on my page. I am getting only array
function createGridCategories(){
$categories = get_categories( array(
'taxonomy' => 'category',
'orderby' => 'name',
'parent' => 0,
'hide_empty' => 0, // change to 1 to hide categores not having a single post
) );
var_dump($categories);
return $categories;
}
add_shortcode( 'gridCategories', 'createGridCategories');
I tried this also
$categories = get_the_category();
var_dump($categories);
I added a shortcode like this in textblock
But still, I am not getting any output on my page. Is there any issue with my code?
Share Improve this question edited Aug 9, 2020 at 8:41 fuxia♦ 107k38 gold badges255 silver badges459 bronze badges asked Aug 9, 2020 at 8:13 user9437856user9437856 1117 bronze badges1 Answer
Reset to default 1Is there any issue with my code?
Yes, there is. get_categories()
returns an array of terms, e.g. WP_Term
objects or a list of term IDs, so you can't simply do return $categories;
.
Instead,
For a basic list such as
Foo category, Bar category, etc.
(i.e. no category links), you can simply set thefields
parameter tonames
which then gives you a list of category names:function createGridCategories() { $categories = get_categories( array( 'fields' => 'names', 'hide_empty' => 0, // other args here ) ); return implode( ', ', $categories ); }
Or you can manually loop through the terms and just build the markup/HTML to your own liking:
function createGridCategories() { $categories = get_categories( array( 'hide_empty' => 0, // other args here ) ); $list = ''; foreach ( $categories as $term ) { $url = get_category_link( $term ); $list .= '<li><a href="' . esc_url( $url ) . '">' . esc_html( $term->name ) . '</a></li>'; } return "<ul>$list</ul>"; }
Or alternatively (for a list/
UL
like the above), you can usewp_list_categories()
:function createGridCategories() { $list = wp_list_categories( array( 'taxonomy' => 'category', 'hide_empty' => 0, 'echo' => 0, 'title_li' => '', // other args here ) ); return "<ul>$list</ul>"; }
本文标签: plugin developmentHow to display the categories on page using shortcode
版权声明:本文标题:plugin development - How to display the categories on page using shortcode? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742197323a2431318.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论