如何在其中一个子页面中显示父页面的子页面(WordPress)

时间:2019-06-19 作者:Quentin Content

我正在寻找如何在子页面中显示父页面的子页面

我只能在父页面中显示所有子项,如下所示

<?php 
 $args = array(
    \'post_type\'      => \'page\', //write slug of post type
    \'posts_per_page\' => -1,
    \'post_parent\'    => $post->ID,
    \'order\'          => \'ASC\',
    \'orderby\'        => \'menu_order\'
 );

 $childrens = new WP_Query( $args );
 if ( $childrens->have_posts() ) : ?>
 <?php while ( $childrens->have_posts() ) : $childrens->the_post(); ?>

 <!-- child <?php the_ID(); ?> -->
 <div class="grid-item grid-50" style="padding: 20px;">

 <?php  if ( $post->post_content=="" ) { ?> 
 <!-- IF NO CONTENT -->

  <?php if ( has_post_thumbnail() ) { ?>
  <!-- THUMBNAIL SEUL SANS LIEN -->
  <?php echo get_the_post_thumbnail( $page->ID, \'full-size\' ); ?>
  <?php edit_post_link(\'(add content?)\', \'<p>\', \'</p>\', null, \'editaddclass\'); ?> 

    <?php } else { ?>
    <!-- BLOC VIDE -->
    <div class="" style="padding: 20px; text-align: center; border: 1px dotted grey;">
    CE BLOC EST VIDE<br>
    <?php edit_post_link(\'(le supprimer / add content)\', \'<p>\', \'</p>\', null, \'editaddclass\'); ?>
    </div>
    <?php } ?> 

    <!-- OR WHIT CONTENT -->       
    <?php } else { ?>

      <!-- SI LE CONTENU -->
      <div class="">

        <?php if ( has_post_thumbnail() ) { ?>
        <!-- THUMBNAIL AVEC CONTENT & LINK -->

        <a href="<?php the_permalink(); ?>">
        <div><?php echo get_the_post_thumbnail( $page->ID, \'principal\' ); ?></div>
        <div style="width: 100%; text-align: center; padding:10px"><?php the_title(); ?></div>
        <?php edit_post_link(\'(modifier)\', \'<p>\', \'</p>\', null, \'editaddclass\'); ?>
        </a>

          <?php } else { ?>
          <!-- IL Y A DU CONTENU MAIS PAS DE THUBNAIL -->
          <div class="" style="padding: 70px 20px; text-align: center; background-color: #eee">
          THUMBNAIL MANQUANTE<br>
          </div>
          <div style="width: 100%; text-align: center; padding:10px">
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
          </div>
          <?php edit_post_link(\'(ajouter en une / modifier)\', \'<p>\', \'</p>\', null, \'editaddclass\'); ?>

        <?php } ?>

      </div>
      <!-- /if le content -->

 <?php }; ?>

 </div>
 <!-- /child <?php the_ID(); ?> -->
 <?php endwhile; endif; wp_reset_query(); ?>

</div>
</div>
<!-- /CHILDREN --> 
找不到如何在每个孩子的末尾显示它

你听懂了吗?:)

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

如果要在子页面中显示父页面的所有子页面,则可以尝试此操作。它将显示子页面标题及其链接。

$current_page_id = get_the_id();
    $parent_id = wp_get_post_parent_id( $current_page_id );
    if(!empty($parent_id))
    {
        $child_args = array(
            \'post_type\'      => \'page\',
            \'posts_per_page\' => -1,
            \'post_parent\'    => $parent_id,
            \'post_status\'    => \'publish\',
            \'order\'          => \'ASC\',
            \'orderby\'        => \'menu_order\'
             );
        $parent = new WP_Query( $child_args );

        if ( $parent->have_posts() ) : 
        echo \'<div class="single-sidebar">\';
        echo \'<ul>\';
             while ( $parent->have_posts() ) : $parent->the_post();
                $addclass  = "";
                if($current_page_id == get_the_ID())
                {
                    $addclass  = "active";
                }
              ?>                        
                <li class="<?php echo $addclass; ?>"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
                <?php 
            endwhile; 

            echo \'</ul>\';
            echo \'</div>\';
         endif; 
            wp_reset_postdata(); 
    }
如果这对你有帮助,请告诉我

相关推荐

具有Get_Pages的多个元密钥

我目前有以下代码,其中我按页面模板过滤;$pages = get_pages( array( ‘post_type’ => ‘page’, ‘meta_key’ => ‘_wp_page_template’, ‘meta_value’ => ‘unit.php’, ‘hierarchical’ => 0 ) ); 我还想按ACF字段排序,因此需要添加以下内容:;‘meta_key’ => ‘status’, ‘ord