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