Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php WordPress:计数子页面_Php_Wordpress_Count - Fatal编程技术网

Php WordPress:计数子页面

Php WordPress:计数子页面,php,wordpress,count,Php,Wordpress,Count,在WordPress的一个页面上,我想显示该页面的所有子页面。目前的工作原理如下: <?php $args = array( 'post_type' => 'page', 'posts_per_page' => -1, 'post_parent' => $post->ID, 'order' => 'ASC', 'orderby' => 'menu_order' ); $parent = ne

在WordPress的一个页面上,我想显示该页面的所有子页面。目前的工作原理如下:

<?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(); ?>             
    <div id="parent-<?php the_ID(); ?>" class="parent-page">            
        <h1><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h1> 
    </div> 
<?php endwhile; ?> 


只需创建一个
$count
变量,并在循环中每次递增它

<?php $count = 1; ?>
<?php while ( $parent->have_posts() ) : $parent->the_post(); ?>             
    <div id="parent-<?php the_ID(); ?>" class="parent-page child<?php echo $count++; ?>">            
        <h1><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h1> 
    </div> 
<?php endwhile; ?> 


你需要什么帮助还不清楚。似乎您只需要修改循环中的标记?太好了,就是这样:)
<?php $count = 1; ?>
<?php while ( $parent->have_posts() ) : $parent->the_post(); ?>             
    <div id="parent-<?php the_ID(); ?>" class="parent-page child<?php echo $count++; ?>">            
        <h1><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h1> 
    </div> 
<?php endwhile; ?>