首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在主页上显示特定类别的帖子

在主页上显示特定类别的帖子
EN

WordPress Development用户
提问于 2018-10-20 06:56:53
回答 1查看 36关注 0票数 0

我想在首页上以下列格式显示门类帖子:

健康:第一、第二、第三、第三

体能:第一岗位,第二岗位,第三岗位

美第一、第二、第三、第三

示例站点: beautyhealthtips.in

有人帮我吗?

谢谢!

EN

回答 1

WordPress Development用户

发布于 2018-10-20 14:12:44

代码语言:javascript
复制
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()

票数 0
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/317141

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档