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
) );
现在您有$latest\\u posts、$health\\u posts、$fitness\\u posts和$beauty\\u posts。它们中的每一个都存储一组3篇文章,您可以使用这些文章生成HTML。您可以使用一个输出整个HTML的短代码,然后将此短代码插入到“页面”中,最后将主页设置为“静态页面”。
注意$health\\u posts、fitness\\u posts和$beauty\\u posts将分别存储属于这些类别的最新帖子。如果您希望通过其他参数(如视图数量)或随机检索这些帖子,则必须使用WP_Query
而不是get_posts()
.