简体   繁体   English

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

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

I have a specific category-slug.php page. 我有一个特定的category-slug.php页面。

I want to display the first category "banner-ads" with only 1 post per page and then below that, display 3 posts per page from the category "featured." 我想显示第一个类别“ banner-ads”,每页仅发布1个帖子,然后在其下方显示“特征”类别中的每页3个帖子。

I don't want to use: **query_posts( 'posts_per_page=4' );** 我不想使用: ** query_posts('posts_per_page = 4'); **

I've tried the pre_get_posts function but can't seem to get it working. 我已经尝试过pre_get_posts函数,但似乎无法正常工作。

Right now the number of posts per page that's displaying is the number I assigned in Settings->Reading 现在,每页显示的帖子数是我在“设置”->“阅读”中分配的数量

Here's my current code: 这是我当前的代码:

$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();

You haven't made reset: wp_reset_postdata(); 您尚未重置: wp_reset_postdata();

Useful about categories: http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters 有关类别的有用信息: http : //codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters

Here is mine for one of pages First loop: 这是我的第一个循环页面之一:

<?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();
?>

Second loop: 第二循环:

<?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();
?>

... rest of code with query_posts() ; ...使用query_posts()剩下的代码;

Okay so I finally figured it out. 好吧,我终于明白了。 If you want to force it to take certain number of posts then add the filter with its function then at the end of the loop remove it. 如果要强制其占用一定数量的帖子,请添加具有其功能的过滤器,然后在循环结束时将其删除。 I realized for my second loop that the ar2_render_posts() function was conflicting with the code so I'm opting to redoing that function from scratch since its basically a layout function. 我在第二个循环中意识到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');

it is pretty simple, just alter query_args in ar2_render_posts 这非常简单,只需在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