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 user215615user215615
Add a comment  | 

1 Answer 1

Reset to default 0

You 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()