dou8mwz5079 2018-09-24 18:48
浏览 208

分页仅显示第一页

The blog page for my wordpress site has the various pages on the bottom of the page, but will only display the first page of results despite which number page you click. A sample of my code is below, let me know if you need more.

if(!is_front_page())
     $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
else
     $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
     $args = array('offset'=> 0, 'paged'=>$paged, 'posts_per_page'=>$posts_per_page);
     $wp_query = new WP_Query($args);

if(have_posts()) : 
     while(have_posts()) : the_post();
  • 写回答

1条回答 默认 最新

  • douou6807 2018-09-25 13:23
    关注

    You are using the main loop to query your posts and not your custom query, plus it's not wise to use the variable name $wp_query it could be conflictual with the global var $wp_query, try changing:

    $wp_query = new WP_Query($args);
    
    if(have_posts()) : 
         while(have_posts()) : the_post();
    

    to

    $query = new WP_Query($args);
    
    if($query->have_posts()) : 
         while($query->have_posts()) : $query->the_post();
    

    Now the reason why the pagination is not working is also likely because you use the argument "offset" which breaks the pagination:

    offset (int) - number of post to displace or pass over. Warning: Setting the offset parameter overrides/ignores the paged parameter and breaks pagination (Click here for a workaround). The 'offset' parameter is ignored when 'posts_per_page'=>-1 (show all posts) is used. source: https://codex.wordpress.org/Class_Reference/WP_Query

    It is irrelevant tu use offset: 0 (default) in your situation.

    评论

报告相同问题?

悬赏问题

  • ¥15 Stata 面板数据模型选择
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用