admin管理员组文章数量:1130349
I want to display categories posts in the following format on the front home:
Health: 1st post 2nd post 3rd post
Fitness: 1st post 2nd post 3rd post
Beauty 1st post 2nd post 3rd post
Example site: beautyhealthtips.in
Does anybody help me?
Thanks!
I want to display categories posts in the following format on the front home:
Health: 1st post 2nd post 3rd post
Fitness: 1st post 2nd post 3rd post
Beauty 1st post 2nd post 3rd post
Example site: beautyhealthtips.in
Does anybody help me?
Thanks!
Share Improve this question asked Oct 20, 2018 at 6:56 rudyardsrudyards 233 bronze badges1 Answer
Reset to default 0function add_excluded_post_ids( $exclude_post_ids, $posts ) {
foreach( $posts as $post ) {
array_push( $exclude_post_ids, $post->ID );
}
return $exclude_post_ids;
}
$exclude_post_ids = array();
$latest_posts = get_posts( array(
'numberposts' => 3
) );
$exclude_post_ids = add_excluded_post_ids( $exclude_post_ids, $latest_posts );
$health_posts = get_posts( array(
'numberposts' => 3,
'category' => get_category_by_slug( 'health' )->term_id, // Change accordingly
'exclude' => $exclude_post_ids
) );
$exclude_post_ids = add_excluded_post_ids( $exclude_post_ids, $latest_posts );
$fitness_posts = get_posts( array(
'numberposts' => 3,
'category' => get_category_by_slug( 'fitness' )->term_id, // Change accordingly
'exclude' => $exclude_post_ids
) );
$exclude_post_ids = add_excluded_post_ids( $exclude_post_ids, $fitness_posts );
$beauty_posts = get_posts( array(
'numberposts' => 3,
'category' => get_category_by_slug( 'beauty' )->term_id, // Change accordingly
'exclude' => $exclude_post_ids
) );
Now you have $latest_posts, $health_posts, $fitness_posts and $beauty_posts. Each one of them stores a set of 3 posts which you can use to generate your HTML. You can use a shortcode that outputs the whole HTML, then insert this shortcode within a "Page" and finally set your homepage to that "Static page".
Note $health_posts, $fitness_posts and $beauty_posts will store the latest posts that belong to those categories, respectively. If you wish to retrieve those posts by another parameter, like amount of views, or randomly, you'll have to use WP_Query instead of get_posts().
I want to display categories posts in the following format on the front home:
Health: 1st post 2nd post 3rd post
Fitness: 1st post 2nd post 3rd post
Beauty 1st post 2nd post 3rd post
Example site: beautyhealthtips.in
Does anybody help me?
Thanks!
I want to display categories posts in the following format on the front home:
Health: 1st post 2nd post 3rd post
Fitness: 1st post 2nd post 3rd post
Beauty 1st post 2nd post 3rd post
Example site: beautyhealthtips.in
Does anybody help me?
Thanks!
Share Improve this question asked Oct 20, 2018 at 6:56 rudyardsrudyards 233 bronze badges1 Answer
Reset to default 0function add_excluded_post_ids( $exclude_post_ids, $posts ) {
foreach( $posts as $post ) {
array_push( $exclude_post_ids, $post->ID );
}
return $exclude_post_ids;
}
$exclude_post_ids = array();
$latest_posts = get_posts( array(
'numberposts' => 3
) );
$exclude_post_ids = add_excluded_post_ids( $exclude_post_ids, $latest_posts );
$health_posts = get_posts( array(
'numberposts' => 3,
'category' => get_category_by_slug( 'health' )->term_id, // Change accordingly
'exclude' => $exclude_post_ids
) );
$exclude_post_ids = add_excluded_post_ids( $exclude_post_ids, $latest_posts );
$fitness_posts = get_posts( array(
'numberposts' => 3,
'category' => get_category_by_slug( 'fitness' )->term_id, // Change accordingly
'exclude' => $exclude_post_ids
) );
$exclude_post_ids = add_excluded_post_ids( $exclude_post_ids, $fitness_posts );
$beauty_posts = get_posts( array(
'numberposts' => 3,
'category' => get_category_by_slug( 'beauty' )->term_id, // Change accordingly
'exclude' => $exclude_post_ids
) );
Now you have $latest_posts, $health_posts, $fitness_posts and $beauty_posts. Each one of them stores a set of 3 posts which you can use to generate your HTML. You can use a shortcode that outputs the whole HTML, then insert this shortcode within a "Page" and finally set your homepage to that "Static page".
Note $health_posts, $fitness_posts and $beauty_posts will store the latest posts that belong to those categories, respectively. If you wish to retrieve those posts by another parameter, like amount of views, or randomly, you'll have to use WP_Query instead of get_posts().
本文标签: Display Specific Categories posts on the home page
版权声明:本文标题:Display Specific Categories posts on the home page 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749243701a2338525.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论