繁体   English   中英

Wordpress-每页的帖子不适用于特定的类别-slug.php

[英]Wordpress - Posts per page not working for specific category-slug.php

我有一个特定的category-slug.php页面。

我想显示第一个类别“ banner-ads”,每页仅发布1个帖子,然后在其下方显示“特征”类别中的每页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();

您尚未重置: wp_reset_postdata();

有关类别的有用信息: http : //codex.wordpress.org/Class_Reference/WP_Query#Category_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()剩下的代码;

好吧,我终于明白了。 如果要强制其占用一定数量的帖子,请添加具有其功能的过滤器,然后在循环结束时将其删除。 我在第二个循环中意识到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');

这非常简单,只需在ar2_render_posts alter query_args ar2_render_posts

ar2_render_posts( null, array ( 
     'query_args' => array ( 'posts_per_page' => 1 ),
), true );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM