将子帖显示到Wordpress页面模板中

时间:2016-09-12 07:43:01

标签: php wordpress

我有自定义类型帖子我们的培训师,其帖子有 XYZ MTJ 等,另有帖子 ABC < / strong>和 TKY 蚀刻, XYZ ABC TKY 的父级我想只显示 ABC TKY 发布

喜欢 -

XYZ

ABC(描述)。

ABC_(描述)。

MTJ

儿童职位1(说明)。

儿童职位2(说明)。

<?php
 /**
  * Template Name: Blog

  The template for displaying all pages.
  *
  * This is the template that displays all pages by default.
  * Please note that this is the WordPress construct of pages
  * and that other 'pages' on your WordPress site will use a
  * different template.
  *
  * @package Sydney
  */

     get_header(); 
 ?>

 <?php
              $array = array('post_type'=>'our-trainers',  'posts_per_page' => 30, 'order' => 'ASC');
              $array_query = new wp_query($array);
            while ($array_query->have_posts() ) : $array_query->the_post();
            ?>
                <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                <?php //the_excerpt(); ?>
        <?php
          $array = array('post_type'=>'our-trainers', 'post_parent' => $post->ID, 'posts_per_page' => 30, 'order' => 'ASC');
          $array_query = new wp_query($array);
        while ($array_query->have_posts() ) : $array_query->the_post();
        ?>
            <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
            <?php the_excerpt(); ?>


    <?php endwhile; // end of the loop. ?>

        <?php endwhile; // end of the loop. ?>

仅显示最后一位父母的帖子和子帖子。

1 个答案:

答案 0 :(得分:0)

你可以尝试更改你的查询。在这种情况下,添加查询密钥:post_parent__in。这应该将结果限制为具有特定父ID的所有our-trainers Post-Type。

    <?php
          $array = array(
              'post_type'       => 'our-trainers', 

              // ADD THE FOLLOWING LIKE: THE ARRAY COULD BE A LIST OF IDS OR JUST 1 ID.
              'post_parent__in' => array("ID_OF_THE_POST_PARENT"), 

              'posts_per_page'  => 30, 
              'order'           => 'ASC'
          );
          $array_query = new wp_query($array);
        while ($array_query->have_posts() ) : $array_query->the_post();
           // THE REST OF THE CODE...

OPTION NR 2

    <?php
          $parentIDs    = array(214, 412);
          $array        = array(
              'post_type'       => 'our-trainers', 
              'posts_per_page'  => 30, 
              'order'           => 'ASC'
          );
          $array_query  = new wp_query($array);
        while ($array_query->have_posts() ) : $array_query->the_post();
           $pID         = get_the_ID();
           // GET THE POST OBJECT;
           $pOBJ        = get_post($pID);
           // CHECK IF THE PARENT ID OF THE POST IS CONTAINED IN THE LIST OF 
           // PARENT_IDS...
           if(in_array($pOBJ->parent_id, $parentIDs)){ 
              // DISPLAY ONLY THESE POSTS...
           }
           // THE REST OF THE CODE...