php foreach 循环次数如何设置?
wordpress主题首页限制循环次数我的部分代码<divclass="ht-portfolio-post-wrap"><divclass="ht-portfolio-p...
wordpress 主题 首页限制循环次数我的部分代码 <div class="ht-portfolio-post-wrap"> <div class="ht-portfolio-posts ht-clearfix"> <?php if($total_portfolio_cat){ $count = 0; $args = array( 'cat' => $total_portfolio_cat, 'posts_per_page' => -1 ); $query = new WP_Query($args); if($query->have_posts()): while($query->have_posts()) : $query->the_post(); $categories = get_the_category(); $category_slug = ""; $cat_slug = array(); foreach ($categories as $category) { $cat_slug[] = 'total-portfolio-'.$category->term_id; } $category_slug = implode(" ", $cat_slug); if(has_post_thumbnail()){ $image_url = get_template_directory_uri().'/images/portfolio-small-blank.png'; $total_image = wp_get_attachment_image_src(get_post_thumbnail_id(),'total-portfolio-thumb'); $total_image_large = wp_get_attachment_image_src(get_post_thumbnail_id(),'large'); }else{ $image_url = get_template_directory_uri().'/images/portfolio-small.png'; $total_image = ""; } ?>
展开
1个回答
展开全部
//如限制5条记录;
//没有数字索引时
$i=1;
foreach($list as $val){
if($i>=5) break;
//do
$i++;
}
//有默认索引时
foreach($list as $key=>$val){
if($key>=5) break;
//do
}
更多追问追答
追问
能不能在我给出的源码上 直接修改 技术小白
追答
wordpress 可以用自带的查询功能
$query = new WP_Query($args);
$query->set( 'posts_per_page', '5' );//把这行插里面
if ($query->have_posts()): while ($query->have_posts()) : $query->the_post();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询