如何在登录页面中为特殊类别的帖子设置不同的部分?

时间:2015-10-11 作者:Todo Pertin

我希望在我的WordPress网站的登录页上显示不同类别的帖子,而其他类别的帖子则显示在其他部分。

这在WordPress中可能吗?

3 个回复
SO网友:Bruno Kos

您可能需要使用WP\\u查询类为该特殊类别创建另一个循环。返回“cats”类别的循环示例如下:

// WP_Query arguments
$args = array (
    \'category_name\'          => \'cats\',
);

// The Query
$query = new WP_Query( $args );

// The Loop
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        // do something
    }
} else {
    // no posts found
}

// Restore original Post Data
wp_reset_postdata();
根据登录页上的节数,您将有多个循环,但不要忘记重置页面上的每个循环。

SO网友:RiaanZA

是的,这绝对是可能的。基本上,您需要将多个循环与自定义循环查询相结合,更多信息可在此处找到:The Loop

 <?php $query = new WP_Query( \'cat=CATEGORY ID YOU WANT TO DISPLAY HERE\' ); ?>
 <?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>

    OUTPUT HTML HERE

 <?php endwhile; endif; ?>

 <?php rewind_posts(); ?>

<?php $query = new WP_Query( \'cat=2ND CATEGORY ID YOU WANT TO DISPLAY HERE\' ); ?>
<?php if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post(); ?>

    OUTPUT HTML HERE

 <?php endwhile; endif; ?>
请记住rewind\\u posts()方法以使其正常工作

SO网友:grzybowski1911

我看到你对另一个答案的评论;哇,我不太喜欢编程或编码"E;所以我想我应该加入另一个选择。

如果您使用的是Elementor或Divi或其他类似的可视化生成器,他们通常会有一个博客帖子模块,您可以按类别进行筛选,这样您就可以在某个或多个类别的帖子页面上创建一个部分,而无需向站点添加代码。

例如,在Divi中,有一个模块允许这样做,它显示了下面的屏幕截图。

enter image description here

相关推荐