限制子页面的数量
[英] Limit the number of child pages
本文介绍了限制子页面的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个 html 结构:
<网页><网络1><网络2><网络3><网络4><网络5><网络6><网络7><打印><打印1><打印2><打印3><打印4><打印5></打印><艺术><第一条><第2条><第3条><第四条><第5条><第六条></艺术></home>
我用它来显示孙子内容,同时隐藏其父内容
<div class="row-fluid"><?php如果 ( have_posts() ) {而 ( have_posts() ) {the_post();$args=数组('orderby' =>'menu_order','订单' =>'ASC','posts_per_page' =>-1,'post__not_in' =>数组(4,368,358,354),'post_type' =>'页','post__in' =>$pageIDs);$childpages = new WP_Query($args);if($childpages->post_count > 0) {/* 显示子内容 */而 ($childpages->have_posts()) {$childpages->the_post();?><div class="span4"><?phpecho "<h2>".get_the_title()."</h2>";回声 the_content();?>
<?if ($counter % 3 == 0): ?>
<div class="row-fluid"><?php endif;?><?php $counter++;?><?php }}wp_reset_query();}}?>
目前显示所有孙子,我如何将孙子的数量限制为每种类型 3 个(印刷 3 个,网页 3 个,艺术 3 个)?
解决方案
首先,我会尽量不使用比需要更多的查询.假设您知道(或知道如何获取)父帖子的 $id
(作为整数),请使用 post_parent 参数:
'页','posts_per_page' =>3、'post_parent' =>$id,'orderby' =>'menu_order','订单' =>'ASC','post__not_in' =>数组(4,368,358,354),);$childpages = new WP_Query( $args);如果( $childpages->have_posts() ):?><div class="row-fluid"><?php而( $childpages->have_posts() ):$childpages->the_post();?><div class="span4"><h2><?php the_title();?><?php the_content();?>
<?php终了;?>
<?php万一;wp_reset_query();?>
尝试 #2:
<?php$args = 数组('child_of' =>4、'父母' =>0,'post_type' =>'页','post_status' =>'发布');$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');如果( have_posts ):while ( have_posts() ) : the_post();?><div class="span4"><h2><?php the_title();?><?php the_content();?>
<?php终了;万一;Endforeach;?>
请让我知道您从这些代码示例中得到了什么.
I have this html structure:
<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>
I use this to display the grandchildren content while hiding its parent
<?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>
At the moment all grandchildren are displayed, how do I limit the number of grandchildren to be 3 per type (3 for the print, 3 for the web and 3 for the art)?
解决方案
First I would try to not use more queries then required. Assuming you know (or know how to get) the $id
(as an integer) of the parent post, use the post_parent parameter:
<?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();
?>
Try #2:
<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>
Let me know please what you got with this code examples.
这篇关于限制子页面的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文