自定义 wp_query 上的 Wordpress 分页(next_posts_link)未显示 [英] Wordpress pagination (next_posts_link) on custom wp_query not showing

查看:37
本文介绍了自定义 wp_query 上的 Wordpress 分页(next_posts_link)未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人问过类似的问题,但我不知道我错过了什么!

Similar questions have been asked, but I cannot figure out what I miss!

我有一个用于自定义类型字段的静态页面,类似于常规存档或类别页面,但我无法进行分页.
如果我手动转到第 2 页(即添加到链接.../page/2),我会收到较新的帖子"链接,但在第一页上没有对于年长的人!next_posts_link() 似乎不存在(没有注入 div 或任何东西)

I have a static page for custom type fields, similar to the regular archive or category page, but I cannot get pagination working.
If I go manually to page 2 (i.e. adding to the link .../page/2) I get the "Newer posts" link, but not on the first page for older ones! next_posts_link() just seems not to exist (no div injected or anything)

这是我的代码:

  $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

  $query_args = array (
        'post_type' => 'bb_articoli',
        'meta_key' => 'bb_data-pubblicazione',
        'orderby' => 'meta_value_num',
        'order' => 'DESC',
        'posts_per_page' => 2,      //for testing purposes
        'paged' => $paged,
        'meta_query' => array(
            array('key' => 'bb_fonte-pubblicazione',
                  'value' => 2,
                  'compare' => '='
                  )
        )
  );

  $query = new WP_Query($query_args);

    if ( $query->have_posts() ) :
       while ( $query->have_posts()) :
             $query->the_post();
             get_template_part( 'content' , get_post_format());
       endwhile;

     next_posts_link();
     previous_posts_link();

     else :
         get_template_part( 'content', 'none' );
 endif;

非常感谢任何帮助.谢谢

Any help is greatly appreciated. Thanks
B

仅供参考:在二十二点使用儿童主题

Just for info: using child theme on twenty twelve

推荐答案

您的解决方案之所以有效是因为您覆盖了全局 $wp_query 变量.更好的解决方案是将 $query->max_num_pages 添加到 next_posts_link().

The reason why your solution works is because you are overwriting the global $wp_query variable. A better solution would be adding $query->max_num_pages to next_posts_link().

next_posts_link('« Older Entries', $query->max_num_pages)

其中 $query 是您新创建的对象的名称.这样你就可以保留 $wp_query.

Where $query is the name of your newly created object. This way you preserve $wp_query.

这篇关于自定义 wp_query 上的 Wordpress 分页(next_posts_link)未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆