我怎么能只展示猫头鹰旋转木马里的父母呢?

时间:2021-04-23 作者:saeed

我有这个代码,它显示了我帖子类型中的所有帖子(家长和孩子)。php是:

<div class="owl-carousel owl-theme">
            <?php
            $c=0;
            $q2=new WP_Query(array(\'post_type\'=> array( \'post\', \'book\' ),\'post_status\'=> \'publish\',\'orderby\'=>\'modified\',\'order\'=>\'desc\'));
            while($q2->have_posts()) {
                $c++;
                $q2->the_post();
                ?>
                <li class="item">
                    <a href="<?php the_permalink(); ?>">
                        <?php the_post_thumbnail(); ?>
                        <div class="metaitem">
                            <ul>
                                <li><?php the_title(); ?></li>
                                <li><?php the_excerpt(); ?></li>
                            </ul>
                        </div>
                    </a>
                </li>
                <?php
            }
            wp_reset_postdata();
            ?>
        </div>
现在,我可以使用什么数组只显示父对象而不显示子对象?我用过\'parent\' => 0 但它不起作用!

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

根据documentation, 你必须使用post_parent => 0

因此,您的代码如下所示:

<div class="owl-carousel owl-theme">
    <?php
    $c  = 0;
    $q2 = new WP_Query( array( 
          \'post_type\'   => array( \'post\', \'book\' ),
          \'post_status\' => \'publish\',
          \'post_parent\' => 0, //add this here
          \'orderby\'     => \'modified\',
          \'order\'       => \'desc\'
    ) );
    
    while ( $q2->have_posts() ) : $q2->the_post();
        $c ++;
        ?>
        <li class="item">
            <a href="<?php the_permalink(); ?>">
                <?php the_post_thumbnail(); ?>
                <div class="metaitem">
                    <ul>
                        <li><?php the_title(); ?></li>
                        <li><?php the_excerpt(); ?></li>
                    </ul>
                </div>
            </a>
        </li>
        <?php
    endwhile;
    wp_reset_postdata();
    ?>
</div>