我有一个特定的类别-段式page页面。
我想显示第一个类别的“旗帜-广告”,每页只有一个帖子,然后在此以下,显示每页3篇文章从类别“特色”。
我不想使用的: **query_posts( 'posts_per_page=4‘);**
我尝试过pre_get_posts函数,但似乎无法工作。
现在,每页显示的文章数量是我在设置->阅读中指定的数量
以下是我的当前代码:
$args1 = array(
'category_name' => 'banner-ads',
'posts_per_page' => 1
);
$the_query = new WP_Query( $args1 );
while ( $the_query->have_posts() ) :
$the_query->the_post();
ar2_render_posts( null, array ( 'type' => 'node' ), true );
endwhile;
wp_reset_postdata();
$args2 = array(
'category_name' => 'featured',
'posts_per_page' => 3
);
$query2 = new WP_Query( $args2 );
while( $query2->have_posts() ):
$query2->next_post();
ar2_render_posts( null, array ( 'type' => 'traditional' ), true );
endwhile;
wp_reset_postdata();发布于 2013-02-06 21:19:38
你还没有重置:wp_reset_postdata();
有用的类别:Parameters
下面是我的第一个页面循环:
<?php
$postq1 = new WP_Query(
array(
'post_type' => array('post','yourcustom'),
'posts_per_page' => 1,
'category_name'=>'banner-ads')
);
if($postq1->have_posts()):
while ( $postq1->have_posts() ) :
$postq1->the_post();?>
<article id="post-<?php the_ID();?>">....</article>
<?php
endwhile;
endif;
wp_reset_postdata();
?>第二圈:
<?php
$postq2 = new WP_Query(
array(
'post_type' => array('post','yourcustom'),
'posts_per_page' => 3,
'category_name'=>'featured')
);
if($postq2->have_posts()):
while ( $postq2->have_posts() ) :
$postq2->the_post();?>
<article id="post-<?php the_ID();?>">....</article>
<?php
endwhile;
endif;
wp_reset_postdata();
?>..。其余代码使用query_posts();
发布于 2013-02-07 21:30:58
好吧,所以我终于想出来了。如果您想要强制它接受一定数量的帖子,那么添加过滤器及其函数,然后在循环的末尾删除它。在我的第二个循环中,我意识到ar2_render_posts()函数与代码冲突,所以我选择从头重做这个函数,因为它基本上是一个布局函数。
add_filter('post_limits', 'my_post_limits');
function my_post_limits( $limit ) {
if ( in_category('banner-ads') ) {
return 'LIMIT 0, 3';
}
return $limit;
}
$args1 = array(
'category_name' => 'banner-ads'
);
$the_query = new WP_Query( $args1 );
while ( $the_query->have_posts() ) :
$the_query->the_post();
ar2_render_posts( null, array ( 'type' => 'node' ), true );
endwhile;
wp_reset_postdata();
remove_filter('post_limits', 'my_post_limits');发布于 2014-03-11 06:47:49
这很简单,只是alter query_args in ar2_render_posts
ar2_render_posts( null, array (
'query_args' => array ( 'posts_per_page' => 1 ),
), true );https://stackoverflow.com/questions/14738686
复制相似问题