分页不适用于自定义分类模板

时间:2016-11-02 作者:Bhuvnesh Gupta

我创建了自定义分类法,并使用以下代码将其注册到“页面”

//hook into the init action and call create_book_taxonomies when it fires
add_action( \'init\', \'create_Offer_Categorys_hierarchical_taxonomy\', 0 );

//create a custom taxonomy name it Offer Categorys for your posts

function create_Offer_Categorys_hierarchical_taxonomy() {

// Add new taxonomy, make it hierarchical like categories
//first do the translations part for GUI

  $labels = array(
    \'name\' => _x( \'Offer Category\', \'offer Categories general name\' ),
    \'singular_name\' => _x( \'Offer Category\', \'taxonomy singular name\' ),
    \'search_items\' =>  __( \'Search Offer Category\' ),
    \'all_items\' => __( \'All Offer Categorys\' ),
    \'parent_item\' => __( \'Parent Offer Category\' ),
    \'parent_item_colon\' => __( \'Parent Offer Category:\' ),
    \'edit_item\' => __( \'Edit Offer Category\' ), 
    \'update_item\' => __( \'Update Offer Category\' ),
    \'add_new_item\' => __( \'Add New Offer Category\' ),
    \'new_item_name\' => __( \'New Offer Category Name\' ),
    \'menu_name\' => __( \'Offer Category\' ),
  );    

// Now register the taxonomy

  register_taxonomy(\'offer-category\',array(\'page\'), array(
    \'hierarchical\' => true,
    \'labels\' => $labels,
    \'show_ui\' => true,
    \'show_admin_column\' => true,
    \'query_var\' => true,
    \'exclude_from_search\' => false,
    \'rewrite\' => array( \'slug\' => \'offer-category\' ),
  ));

}
然后我创建了一个名为taxonomy-offer-category.php

问题是分页无法正常工作。编写分页代码后,分页url如下所示\'/offer-category/automobile/page/2\', 等

但是这个页面给出了错误(404),但是当我手动点击url时

/offer-category/automobile?page=2 然后给出结果。我不知道是什么问题。以下是分页代码

$term_id=get_queried_object()->term_id;
$paged = (get_query_var(\'page\')) ? get_query_var(\'page\') : 1;
// echo $paged; 
$args = array(
    \'post_type\' => \'page\',
        \'paged\'    => $paged,
        \'posts_per_page\' => \'1\',        
    \'tax_query\' => array(       
        array(  \'taxonomy\' => \'offer-category\',
            \'field\'    => \'id\',
            \'terms\'    => $term_id

                    )   
    ),
);
wp_reset_query();
//$page_args=array(\'post_type\' => \'page\');
$query = new WP_Query($args);




$big = 999999999; // need an unlikely integer
                $links = paginate_links( array(

                    \'current\' => max( 1, get_query_var(\'page\') ),
                    \'total\' => $query->max_num_pages,
                    \'prev_text\'    => \'<\',
                    \'next_text\'    => \'>\',
                    \'type\' => \'array\'
                ) );
我已经尝试了谷歌所有可能的答案,比如使用格式,使用页面代替页面等,这个问题是很久以前的问题了,我不知道它是否已经修复。你们能帮我一下吗。

谢谢

1 个回复
SO网友:CodeMascot

如果您的站点位于域的根(类似http://www.example.com), 查找位置/ 在配置文件中阻塞,并向其中添加以下行。

try_files $uri $uri/ /index.php?$args;
在这里,Nginx检查URL中是否存在文件($uri), 然后是目录($uri/). 如果找不到目录或文件,则执行内部重定向到/index.php 将查询字符串参数作为参数传递。

编辑后应如下所示:

location / {
    index index.php index.html index.htm;
    try_files $uri $uri/ /index.php?$args;
}
如果您的站点位于子文件夹中(例如/site), 您必须添加一个额外的位置/site/ 阻止到配置文件:

location /site/ {
    try_files $uri $uri/ /site/index.php?$args;
}
在配置文件中完成更改后,通过以下方式重新加载nginx配置:

nginx -s reload
Wordpress的永久链接现在应该可以正常工作了。

我找到了这个答案here下面是一些其他有用的例子-

  1. https://codex.wordpress.org/Nginx
  2. http://www.lowendguide.com/3/webservers/wordpress-permalinks-with-nginx/
  3. https://www.pmg.com/blog/a-mostly-complete-guide-to-the-wordpress-rewrite-api/

相关推荐

Why my pagination gets 404?

我试图理解为什么我的分页从第二页开始,每页都会出现404错误。现在在我设置的阅读设置中Blog pages show at most 至1。<?php $paged = (get_query_var(\'paged\')) ? get_query_var(\'paged\') : 1; $query = new WP_Query( array( \'post_type\' => \'behold_t