admin管理员组文章数量:1201147
How can I get a random category name one at a time?? The post may have multiple category instead of a single category. I just want to show a single category name in the post grid loop.
$home_blog_posts = get_posts(array(
'posts_per_page' => 3
));
Thanks.
How can I get a random category name one at a time?? The post may have multiple category instead of a single category. I just want to show a single category name in the post grid loop.
$home_blog_posts = get_posts(array(
'posts_per_page' => 3
));
Thanks.
Share Improve this question asked Apr 13, 2022 at 6:40 user215615user2156151 Answer
Reset to default 0You can use wp_get_post_categories
to get the categories from your posts. Then you can use array_rand
to get one random category from that array.
Check out these links on how to use wp_get_post_categories
and array_rand
to suit your needs:
https://developer.wordpress.org/reference/functions/wp_get_post_categories/ https://www.php.net/manual/en/function.array-rand.php
本文标签: theme developmentHow to get a random single category name in getposts()
版权声明:本文标题:theme development - How to get a random single category name in get_posts()? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738629510a2103658.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论