根据特定分类显示CPT帖子

时间:2019-12-11 作者:Paweł Skaba

所以我有一个自定义的帖子类型opencourses. 有一种自定义分类法,称为typ. 很明显,在内部,我有一个分为两大支柱(生产和服务)的多个术语。

不幸的是,我的当前代码显示了自定义帖子中的所有帖子。

我试图实现的是在专门针对生产的页面上显示生产条款/帖子,服务也是如此。

代码如下

            <div class="listaWarsztatow" id="listaWarsztatow">
                            <?php

                                $taxonomy = \'typ\';

                                $args = array(
                                    \'orderby\' => \'title\',
                                    \'order\' => \'ASC\',
                                    \'childless\' => true                         
                                );

                                $taxonomy_terms = get_terms($taxonomy, $args);
                                if($taxonomy_terms) {
                                    foreach($taxonomy_terms as $taxonomy_term) {
                                        $args = array(
                                            \'post_type\' => \'opencourses\',
                                            "$taxonomy" => $taxonomy_term->slug,
                                            \'post_status\' => \'publish\',
                                            \'posts_per_page\' => -1,

                                        );
                                        $query = new WP_Query( $args );

                                        if ( $query->have_posts() ) : ?>

                                        <?php $color = get_field(\'blok_szkoleniowy_kolor\', $taxonomy_term); ?>

                                        <div class="term_anchor" id="<?php echo $taxonomy_term->slug; ?>"></div>
                                        <h4 class="term_blok_szkoleniowy"><span class="spacer" style="background:<?php echo $color; ?>"></span><?php echo $taxonomy_term->name; ?></h4>

                                            <?php while ( $query->have_posts() ) : $query->the_post(); ?>

                                            <div class="row">
                                                <div class="col-md-12">
                                                    <div class="coursWrapper">

                                                        <div class="visibleInfoCourses" style="border-left: 5px solid <?php echo $color; ?>">
                                                                <div class="row">
                                                                    <div class="col-sm-7">
                                                                        <p><a href="<?php the_permalink() ?>"><?php the_title() ?></a>
                                                                        <?php if (get_field(\'nowosc\')) : ?><span class="labelItem labeImNew">Nowość</span><?php endif ?>
                                                                        <?php if (get_field(\'new_form\')) : ?><span class="labelItem labeImNewForm">Nowa formuła</span><?php endif ?>
                                                                        <?php if (get_field(\'new_date_info\')) : ?><span class="labelItem labeImNewDate">Zmiana terminu</span><?php endif ?>
                                                                        </p>
                                                                    </div>
                                                                    <div class="col-sm-5">
                                                                        <div class="buttonsHereRight">
                                                                            <button class="btnBorderGrey btn-lean-sm">Terminy warsztatów</button>
                                                                        </div>
                                                                    </div>
                                                                </div>                                                                                                      
                                                        </div>

                                                        <div class="InvisibleInfoCourses" style="border-left: 5px solid <?php echo $color; ?>">
                                                            <?php if (have_rows(\'terminy_warsztatow\')) : $i = 0; ?>
                                                                <div class="TerminySzkolen" style="padding: 0;">
                                                                    <?php while (have_rows(\'terminy_warsztatow\')) : the_row();
                                                                        $i++;
                                                                        $hotele = get_sub_field(\'polecane_hotele\');
                                                                        $zapisy = get_sub_field(\'stan_zapisow\'); ?>
                                                                        <div class="InvisibleInfoCoursesDetails" <?php if ($zapisy == \'Sprzedane\') : echo " style=\'opacity:.6;\'";endif; ?>>
                                                                            <div class="row">
                                                                                <div class="col-sm-10">
                                                                                    <span><img src="<?php the_sub_field(\'logotyp\') ?>" alt="<?php the_sub_field(\'lokalizacja\') ?>"></span>
                                                                                    <span><i style="opacity: .6;" class="glyphicon glyphicon-time"></i><?php the_sub_field(\'data\') ?></span>
                                                                                    <span><i style="opacity: .6;" class="glyphicon glyphicon-map-marker"></i><?php the_sub_field(\'lokalizacja\') ?></span>
                                                                                    <span><i style="opacity: .6;" class="glyphicon glyphicon glyphicon-list-alt"></i><?php echo $zapisy; ?></span>
                                                                                </div>
                                                                                <div class="col-sm-2">
                                                                                    <div class="buttonsHereRight">
                                                                                        <!-- <a class="btn-lean btn-lean-sm" target="_blank" download="Formularz zgłoszeniowy" href="<?php the_sub_field(\'przycisk\') ?>" onclick="gtag(\'event\', \'szkolenia\', {\'event_category\' : \'szkolenie-otwarte-lista-szkoleń\',\'event_label\' : \'<?php the_title() ?> \'});" <?php if ($zapisy == \'Sprzedane\') : echo " style=\'display:none\'";endif; ?>>Zapisz się</a> -->
                                                                                        <!-- <a class="btn-lean btn-lean-sm" href="<?php the_permalink(); ?>" onclick="gtag(\'event\', \'szkolenia\', {\'event_category\' : \'szkolenie-otwarte-lista-szkoleń\',\'event_label\' : \'<?php the_title() ?> \'});" <?php if ($zapisy == \'Sprzedane\') : echo " style=\'display:none\'"; endif; ?>>Zapisz się</a> -->
                                                                                        <a class="btn-lean btn-lean-sm" href="<?php the_permalink(); ?>" <?php if ($zapisy == \'Sprzedane\') : echo " style=\'display:none\'"; endif; ?>>Zapisz się</a>
                                                                                    </div>
                                                                                </div>
                                                                            </div>
                                                                        </div>
                                                                    <?php endwhile; ?>
                                                                </div>
                                                            <?php endif ?>
                                                        </div>
                                                    </div>
                                                </div>
                                            </div>

                                            <?php endwhile; ?>



                                        <?php wp_reset_postdata(); // so nothin\' weird happens to other loops
                                        endif;
                                    }
                                }

                            ?>
                            </div>
这里怎么了?

1 个回复
最合适的回答,由SO网友:zain_ali 整理而成

$args = array(
    \'tax_query\' => array(
        array(
            \'taxonomy\' => \'typ\',
            \'field\'    => \'slug\',
            \'terms\'    => \'production\',
        ),
    ),
    \'post_type\' => \'opencourses\',
    \'posts_per_page\' => -1,
    \'post_status\' => \'publish\',
);
$query = new WP_Query( $args );
请尝试以上代码。谢谢

相关推荐

Modals using loops and ACF

我试图制作一个页面,查询特定类别(“景点”)的每一篇帖子。我已经能够成功地获得帖子,我只需要让modals工作。我在我的循环中做了一个按钮,它的标题是循环所在的任何帖子。我希望这样,每当人们单击该按钮时,它就会打开一个模式,显示代码中ACF I列表中的所有字段。不过,我有一些问题。由于某种原因,我无法让javascript正常工作。现在都在页面模板文件中,但我已经尝试通过函数将脚本排队。php等。我的猜测是,我正在尝试制作文档。getElementsByClassName而不是documents。getE