Php 限制子页面的数量
我有这个html结构:Php 限制子页面的数量,php,wordpress,Php,Wordpress,我有这个html结构: <home> <Web> <web 1> <web 2> <web 3> <web 4> <web 5> <web 6> <web 7> </web> <Print> <Print 1> <Print 2> <Print 3> <
<home>
<Web>
<web 1>
<web 2>
<web 3>
<web 4>
<web 5>
<web 6>
<web 7>
</web>
<Print>
<Print 1>
<Print 2>
<Print 3>
<Print 4>
<Print 5>
</print>
<Art>
<Art 1>
<Art 2>
<Art 3>
<Art 4>
<Art 5>
<Art 6>
</art>
</home>
我使用它来显示孙子内容,同时隐藏其父内容
<?php $counter = 1 ?>
<div class="row-fluid">
<?php
if ( have_posts() ) {
while ( have_posts() ) {
the_post();
$args=array(
'orderby' => 'menu_order',
'order' => 'ASC',
'posts_per_page' => -1,
'post__not_in' => array(4,368,358,354),
'post_type' => 'page',
'post__in' => $pageIDs
);
$childpages = new WP_Query($args);
if($childpages->post_count > 0) { /* display the children content */
while ($childpages->have_posts()) {
$childpages->the_post(); ?>
<div class="span4">
<?php
echo "<h2>".get_the_title()."</h2>";
echo the_content();
?>
</div>
<? if ($counter % 3 == 0): ?>
</div>
<div class="row-fluid">
<?php endif; ?>
<?php $counter++; ?>
<?php }
}
wp_reset_query();
}
}
?>
</div>
在显示所有孙辈的那一刻,我如何将孙辈的数量限制为每种类型3个(印刷版3个,网络版3个,艺术品版3个)?首先,我会尽量不使用更多的查询,然后再进行必要的查询。假设您知道(或知道如何获取)父帖子的
$id
(作为整数),请使用:
试试#2:
请让我知道您从这些代码示例中得到了什么。'posts\u per\u page'=>'3',这有意义吗?还有,为什么您有两个查询?_post()和WP_查询?老实说,我不确定,我已经用了2天了,我尝试了很多东西,现在我相当困惑。你会怎么做才能让我显示3个孙子的标题和内容,以及它的父标题?“每页的帖子”=>“3”只会显示3个孙子,只有一个孩子,谢谢!代码的第一位只显示一个孩子的3个孙子,但我需要为每个3个孩子显示3个孙子是的,但是如果不是“post\u parent”=>id,而是像“post\u parent”=>array(1,2,3)那样插入它,其中的数字是子页面的id,那么一切都应该正常。另一种方法是自动获取所有孩子的第一个id。或者你试过第二个代码了吗?对不起,我在上面的评论中犯了错误。post_parent不能是数组,它是一个整数,正如我在回答中所说的:Pno luck:(在查询中添加了这个:'post_parent'=>array(368358354),我仍然只得到了354的3个孙子。我现在用第二位代码尝试您提供的其他代码,我得到了以下错误:致命错误:对成员函数的调用在
<?php
$args = array(
'post_type' => 'page',
'posts_per_page' => 3,
'post_parent' => $id,
'orderby' => 'menu_order',
'order' => 'ASC',
'post__not_in' => array(4,368,358,354),
);
$childpages = new WP_Query( $args );
if ( $childpages->have_posts() ) :
?>
<div class="row-fluid">
<?php
while ( $childpages->have_posts() ) :
$childpages->the_post();
?>
<div class="span4">
<h2>
<?php the_title(); ?>
</h2>
<?php the_content(); ?>
</div>
<?php
endwhile;
?>
</div>
<?php
endif;
wp_reset_query();
?>
<div class="row-fluid">
<?php
$args = array(
'child_of' => 4,
'parent' => 0,
'post_type' => 'page',
'post_status' => 'publish'
);
$childrens = query_posts('showposts=100&post_parent=4&post_type=page&orderby=menu_order&order=asc');
foreach ( $childrens as $children ) :
query_posts('showposts=3&post_parent='.$children->ID.'&post_type=page&orderby=menu_order&order=asc');
if ( have_posts ) :
while ( have_posts() ) : the_post();
?>
<div class="span4">
<h2>
<?php the_title(); ?>
</h2>
<?php the_content(); ?>
</div>
<?php
endwhile;
endif;
endforeach;
?>
</div>