创建if语句以按Wordpress中的cpt按类别过滤帖子 [英] Creating if statements to filter posts by category from cpt in wordpress

查看:89
本文介绍了创建if语句以按Wordpress中的cpt按类别过滤帖子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将cpt中的帖子放入不同的引导选项卡。

I am trying to put posts from a cpt into different bootstrap tabs.

到目前为止,我将所有帖子标题都显示在第一个选项卡上。然后,我在第二个选项卡上获得一个帖子标题,在以下两个选项卡上获得相同的标题。类别不是自定义分类法,而是与自定义帖子类型关联的默认wordpress类别。自定义帖子类型称为 journal,代码位于archive-journal.php

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. The custom post type is called 'journal' and the code is inside archive-journal.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?

谢谢

推荐答案

经过很多努力和思考,我找到了答案。我在wordpress阅读设置中将我的最高帖子设置为10 !! h!因此,通过更改该设置或更有效的方法,可以实现非常简单的修复,这是我在CSS技巧中找到的。它使用-1设置调用CPT的所有帖子。将日志更改为您的cpt名称。

After a lot of struggling and thinking I found the answer. I had my max posts set to 10 in wordpress reading settings!! Doh! So a really simple fix by changing that setting or better still this, which I found on css tricks. It calls all posts for the CPT with the -1 setting. Change journal to your cpt name.

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

?>

这篇关于创建if语句以按Wordpress中的cpt按类别过滤帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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