我想在首页上以下列格式显示门类帖子:
健康:第一、第二、第三、第三
体能:第一岗位,第二岗位,第三岗位
美第一、第二、第三、第三
示例站点: beautyhealthtips.in
有人帮我吗?
谢谢!
发布于 2018-10-20 14:12:44
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_posts,$health_posts,$fitness_posts和$beauty_posts。它们中的每一个都存储了一组3条帖子,您可以使用这些帖子来生成HTML。您可以使用一个输出整个HTML的短代码,然后在一个“页面”中插入这个短代码,最后将您的主页设置为“静态页面”。
注:$health_posts、$fitness_posts和$beauty_posts将分别存储属于这些类别的最新帖子。如果希望通过另一个参数(如视图数量)或随机检索这些帖子,则必须使用WP_Query而不是get_posts()。
https://wordpress.stackexchange.com/questions/317141
复制相似问题