类别的分页不起作用

时间:2019-07-08 作者:Manjulam Kumar

我正在尝试为类别项目列表归档实现分页。php页面。但这不起作用。下面是我正在使用的代码。

Custom Query:

$cat = single_cat_title("", false);
$cat_ID = get_cat_ID ($cat);

$args = array(
    \'posts_per_page\'    => \'2\',
    \'post_type\' => \'mathematics\',
    \'cat\' => $cat_ID,
    \'paged\' => get_query_var(\'paged\',1)
);
$posts = new WP_Query($args);
Pagination code:

echo paginate_links(array(
              \'base\'               => \'%_%\',
              \'format\'             => \'?paged=%#%\',
              \'total\' => $posts->max_num_pages,
              ));
加载页面时,它会显示分页链接,但当我单击next时,它会显示404错误。

主页链接:http://mk.local/category/mathematics/average/单击“下一步”时:http://mk.local/category/mathematics/average/?paged=2

我已经尝试过更新永久链接

6 个回复
最合适的回答,由SO网友:Manjulam Kumar 整理而成

我在函数中添加了以下代码。php和它现在运行良好。

function custom_query( $query ){

    if(is_category()){
        $query->set( \'post_type\', array( \'math\' ) );
        $query->set( \'posts_per_page\', \'2\' );
    }
}
add_action(\'pre_get_posts\', \'custom_query\');
谢谢大家的建议。

SO网友:moped

您应该使用pre_get_posts 获取正确数量的类别页面和链接的操作
解释了完整的解决方案here.

SO网友:Ahmed Faruq

删除所有分页并尝试类似的操作,其中mytheme是主题的名称

   <?php
$GLOBALS[\'wp_query\']->max_num_pages = $custom_query->max_num_pages;
the_posts_pagination(array(
    \'next_text\' => \'<span class="meta-nav" aria-hidden="true">\' . __(\'Next Post\', \'mytheme\') . \'</span> \' . \'<span class="screen-reader-text">\' . __(\'Next post:\', \'mytheme\') . \'</span> <br/>\' . \'<span class="post-title">%title</span>\',
    \'prev_text\' => \'<span class="meta-nav" aria-hidden="true">\' . __(\'Previous Post\', \'mytheme\') . \'</span> \' . \'<span class="screen-reader-text">\' . __(\'Previous post:\', \'mytheme\') . \'</span> <br/>\' . \'<span class="post-title">%title</span>\'
));
?>
告诉我进展如何

SO网友:Jason Is My Name

这家伙展示了如何创建AJAX分页。。。

https://artisansweb.net/load-wordpress-post-ajax/

非常容易遵循,效果显著。

SO网友:Manjulam Kumar
  1. $wp_query dump on page 1 (http://archive.local/category/mathematics/average)

    WP_Query Object ( [query] => Array ( [category_name] => mathematics/average ) [query_vars] => Array ( [category_name] => average [error] => [m] => [p] => 0 [post_parent] => [subpost] => [subpost_id] => [attachment] => [attachment_id] => 0 [name] => [static] => [pagename] => [page_id] => 0 [second] => [minute] => [hour] => [day] => 0 [monthnum] => 0 [year] => 0 [w] => 0 [tag] => [cat] => 6 [tag_id] => [author] => [author_name] => [feed] => [tb] => [paged] => 0 [meta_key] => [meta_value] => [preview] => [s] => [sentence] => [title] => [fields] => [menu_order] => [embed] => [category__in] => Array ( ) [category__not_in] => Array ( ) [category__and] => Array ( ) [post__in] => Array ( ) [post__not_in] => Array ( ) [post_name__in] => Array ( ) [tag__in] => Array ( ) [tag__not_in] => Array ( ) [tag__and] => Array ( ) [tag_slug__in] => Array ( ) [tag_slug__and] => Array ( ) [post_parent__in] => Array ( ) [post_parent__not_in] => Array ( ) [author__in] => Array ( ) [author__not_in] => Array ( ) [ignore_sticky_posts] => [suppress_filters] => [cache_results] => 1 [update_post_term_cache] => 1 [lazy_load_term_meta] => 1 [update_post_meta_cache] => 1 [post_type] => [posts_per_page] => 2 [nopaging] => [comments_per_page] => 50 [no_found_rows] => [order] => DESC ) [tax_query] => WP_Tax_Query Object ( [queries] => Array ( [0] => Array ( [taxonomy] => category [terms] => Array ( [0] => average ) [field] => slug [operator] => IN [include_children] => 1 ) ) [relation] => AND [table_aliases:protected] => Array ( [0] => wp_term_relationships ) [queried_terms] => Array ( [category] => Array ( [terms] => Array ( [0] => average ) [field] => slug ) ) [primary_table] => wp_posts [primary_id_column] => ID ) [meta_query] => WP_Meta_Query Object ( [queries] => Array ( ) [relation] => [meta_table] => [meta_id_column] => [primary_table] => [primary_id_column] => [table_aliases:protected] => Array ( ) [clauses:protected] => Array ( ) [has_or_relation:protected] => ) [date_query] => [queried_object] => WP_Term Object ( [term_id] => 6 [name] => Average [slug] => average [term_group] => 0 [term_taxonomy_id] => 6 [taxonomy] => category [description] => [parent] => 5 [count] => 6 [filter] => raw [cat_ID] => 6 [category_count] => 6 [category_description] => [cat_name] => Average [category_nicename] => average [category_parent] => 5 ) [queried_object_id] => 6 [request] => SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (6) ) AND wp_posts.post_type = \'post\' AND (wp_posts.post_status = \'publish\') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 0, 2 [posts] => Array ( ) [post_count] => 0 [current_post] => -1 [in_the_loop] => [comment_count] => 0 [current_comment] => -1 [found_posts] => 0 [max_num_pages] => 0 [max_num_comment_pages] => 0 [is_single] => [is_preview] => [is_page] => [is_archive] => 1 [is_date] => [is_year] => [is_month] => [is_day] => [is_time] => [is_author] => [is_category] => 1 [is_tag] => [is_tax] => [is_search] => [is_feed] => [is_comment_feed] => [is_trackback] => [is_home] => [is_privacy_policy] => [is_404] => [is_embed] => [is_paged] => [is_admin] => [is_attachment] => [is_singular] => [is_robots] => [is_posts_page] => [is_post_type_archive] => [query_vars_hash:WP_Query:private] => a661d92bb8a6ebd714003f1268022535 [query_vars_changed:WP_Query:private] => [thumbnails_cached] => [stopwords:WP_Query:private] => [compat_fields:WP_Query:private] => Array ( [0] => query_vars_hash [1] => query_vars_changed ) [compat_methods:WP_Query:private] => Array ( [0] => init_query_flags [1] => parse_tax_query ) )

  2. $wp_query dump on page 2 (http://archive.local/category/mathematics/average/page/2/)

    WP_Query Object ( [query] => Array ( [paged] => 2 [category_name] => mathematics/average ) [query_vars] => Array ( [paged] => 2 [category_name] => average [error] => [m] => [p] => 0 [post_parent] => [subpost] => [subpost_id] => [attachment] => [attachment_id] => 0 [name] => [static] => [pagename] => [page_id] => 0 [second] => [minute] => [hour] => [day] => 0 [monthnum] => 0 [year] => 0 [w] => 0 [tag] => [cat] => 6 [tag_id] => [author] => [author_name] => [feed] => [tb] => [meta_key] => [meta_value] => [preview] => [s] => [sentence] => [title] => [fields] => [menu_order] => [embed] => [category__in] => Array ( ) [category__not_in] => Array ( ) [category__and] => Array ( ) [post__in] => Array ( ) [post__not_in] => Array ( ) [post_name__in] => Array ( ) [tag__in] => Array ( ) [tag__not_in] => Array ( ) [tag__and] => Array ( ) [tag_slug__in] => Array ( ) [tag_slug__and] => Array ( ) [post_parent__in] => Array ( ) [post_parent__not_in] => Array ( ) [author__in] => Array ( ) [author__not_in] => Array ( ) [ignore_sticky_posts] => [suppress_filters] => [cache_results] => 1 [update_post_term_cache] => 1 [lazy_load_term_meta] => 1 [update_post_meta_cache] => 1 [post_type] => [posts_per_page] => 2 [nopaging] => [comments_per_page] => 50 [no_found_rows] => [order] => DESC ) [tax_query] => WP_Tax_Query Object ( [queries] => Array ( [0] => Array ( [taxonomy] => category [terms] => Array ( [0] => average ) [field] => slug [operator] => IN [include_children] => 1 ) ) [relation] => AND [table_aliases:protected] => Array ( [0] => wp_term_relationships ) [queried_terms] => Array ( [category] => Array ( [terms] => Array ( [0] => average ) [field] => slug ) ) [primary_table] => wp_posts [primary_id_column] => ID ) [meta_query] => WP_Meta_Query Object ( [queries] => Array ( ) [relation] => [meta_table] => [meta_id_column] => [primary_table] => [primary_id_column] => [table_aliases:protected] => Array ( ) [clauses:protected] => Array ( ) [has_or_relation:protected] => ) [date_query] => [request] => SELECT SQL_CALC_FOUND_ROWS wp_posts.ID FROM wp_posts LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE 1=1 AND ( wp_term_relationships.term_taxonomy_id IN (6) ) AND wp_posts.post_type = \'post\' AND (wp_posts.post_status = \'publish\') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date DESC LIMIT 2, 2 [posts] => Array ( ) [post_count] => 0 [current_post] => -1 [in_the_loop] => [comment_count] => 0 [current_comment] => -1 [found_posts] => 0 [max_num_pages] => 0 [max_num_comment_pages] => 0 [is_single] => [is_preview] => [is_page] => [is_archive] => [is_date] => [is_year] => [is_month] => [is_day] => [is_time] => [is_author] => [is_category] => [is_tag] => [is_tax] => [is_search] => [is_feed] => [is_comment_feed] => [is_trackback] => [is_home] => [is_privacy_policy] => [is_404] => 1 [is_embed] => [is_paged] => [is_admin] => [is_attachment] => [is_singular] => [is_robots] => [is_posts_page] => [is_post_type_archive] => [query_vars_hash:WP_Query:private] => 6ff57f0f1abfeb2f58bbec696009b6bf [query_vars_changed:WP_Query:private] => [thumbnails_cached] => [stopwords:WP_Query:private] => [compat_fields:WP_Query:private] => Array ( [0] => query_vars_hash [1] => query_vars_changed ) [compat_methods:WP_Query:private] => Array ( [0] => init_query_flags [1] => parse_tax_query ) )

I don\'t understand why post_type is \'post\'. I tried adding pre_get_posts filter like below.

function custom_query(){
    global $wp_query;
    $wp_query-> set(\'post_type\', \'maths\');
}
add_action(\'pre_get_posts\', \'custom_query\');

this changes the post_type to \'maths\'. However the issue still remains unresolved.

I have recently started Wordpress development and feel totally lost with this issue :(

Edit: I just added 5 \'posts\' under \'Average\' category and the pagination working. So, it seems that some how it is considering the max_num_pages from \'posts\' for pagination and not from my custom post.

SO网友:WebMat

在进行\\u posts\\u分页之前,需要将自定义查询的最大页设置为全局,如

$GLOBALS[\'wp_query\']->max_num_pages = $query->max_num_pages;