如何在WordPress中用缩略图和内容在父帖子上显示子帖子

时间:2017-02-08 作者:Rahul Soni

我正试图用缩略图和内容在其家长的帖子中显示子帖子。我还为产品创建了一个自定义帖子,并在自定义模板产品中仅显示父帖子。php。现在我想显示父母的所有孩子。这是我的密码。

<?php
/**
 * Template Name: Product Page 
 *
 * Template for displaying a page just with the header and footer area and a "naked" content area in between.
 * Good for landingpages and other types of pages where you want to add a lot of custom markup.
 *
 * @package understrap
 */
get_header();
?>
<section class="inArea">
<div class="container">

    <div class="col-md-12 inconA wow fadeInUp" data-wow-duration="1s" data-wow-delay="0.3s" style="visibility: visible; animation-duration: 1s; animation-delay: 0.3s; animation-name: fadeInUp;">

        <h1 class="wow fadeInDown" data-wow-duration="1s" data-wow-delay="1s" style="visibility: visible; animation-duration: 1s; animation-delay: 1s; animation-name: fadeInDown;">our PRODUCTS</h1>  
        <div class="clear"></div>

        <div class="products wow fadeInUp" data-wow-duration="1s" data-wow-delay="0.6s" style="visibility: visible; animation-duration: 1s; animation-delay: 0.6s; animation-name: fadeInUp;">
            <div class="clear"></div>
            <?php
            $query = new WP_Query(array(\'post_type\' => \'products\', \'paged\' => $paged, \'post_parent\' => 0, \'posts_per_page\' => \'-1\'));

            if ($query->have_posts()) :
                ?>
                <?php while ($query->have_posts()) : $query->the_post(); ?> 
                    <div class="col-md-3 col-sm-6 wow fadeInRight" data-wow-duration="1s" data-wow-delay="0.9s" style="visibility: visible; animation-duration: 1s; animation-delay: 0.9s; animation-name: fadeInRight;">
                        <div class="products_cat">
                            <div class="img_cls"> 
                                <?php
                                if (has_post_thumbnail()) { // check if the post has a Post Thumbnail assigned to it.
                                    the_post_thumbnail(\'full\');
                                }
                                ?>
                            </div>
                            <div class="clearfix"></div>
                            <h2 class="title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></h2>
                        </div>
                    </div>
                <?php endwhile;
                wp_reset_postdata();
                ?>
                <!-- show pagination here -->
            </div>
        </div>
</section>
<?php else : ?>
<!-- show 404 error here -->
<?php endif; ?>


<?php
get_footer();

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

为了检查我是否理解您的问题,您想在每个父帖子下显示子帖子吗?

那样的话,你得再写一封WP_Query 在您的while 循环,父项作为循环中的当前post。

类似于:

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

?> 
                <div class="col-md-3 col-sm-6 wow fadeInRight">
                    <div class="products_cat">
                        <div class="img_cls"> 
                            <?php
                               if (has_post_thumbnail()) { 
                                   the_post_thumbnail(\'full\');
                               }?>
                        </div>
                        <div class="clearfix"></div>
                        <h2 class="title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></h2>
                    </div>
                </div>
            <?php
                             endwhile;
            wp_reset_postdata();
            ?>
编辑:

将单个产品归档。php和add

$child_query = new WP_Query(array(\'post_type\' => \'products\', \'post_parent\' => get_the_ID(), \'posts_per_page\' => \'-1\'));
                           while ($child_query ->have_posts()) : $child_query ->the_post();
?> 
                <div class="col-md-3 col-sm-6 wow fadeInRight">
                    <div class="products_cat">
                        <div class="img_cls"> 
                            <?php
                               if (has_post_thumbnail()) { 
                                   the_post_thumbnail(\'full\');
                               }?>
                        </div>
                        <div class="clearfix"></div>
                        <h2 class="title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></h2>
                    </div>
                </div>
            <?php
                           endwhile;
请检查语法错误并添加尚未指定的必要代码。很抱歉,经过这么多的编辑,我忘记了缩进。

相关推荐

Taxonomy filter all children

我有一个自定义分类过滤器,它将过滤选定分类中的所有页面。我希望代码能够选择该分类法中的页面,以及千页的子页面。这是密码。add_action(\'restrict_manage_posts\', \'restrict_manage_posts_section\'); function restrict_manage_posts_section() { global $post_type; if ( is_object_in_taxonomy( $post_t