php,遍历每个子页面

时间:2018-07-30 12:16:15

标签: php wordpress

我对php完全陌生,所以如果这是一个愚蠢的问题,请原谅我。

我想要的是:获取当前页面ID(是父页面),查找该特定父页面的所有子页面 并输出每个子页面的永久链接和标题

以下代码有效,但它是针对类别而不是父页面的(我必须手动添加类别页面ID)

<div class="col-md-3">
    <div class="maddos-category-container">
        <div class="maddos-category-header">
            <h3 class="maddos-category-header-title">
                <a href="<?php echo get_category_link( "17" );?>">
                    <?php echo get_cat_name(17);?>
                </a>
            </h3>
        </div>

       <div class="maddos-category-wrapper">
           <ol>
               <?php
                   $args = array('category' => 17, 'post_type' =>  'post',  'numberposts' => -1); 
                   $postslist = get_posts($args);    

                   foreach ($postslist as $post) :  setup_postdata($post); 
               ?>  
                       <li>
                           <a href="<?php the_permalink(); ?>">
                               <?php the_title(); ?>
                           </a>
                       </li> 
                   <?php endforeach; ?>
               </div>
           </div>
       </div>
   </ol>

如果有人可以帮助我,我将不胜感激(我不太了解php,但我确实需要解决此问题)

编辑: 这就是我得到的(wordpress注释为thnx)(并且有效)

例如,排除页面8020和9021的最佳方法是什么?

<?php

$args = array(
    'post_type'      => 'page',
    'posts_per_page' => -1,
    'post_parent'    => $post->ID,
    'order'          => 'ASC',
    'orderby'        => 'menu_order'
 );


$parent = new WP_Query( $args );

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

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





<?php the_permalink(); ?> <?php the_title(); ?>



    <?php endwhile; ?>

<?php endif; wp_reset_postdata(); ?>

0 个答案:

没有答案