分类的存档页面上的第3页后分页不起作用

时间:2019-01-23 作者:James

我有一个自定义帖子类型“show”,在该自定义帖子类型中,我设置了一个分类/类别“venture”,它有两个选项“venture one”和“venture two”(slugs)。在我设置的两个归档页面上,每个页面都显示了“场馆一号”或“场馆二号”的所有帖子,页面分页在第3页之后不起作用。我使用的是数字分页,在视觉上它确实显示了正确的页数(给定我设置的每页的帖子数),但是如果你点击任何超过“3”的内容,我会得到404。

我尝试过但没有成功的方法:

重置permalinks直接通过wp dashboard更改每页的最大帖子数摆脱我的自定义查询并使用pre\\u get\\u帖子(注意:我所有的研究都指出这是解决我的问题的途径,所以我的假设是它可能会起作用,我只是在functions.php或其他地方对函数进行了错误编码)。If using pre_get_posts in functions.php is actually the solution, do I need to add anything new in my template files of the problematic archive pages other than the standard wp loop? (and of course getting rid of the custom query).重申一下,除了第3页上的分页显示404之外,其他一切都很正常。

最后要注意的是,分页在整个自定义帖子的归档页面上确实非常有效。该问题仅出现在“场馆一号”和“场馆二号”的档案页面上。

WP\\U查询

<?php
 $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1;  
 $today = current_time(\'Ymd\');

 $args = array (
         \'post_type\' => \'show\',
         \'posts_per_page\' => 3,
         \'paged\' => $paged,
         \'meta_key\'  => \'show_date\',
         \'order\' => \'ASC\',
         \'orderby\' => \'meta_value\',

     \'tax_query\' => array(
        array(
            \'taxonomy\' => \'venue\',
            \'field\' => \'slug\',
            \'terms\' => \'venue-one\'
        )
    ),       

            \'meta_query\' => array(
        array(
            \'key\'   => \'show_date\',
            \'compare\' => \'>=\',
            \'value\'   => $today
    ),
),
        );
$the_query = new WP_Query($args);
?> 
回路

<?php if ($the_query->have_posts() ): while ($the_query->have_posts() ) :$the_query->the_post(); $fields = (object) get_fields(); ?>
分页函数(在functions.php中)

function pagination_bar_venue( $custom_query ) {

$total_pages = $custom_query->max_num_pages;
$big = 999999999; // need an unlikely integer

if ($total_pages > 1){
    $current_page = max(1, get_query_var(\'paged\'));

    echo paginate_links(array(
        \'base\' => str_replace( $big, \'%#%\', esc_url( get_pagenum_link( $big ) ) ),
        \'format\' => \'?paged=%#%\',
        \'current\' => $current_page,
        \'total\' => $total_pages,
    ));
  }
}
在模板中调用分页

 <div class="pagination-shows"> 
    <?php pagination_bar_venue($the_query); ?>
 </div>
EDIT + SOME GOOD NEWS使用下面的pre\\u get\\u posts功能使一切正常,但。。。分页链接现在没有显示在帖子下面。要清楚的是,下面的代码已经起作用了,我现在可以看到第3页以外的帖子(直到任何页面上都有帖子为止,如果我键入url,所有这些都可以正常工作)。然而,一旦实现,物理单击页码的链接就消失了。如何将$paged变量传递到下面,以便它显示备份?还是有其他问题?

  // get taxonomy posts
function get_tax_posts( $query ) {
// Make sure this only fires when we want it too
  if( !is_admin() && $query->is_main_query() && $query->is_tax(\'venue\')) {

    // If so, modify the order variables
    $query->set(\'post_type\', \'show\' );
    $query->set(\'posts_per_page\', \'3\'  );
    $query->set( \'meta_key\', \'show_date\' );
    $query->set( \'order\', \'ASC\' );
    $query->set( \'orderby\', \'meta_value\' );
    $meta_query[] = array(
                array(
               \'key\' => \'show_date\',
               \'value\' => current_time(\'Ymd\'),
               \'compare\' => \'>=\',
                 ),
                 );
    $query->set(\'meta_query\',array( $meta_query ) );

       $taxquery = array(
        array(
             \'taxonomy\' => \'venue\',
             \'field\' => \'slug\',
             \'terms\' => \'venue-one\',
        )
    );
    $query->set( \'tax_query\', $taxquery );        
  }  
}
add_action(\'pre_get_posts\', \'get_tax_posts\', 9999);
现在分页已修复,但注意到另一个问题。至于分页,我替换了以下内容:

<?php pagination_bar_venue($the_query);?>
使用:

<?php the_posts_pagination();?>
现在分页也可以工作了。

我认为,正在发生的新问题是基于挂钩的优先级。如上所述,我有(2)个分类法来激活它,所以我在函数php中做了两个单独的函数,一个用于场馆一号(上面的代码),另一个用于场馆二号(当然有不同的函数名)。看来你不可能两者兼得。唯一能起作用的是挂钩优先级较高的那个。有解决方法吗?谢谢

编辑-我想问题是我的函数名太相似了,它们不精确,但除了几个字母外很接近。我把它们改成一点都不一样,现在一切似乎都正常了!

1 个回复
SO网友:Steffan Schlüter

我有一个类似的问题,当分页只在某些时候起作用时,这可能也很有用:例如/page/1/和/page/2/正在起作用和/page/3/不起作用。结果:错误404。问题是:在加载模板之前,总是从数据库加载posts\\u per\\u页面(WordPress设置/阅读)的默认值(每页12项)。因此,custom\\u post WP\\u查询中的$args=[\'posts\\u per\\u page\'=>;1]($args)对分页和加载元素的数量有影响,但对分页重写规则没有影响!建议的解决方案是向函数中添加pre\\u get\\u posts()。php和按自定义post\\u类型分隔。示例:

//Add to functions.php because it have to be loaded before template loading
function hwl_home_pagesize( $query ) {
    if ( get_post_type() == "your_custom_post_type" )
        $query->query_vars[\'posts_per_page\'] = 2; //same as in WP_Query arg or add to WP_Query arg get_option(\'posts_per_page\')
}
add_action(\'pre_get_posts\', \'hwl_home_pagesize\', 1);
另请参见:https://core.trac.wordpress.org/ticket/22299 (在我看来,这仍然是一个架构问题,不管9年前已经说过什么。)

相关推荐

change pagination url

目前,我的分页页面URL为:http://www.example.com/category_name/page/2但我需要将此URL结构更改为:http://www.example.com/category_name/?page=2等有什么解决方案吗?