简体   繁体   English

创建if语句以按Wordpress中的cpt按类别过滤帖子

[英]Creating if statements to filter posts by category from cpt in wordpress

I am trying to put posts from a cpt into different bootstrap tabs. 我正在尝试将cpt中的帖子放入不同的引导标签。

So far I am getting all the posts titles on the first tab. 到目前为止,我在第一个选项卡上获得了所有帖子标题。 I then get one post title on the second tab and that same title on the following two tabs. 然后,我在第二个选项卡上获得一个帖子标题,在以下两个选项卡上获得相同的标题。 The categories are not custom taxonomies but the default wordpress categories, associated with the custom post type. 类别不是自定义分类法,而是与自定义帖子类型关联的默认wordpress类别。 The custom post type is called 'journal' and the code is inside archive-journal.php 自定义帖子类型称为“ journal”,代码位于archive-journal.php中

PHP 的PHP

<!-- Tab panes -->
<div class="tab-content">

    <?php if ( have_posts() ) : ?>
        <?php while ( have_posts() ) : the_post(); ?> 

            <!-- cambridge winter college -->
            <div role="tabpanel" class="tab-pane active" id="tab1">
                <!-- accordian -->
                    <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
                        <?php if (in_category('cambridge-winter-college')) ?>
                            <?php the_title(); ?>
                    </div>
                <!-- / accordian -->
                <div class="terminator"></div>
            </div>
            <!-- / cambridge winter college  -->

            <!-- oxford summer 1 -->
            <div role="tabpanel" class="tab-pane" id="tab2">
                <!-- oxford summer college 1 -->
                <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
                    <?php if (in_category('oxford_summer_college_1')) ?>
                        <?php the_title(); ?>
                </div>
            </div>
            <!-- / oxford summer 1 -->


            <!-- oxford summer college 2 -->
            <div role="tabpanel" class="tab-pane" id="tab3">
                <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
                    <?php if (in_category('oxford_summer_college_2')) ?>
                        <?php the_title(); ?>
                </div> 
            </div>
            <!-- / oxford summer college 2 -->

            <!-- cambridge summer college  -->
            <div role="tabpanel" class="tab-pane" id="tab4">
                <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
                    <?php if (in_category('cambridge_summer_college')) ?>
                        <?php the_title(); ?>
                </div>
            </div>
            <!-- /cambridge summer college -->

        <?php endwhile; ?>
    <?php endif; ?> 

</div>
<!-- / tab content -->

Any ideas on how to achieve this? 关于如何实现这一目标的任何想法?

Thanks 谢谢

You'll have to re-organize your code's structure. 您必须重新组织代码的结构。 The issue here is that you're displaying all tabs for each post because the tabs are inside the loop. 这里的问题是您正在显示每个帖子的所有选项卡,因为选项卡位于循环内。

Try to do the following: 尝试执行以下操作:

<div class="tab-content">
    <?php if ( have_posts() ) : ?>

        <!-- cambridge winter college -->
        <div role="tabpanel" class="tab-pane active" id="tab1">
            <!-- accordian -->
            <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
                 <?php while ( have_posts() ) : the_post(); ?>
                     <?php if (in_category('cambridge-winter-college')) : ?>
                         <?php the_title(); ?>
                     <?php endif; ?>
                 <?php endwhile; ?>
            </div>
            <!-- / accordian -->
            <div class="terminator"></div>
        </div>
        <!-- / cambridge winter college  -->

        <?php rewind_posts(); ?>

        <!-- REPEAT THE CODE ABOVE FOR OTHER TABS -->

    <?php endif; ?>
</div>
<!-- / tab content -->

This way you'll loop through the posts inside the tab. 这样,您将循环浏览选项卡内的帖子。

Let me know if that works. 让我知道是否可行。

UPDATE 更新

Calling rewind_posts() between each loop. 在每个循环之间调用rewind_posts()

After a lot of struggling and thinking I found the answer. 经过很多努力和思考,我找到了答案。 I had my max posts set to 10 in wordpress reading settings!! 我在wordpress阅读设置中将我的最高帖子设置为10 !! Doh! h! So a really simple fix by changing that setting or better still this, which I found on css tricks. 因此,通过更改该设置或更有效的方法,可以实现非常简单的修复,这是我在CSS技巧中找到的。 It calls all posts for the CPT with the -1 setting. 它使用-1设置为CPT调用所有帖子。 Change journal to your cpt name. 将日记更改为您的cpt名称。

PHP 的PHP

  // CPT all posts in Archive layout
function set_posts_per_page_for_journal_cpt( $query ) {
  if ( !is_admin() && $query->is_main_query() && is_post_type_archive( 'journal' ) ) {
    $query->set( 'posts_per_page', '-1' );
  }
}
add_action( 'pre_get_posts', 'set_posts_per_page_for_journal_cpt' );

?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM