【问题标题】:Wordpress ignoring post_per_pageWordpress 忽略posts_per_page
【发布时间】:2016-07-19 20:02:01
【问题描述】:

看起来很简单,但我似乎无法理解发生了什么。

我需要将帖子数限制为 25,但我的 wp_query 总是返回所有记录,并忽略 posts_per_page 参数。

这是我的代码:

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
    'posts_per_page' => 25,
    'paged' => $paged,
    'post_type' => 'noo_company',
    'industry' => $industry,
);

$the_query = new WP_Query( $args );
$posts = $the_query->posts;

这是我执行print_r($the_query) 时输出的第一部分。

WP_Query 对象 ( [query] => 数组 ( [posts_per_page] => 25 [paged] => 1 [post_type] => noo_company [industry] => 咨询 ) [query_vars] => 数组 ( [posts_per_page] => -1 [paged] => 1 [post_type] => noo_company [industry] => 咨询 ...

起初[query] 中的posts_per_page 的值在我设置时是可以的,但在第二部分[query_vars] 它被重置为'-1',我不知道为什么会发生这种情况。

在此先感谢您提供任何有用的建议。

【问题讨论】:

  • 在查询前尝试wp_reset_query();
  • 已经做了,没用
  • 它不能在没有任何插件的情况下在您自己的主题上工作,还是在有插件的其他人的主题上工作?
  • 是别人的主题
  • 尝试在默认主题之一中使用它。插件或主题本身可能会重写查询。

标签: php wordpress


【解决方案1】:

此代码对我有用,希望对您有用,只需复制并粘贴代码并安装一个插件名称为“wp_pagenavi”以进行页面分页。

if ( have_posts() ) : 
`enter code here`$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
  $query_args = array(
    'post_type' => 'noo_company',
    'posts_per_page' => 5,
    'paged' => $paged
  );
// create a new instance of WP_Query
$the_query = new WP_Query( $query_args );
if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); // run the loop 
the_title();
endwhile;
if ($the_query->max_num_pages > 1) { 
if(function_exists('wp_pagenavi')) { wp_pagenavi();} 
} 
else: 
_e('Sorry, no posts matched your criteria.'); 
endif;  
endif;

【讨论】:

    【解决方案2】:

    我删除了 post_type 参数,现在一切正常,它可能是特定于主题的,我不知道究竟是什么导致了这个问题,但无论如何它现在已经修复了。

    【讨论】:

      猜你喜欢
      • 2015-03-24
      • 1970-01-01
      • 1970-01-01
      • 2013-10-19
      • 2021-09-03
      • 1970-01-01
      • 1970-01-01
      • 2023-01-10
      • 2019-06-03
      相关资源
      最近更新 更多