$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Display Specific Categories posts on the home page|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Display Specific Categories posts on the home page

matteradmin9PV0评论

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

1 Answer 1

Reset to default 0
function 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().

Post a comment

comment list (0)

  1. No comments so far