类别过滤器中的发布循环不起作用 [英] Post loop in category filter not working

查看:70
本文介绍了类别过滤器中的发布循环不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有4个引导程序选项卡的页面。每个选项卡都包含来自不同类别的自定义帖子类型帖子。 4个类别,4个标签。使用in_category()应该很容易。但是由于某种原因,我只能过滤第一个标签。其余三个选项卡根本不显示任何帖子。

I have a page with 4 bootstrap tabs. Each tab is to contain custom post type posts form a different category. 4 categories, 4 tabs. This should be really easy using in_category(); but for some reason I am only able to filter for the first tab. The remaining three tabs show no post at all.

请针对我的方法查看此问题:
创建if语句以按类别从wordpress中的cpt过滤帖子

Please see this question for my approach: Creating if statements to filter posts by category from cpt in wordpress

这是来自archive-journal.php的代码,其中journal是我的cpt。我正在使用高级自定义字段。

Here is the code from archive-journal.php, where journal is my cpt. I am using advanced custom fields.

PHP

<!-- Tab panes -->
<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 $entry_count = 0; ?>
                        <?php while ( have_posts() ) : the_post(); ?> 
                            <?php if (in_category('cambridge-winter-college')) : ?>

                                <?php $entry_count++; ?>

                                <!-- entry -->
                                <div class="entry">
                                    <div class="col-lg-3 date">
                                        <time class="cbp_tmtime"><span><?php the_field( 'day' ); ?></span><br> <span><?php the_field( 'date' ); ?><sup><?php the_field( 'suffix' ); ?></sup> <?php the_field( 'month' ); ?></span> </time>
                                        <div class="cbp_tmicon"></div>
                                    </div>

                                    <div class="panel-heading panel-head col-lg-9">
                                        <a data-toggle="collapse" data-parent="#accordion" href="#collapse-cw-<?php echo $entry_count; ?>">
                                          View <?php the_field( 'day' ); ?>'s journal entry
                                        </a>
                                    </div>

                                    <div id="collapse-cw-<?php echo $entry_count; ?>" class="panel-collapse collapse in col-lg-12 pull-right">
                                      <div class="panel-body col-lg-9 pull-right">
                                        <div class="row">
                                            <div class="col-lg-12 entry-text">

                                                <aside class="col-lg-6 pull-right flexbox-2">       
                                                    <div class="schedule" >
                                                        <?php $table = get_field( 'schedule' ); 
                                                             if ( $table ) { 
                                                                 echo '<table>'; 
                                                                     if ( $table['header'] ) { 
                                                                         echo '<thead><tr>'; 
                                                                             echo ''; 
                                                                                 foreach ( $table['header'] as $th ) { 
                                                                                     echo '<th>'; 
                                                                                         echo $th['c']; 
                                                                                     echo '</th>'; 
                                                                                 } 
                                                                             echo '</tr>'; 
                                                                         echo '</thead>'; 
                                                                     } 

                                                                     echo '<tbody>'; 
                                                                         foreach ( $table['body'] as $tr ) { 
                                                                             echo '<tr>'; 
                                                                                 foreach ( $tr as $td ) { 
                                                                                     echo '<td>'.$td['c'].'</td>'; 
                                                                                 } 
                                                                             echo '</tr>'; 
                                                                         } 
                                                                     echo '</tbody>'; 
                                                                 echo '</table>'; 
                                                             } ?>  
                                                    </div>
                                                    <?php if (the_field('fun_fact')) { ?>
                                                    <div class="fun-fact" style="">
                                                        <?php the_field( 'fun_fact' ); ?>
                                                    </div>
                                                    <?php } ?>
                                                </aside>

                                                <div class="flexbox-1">
                                                    <?php the_field( 'entry_text' ); ?>
                                                </div>

                                            </div>                          
                                        </div>

                                        <div class="row carousel-wrapper">

                                            <div id="carousel-cw-<?php echo $entry_count; ?>" class="carousel slide" data-ride="carousel">                          

                                              <div class="carousel-inner" role="listbox">

                                                  <?php if ( have_rows( 'slider' ) ) : ?>
                                                    <?php $image_count = 0 ?>
                                                    <?php while ( have_rows( 'slider' ) ) : the_row(); ?>
                                                    <?php $image_count++; ?>    
                                                        <?php if ( get_sub_field( 'image') ) { ?>
                                                            <div class="item <?php if ($image_count == 1) echo 'active' ?>">
                                                              <img src="<?php the_sub_field( 'image' ); ?>" title="">
                                                              <figcaption><?php the_sub_field( 'caption' ); ?></figcaption>
                                                            </div>
                                                        <?php } ?>
                                                    <?php endwhile; ?>
                                                <?php else : ?>
                                                    <?php // no rows found ?>
                                                <?php endif; ?>

                                              <a class="left carousel-control" href="#carousel-cw-<?php echo $entry_count; ?>" role="button" data-slide="prev">
                                                <span class="fa fa-chevron-left glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
                                                <span class="sr-only">Previous</span>
                                              </a>
                                              <a class="right carousel-control" href="#carousel-cw-<?php echo $entry_count; ?>" role="button" data-slide="next">
                                                <span class="fa fa-chevron-right glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
                                                <span class="sr-only">Next</span>
                                              </a>
                                            </div>
                                            <!-- / carousel -->

                                        </div>
                                      </div>
                                    </div>

                                </div>

                            </div>
                            <!-- /entry -->



                            <?php endif; ?>
                        <?php endwhile; ?>
                    </div>
                <!-- / accordian -->
                <div class="terminator"></div>
            </div>
            <!-- / cambridge winter college  -->



            <!-- oxford summer 1 -->
            <div role="tabpanel" class="tab-pane" id="tab2">
                <!-- accordian -->
                    <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">

                        <?php $entry_count = 0; ?>
                        <?php while ( have_posts() ) : the_post(); ?> 
                            <?php if (in_category('oxford_summer_college_1')) : ?>

                                <?php $entry_count++; ?>


                                <!-- entry -->
                                <div class="entry">
                                    <div class="col-lg-3 date">
                                        <time class="cbp_tmtime"><span><?php the_field( 'day' ); ?></span><br> <span><?php the_field( 'date' ); ?><sup><?php the_field( 'suffix' ); ?></sup> <?php the_field( 'month' ); ?></span> </time>
                                        <div class="cbp_tmicon"></div>
                                    </div>

                                    <div class="panel-heading panel-head col-lg-9">
                                        <a data-toggle="collapse" data-parent="#accordion" href="#collapse-os1-<?php echo $entry_count; ?>">
                                          View <?php the_field( 'day' ); ?>'s journal entry
                                        </a>
                                    </div>

                                    <div id="collapse-os1-<?php echo $entry_count; ?>" class="panel-collapse collapse in col-lg-12 pull-right">
                                      <div class="panel-body col-lg-9 pull-right">
                                        <div class="row">
                                            <div class="col-lg-12 entry-text">

                                                <aside class="col-lg-6 pull-right flexbox-2">       
                                                    <div class="schedule" >
                                                        <?php $table = get_field( 'schedule' ); 
                                                             if ( $table ) { 
                                                                 echo '<table>'; 
                                                                     if ( $table['header'] ) { 
                                                                         echo '<thead><tr>'; 
                                                                             echo ''; 
                                                                                 foreach ( $table['header'] as $th ) { 
                                                                                     echo '<th>'; 
                                                                                         echo $th['c']; 
                                                                                     echo '</th>'; 
                                                                                 } 
                                                                             echo '</tr>'; 
                                                                         echo '</thead>'; 
                                                                     } 

                                                                     echo '<tbody>'; 
                                                                         foreach ( $table['body'] as $tr ) { 
                                                                             echo '<tr>'; 
                                                                                 foreach ( $tr as $td ) { 
                                                                                     echo '<td>'.$td['c'].'</td>'; 
                                                                                 } 
                                                                             echo '</tr>'; 
                                                                         } 
                                                                     echo '</tbody>'; 
                                                                 echo '</table>'; 
                                                             } ?>  
                                                    </div>
                                                    <div class="fun-fact" style="">
                                                        <?php the_field( 'fun_fact' ); ?>
                                                    </div>
                                                </aside>

                                                <div class="flexbox-1">
                                                    <?php the_field( 'entry_text' ); ?>
                                                </div>

                                            </div>                          
                                        </div>

                                        <div class="row carousel-wrapper">

                                            <div id="carousel-os1-<?php echo $entry_count; ?>" class="carousel slide" data-ride="carousel">                         

                                              <div class="carousel-inner" role="listbox">

                                                  <?php if ( have_rows( 'slider' ) ) : ?>
                                                    <?php $image_count = 0 ?>
                                                    <?php while ( have_rows( 'slider' ) ) : the_row(); ?>
                                                    <?php $image_count++; ?>    
                                                        <?php if ( get_sub_field( 'image') ) { ?>
                                                            <div class="item <?php if ($image_count == 1) echo 'active' ?>">
                                                              <img src="<?php the_sub_field( 'image' ); ?>">
                                                              <figcaption><?php the_sub_field( 'caption' ); ?></figcaption>
                                                            </div>
                                                        <?php } ?>
                                                    <?php endwhile; ?>
                                                <?php else : ?>
                                                    <?php // no rows found ?>
                                                <?php endif; ?>

                                              <a class="left carousel-control" href="#carousel-os1-<?php echo $entry_count; ?>" role="button" data-slide="prev">
                                                <span class="fa fa-chevron-left glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
                                                <span class="sr-only">Previous</span>
                                              </a>
                                              <a class="right carousel-control" href="#carousel-os1-<?php echo $entry_count; ?>" role="button" data-slide="next">
                                                <span class="fa fa-chevron-right glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
                                                <span class="sr-only">Next</span>
                                              </a>
                                            </div>
                                            <!-- / carousel -->

                                        </div>
                                      </div>
                                    </div>

                                <!-- </div> -->

                            </div>
                            <!-- /entry -->



                            <?php endif; ?>
                        <?php endwhile; ?>
                    </div>
                <!-- / accordian -->
                <div class="terminator"></div>
            </div>
            <!-- / oxford summer 1 -->


            <!-- oxford summer college 2 -->
            <div role="tabpanel" class="tab-pane" id="tab3">
                <!-- accordian -->
                    <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
                        <?php $entry_count = 0; ?>
                        <?php while ( have_posts() ) : the_post(); ?> 
                            <?php if (in_category('oxford_summer_college_2')) : ?>

                                <?php $entry_count++; ?>

                                <!-- entry -->
                                <div class="entry">
                                    <div class="col-lg-3 date">
                                        <time class="cbp_tmtime"><span><?php the_field( 'day' ); ?></span><br> <span><?php the_field( 'date' ); ?><sup><?php the_field( 'suffix' ); ?></sup> <?php the_field( 'month' ); ?></span> </time>
                                        <div class="cbp_tmicon"></div>
                                    </div>

                                    <div class="panel-heading panel-head col-lg-9">
                                        <a data-toggle="collapse" data-parent="#accordion" href="#collapse-os2-<?php echo $entry_count; ?>">
                                          View <?php the_field( 'day' ); ?>'s journal entry
                                        </a>
                                    </div>

                                    <div id="collapse-os2-<?php echo $entry_count; ?>" class="panel-collapse collapse in col-lg-12 pull-right">
                                      <div class="panel-body col-lg-9 pull-right">
                                        <div class="row">
                                            <div class="col-lg-12 entry-text">

                                                <aside class="col-lg-6 pull-right flexbox-2">       
                                                    <div class="schedule" >
                                                        <?php $table = get_field( 'schedule' ); 
                                                             if ( $table ) { 
                                                                 echo '<table>'; 
                                                                     if ( $table['header'] ) { 
                                                                         echo '<thead><tr>'; 
                                                                             echo ''; 
                                                                                 foreach ( $table['header'] as $th ) { 
                                                                                     echo '<th>'; 
                                                                                         echo $th['c']; 
                                                                                     echo '</th>'; 
                                                                                 } 
                                                                             echo '</tr>'; 
                                                                         echo '</thead>'; 
                                                                     } 

                                                                     echo '<tbody>'; 
                                                                         foreach ( $table['body'] as $tr ) { 
                                                                             echo '<tr>'; 
                                                                                 foreach ( $tr as $td ) { 
                                                                                     echo '<td>'.$td['c'].'</td>'; 
                                                                                 } 
                                                                             echo '</tr>'; 
                                                                         } 
                                                                     echo '</tbody>'; 
                                                                 echo '</table>'; 
                                                             } ?>  
                                                    </div>
                                                    <div class="fun-fact" style="">
                                                        <?php the_field( 'fun_fact' ); ?>
                                                    </div>
                                                </aside>

                                                <div class="flexbox-1">
                                                    <?php the_field( 'entry_text' ); ?>
                                                </div>

                                            </div>                          
                                        </div>

                                        <div class="row carousel-wrapper">

                                            <div id="carousel-os2-<?php echo $entry_count; ?>" class="carousel slide" data-ride="carousel">                         

                                              <div class="carousel-inner" role="listbox">

                                                  <?php if ( have_rows( 'slider' ) ) : ?>
                                                    <?php $image_count = 0 ?>
                                                    <?php while ( have_rows( 'slider' ) ) : the_row(); ?>
                                                    <?php $image_count++; ?>    
                                                        <?php if ( get_sub_field( 'image') ) { ?>
                                                            <div class="item <?php if ($image_count == 1) echo 'active' ?>">
                                                              <img src="<?php the_sub_field( 'image' ); ?>">
                                                              <figcaption><?php the_sub_field( 'caption' ); ?></figcaption>
                                                            </div>
                                                        <?php } ?>
                                                    <?php endwhile; ?>
                                                <?php else : ?>
                                                    <?php // no rows found ?>
                                                <?php endif; ?>

                                              <a class="left carousel-control" href="#carousel-os2-<?php echo $entry_count; ?>" role="button" data-slide="prev">
                                                <span class="fa fa-chevron-left glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
                                                <span class="sr-only">Previous</span>
                                              </a>
                                              <a class="right carousel-control" href="#carousel-os2-<?php echo $entry_count; ?>" role="button" data-slide="next">
                                                <span class="fa fa-chevron-right glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
                                                <span class="sr-only">Next</span>
                                              </a>
                                            </div>
                                            <!-- / carousel -->

                                        </div>
                                      </div>
                                    </div>

                                </div>

                            </div>
                            <!-- /entry -->



                            <?php endif; ?>
                        <?php endwhile; ?>
                    </div>
                <!-- / accordian -->
                <div class="terminator"></div> 
            </div>
            <!-- / oxford summer college 2 -->

            <!-- cambridge summer college  -->
            <div role="tabpanel" class="tab-pane" id="tab4">
                <!-- accordian -->
                    <div class="panel-group" id="accordion" role="tablist" aria-multiselectable="true">
                        <?php $entry_count = 0; ?>
                        <?php while ( have_posts() ) : the_post(); ?> 
                            <?php if (in_category('cambridge_summer_college')) : ?>

                                <?php $entry_count++; ?>

                                <!-- entry -->
                                <div class="entry">
                                    <div class="col-lg-3 date">
                                        <time class="cbp_tmtime"><span><?php the_field( 'day' ); ?></span><br> <span><?php the_field( 'date' ); ?><sup><?php the_field( 'suffix' ); ?></sup> <?php the_field( 'month' ); ?></span> </time>
                                        <div class="cbp_tmicon"></div>
                                    </div>

                                    <div class="panel-heading panel-head col-lg-9">
                                        <a data-toggle="collapse" data-parent="#accordion" href="#collapse-cs-<?php echo $entry_count; ?>">
                                          View <?php the_field( 'day' ); ?>'s journal entry
                                        </a>
                                    </div>

                                    <div id="collapse-cs-<?php echo $entry_count; ?>" class="panel-collapse collapse in col-lg-12 pull-right">
                                      <div class="panel-body col-lg-9 pull-right">
                                        <div class="row">
                                            <div class="col-lg-12 entry-text">

                                                <aside class="col-lg-6 pull-right flexbox-2">       
                                                    <div class="schedule" >
                                                        <?php $table = get_field( 'schedule' ); 
                                                             if ( $table ) { 
                                                                 echo '<table>'; 
                                                                     if ( $table['header'] ) { 
                                                                         echo '<thead><tr>'; 
                                                                             echo ''; 
                                                                                 foreach ( $table['header'] as $th ) { 
                                                                                     echo '<th>'; 
                                                                                         echo $th['c']; 
                                                                                     echo '</th>'; 
                                                                                 } 
                                                                             echo '</tr>'; 
                                                                         echo '</thead>'; 
                                                                     } 

                                                                     echo '<tbody>'; 
                                                                         foreach ( $table['body'] as $tr ) { 
                                                                             echo '<tr>'; 
                                                                                 foreach ( $tr as $td ) { 
                                                                                     echo '<td>'.$td['c'].'</td>'; 
                                                                                 } 
                                                                             echo '</tr>'; 
                                                                         } 
                                                                     echo '</tbody>'; 
                                                                 echo '</table>'; 
                                                             } ?>  
                                                    </div>
                                                    <div class="fun-fact" style="">
                                                        <?php the_field( 'fun_fact' ); ?>
                                                    </div>
                                                </aside>

                                                <div class="flexbox-1">
                                                    <?php the_field( 'entry_text' ); ?>
                                                </div>

                                            </div>                          
                                        </div>

                                        <div class="row carousel-wrapper">

                                            <div id="carousel-cs-<?php echo $entry_count; ?>" class="carousel slide" data-ride="carousel">                          

                                              <div class="carousel-inner" role="listbox">

                                                  <?php if ( have_rows( 'slider' ) ) : ?>
                                                    <?php $image_count = 0 ?>
                                                    <?php while ( have_rows( 'slider' ) ) : the_row(); ?>
                                                    <?php $image_count++; ?>    
                                                        <?php if ( get_sub_field( 'image') ) { ?>
                                                            <div class="item <?php if ($image_count == 1) echo 'active' ?>">
                                                              <img src="<?php the_sub_field( 'image' ); ?>">
                                                              <figcaption><?php the_sub_field( 'caption' ); ?></figcaption>
                                                            </div>
                                                        <?php } ?>
                                                    <?php endwhile; ?>
                                                <?php else : ?>
                                                    <?php // no rows found ?>
                                                <?php endif; ?>

                                              <a class="left carousel-control" href="#carousel-cs-<?php echo $entry_count; ?>" role="button" data-slide="prev">
                                                <span class="fa fa-chevron-left glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
                                                <span class="sr-only">Previous</span>
                                              </a>
                                              <a class="right carousel-control" href="#carousel-cs-<?php echo $entry_count; ?>" role="button" data-slide="next">
                                                <span class="fa fa-chevron-right glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
                                                <span class="sr-only">Next</span>
                                              </a>
                                            </div>
                                            <!-- / carousel -->

                                        </div>
                                      </div>
                                    </div>

                                </div>

                            </div>
                            <!-- /entry -->



                            <?php endif; ?>
                        <?php endwhile; ?>
                    </div>
                <!-- / accordian -->
                <div class="terminator"></div>
            </div>
            <!-- /cambridge summer college -->


    <?php endif; ?> 

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

Sorry for the long code dump but it is basically the same and copied 4 times. Really I cannot see what is wrong here. Any advice would be greatly appreciated.

Sorry for the long code dump but it is basically the same and copied 4 times. Really I cannot see what is wrong here. Any advice would be greatly appreciated.

推荐答案

Amazingly simple solution. I had the max posts set to 10. Really an obvious mistake that I have fallen foul to a few times. Simply changing the max posts did the trick but seeing as I want to have a blog aswell with pagination I added the following from css tricks to enable all posts from my CPT. Cpt is Journal.

Amazingly simple solution. I had the max posts set to 10. Really an obvious mistake that I have fallen foul to a few times. Simply changing the max posts did the trick but seeing as I want to have a blog aswell with pagination I added the following from css tricks to enable all posts from my CPT. Cpt is Journal.

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' );

?>

这篇关于类别过滤器中的发布循环不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆