4

我在用着:

  • WordPress 3.4
  • WP-PageNavi 2.82

我注册了自定义分类(目录

<?php

add_action('init', 'pca_register_taxonomy', 0);

function pca_register_taxonomy()
{
    register_taxonomy('catalog', null,
       array(
            'label' => __('Catalogs', __),
            'labels' => array(
                'name' => __('Catalogs', __),
                'singular_name' => __('Catalog', __),
                'search_items' => __('Search Catalogs', __),
                'popular_items' => __('Popular Catalogs', __),
                'all_items' => __('All Catalogs', __),
                'parent_item' => __('Parent Catalog', __),
                'parent_item_colon' => __('Parent Catalog', __),
                'edit_item' => __('Edit Catalog', __),
                'update_item' => __('Update Catalog', __),
                'add_new_item' => __('Add New Catalog', __),
                'new_item_name' => __('New Catalog Name', __),
                'separate_items_with_commas' => __('Separate catalogs with commas', __),
                'add_or_remove_items' => __('Add or remove catalogs', __),
                'choose_from_most_used' => __('Choose from the most used catalogs', __),
                'menu_name' => __('Catalogs', __)
            ),
            'public' => true,
            'show_in_nav_menus' => true,
            'show_ui' => true,
            'show_tagcloud' => false,
            'hierarchical' => true,
            'query_var' => true,
            'rewrite' => array(
                'slug' => 'catalog',
                'with_front' => true,
                'hierarchical' => true
            ),
            'capabilities' => array(
                'manage_terms',
                'edit_terms',
                'delete_terms',
                'assign_terms'
            )
        )
    );
}

?>

我注册自定义帖子类型(产品

<?php

add_action('init', 'pca_register_post_type');

function pca_register_post_type()
{
    register_post_type('product',
        array(
            'label' => __('Products', __),
            'labels' => array(
                'name' => __('Products', __),
                'singular_name' => __('Product', __),
                'add_new' => __('Add New', __),
                'add_new_item' => __('Add New Product', __),
                'edit_item' => __('Edit Product', __),
                'new_item' => __('New Product', __),
                'all_items' => __('All Products', __),
                'view_item' => __('View Product', __),
                'search_items' => __('Search Products', __),
                'not_found' =>  __('No products found', __),
                'not_found_in_trash' => __('No products found in Trash', __), 
                'parent_item_colon' => '',
                'menu_name' => __('Products', __)
            ),
            'description' => '',
            'public' => true,
            'exclude_from_search' => false,
            'publicly_queryable' => true,
            'show_ui' => true,
            'show_in_nav_menus' => true,
            'show_in_menu' => true,
            'show_in_admin_bar' => true,
            'menu_position' => 20,
            'capability_type' => 'post',
            'meta_cap' => true,
            'hierarchical' => false,
            'supports' => array(
                'title',
                'editor',
                'thumbnail',
                'page-attributes',
                'post-formats'
            ),
            'taxonomies' => array('catalog'),
            'has_archive' => false,
            'rewrite' => array(
                'slug' => 'products',
                'with_front' => true,
                'feeds' => false,
                'pages' => true
            ),
            'query_var' => true,
            'can_export' => true
        )
    );
}

?>

然后我为税收创建一个新文件 - > taxonomy-catalog.php

在此文件中,我从指定目录(tax )中查询所有产品(自定义帖子类型):

<?php

$paged = get_query_var('paged');
$paged = ($paged) ? $paged : 1;

$products = new WP_Query(array(
    'catalog' => $catalog_data->slug, // $catalog_data is the current taxonomy (woman)
    'post_type' => 'product',
    'posts_per_page' => 12,
    'paged' => $paged
));

?>

<?php while ($products->have_posts()) : $products->the_post(); ?>
// Show title, content ... everything ok
<?php endwhile; ?>

<?php if (function_exists('wp_pagenavi')) wp_pagenavi(array('query' => $products)); ?>
<?php wp_reset_postdata(); ?>

分页显示正确,但是当我单击第 2 页或更多页面时,出现 404 错误。

  • 作品 -> mywebsite.com/catalog/woman
  • 不工作-> mywebsite.com/catalog/woman/page/2

我已经刷新了永久链接。

有什么办法解决这个问题吗?谢谢

4

6 回答 6

8

将其放入您的“functions.php”中,然后重新生成永久链接。它对我有用!

function taxonomy_rewrite_fix($wp_rewrite) {
    $r = array();
    foreach($wp_rewrite->rules as $k=>$v){
        $r[$k] = str_replace('catalog=$matches[1]&paged=','catalog=$matches[1]&page=',$v);
    }
    $wp_rewrite->rules = $r;
}
add_filter('generate_rewrite_rules', 'taxonomy_rewrite_fix');

关键是将“paged”替换为“page”到自定义分类的重写规则中。

这是我在这里的第一个贡献。希望我能帮助你。

于 2015-07-06T07:41:21.093 回答
1

Once I have faced problem about this and passed hard times hrs to hrs by pulling hair. I googled and didn't found any specific solution about the topics. I found several talents' article but they weren't satisfying my problems. Actually custom taxonomy archive page pagination is depends on some settings of arguments of related functions. So I am actually going here sharing my thoughts of solving the taxonomy archive pagination problem.

Five things you need for custom taxonomy archive page pagination working perfectly :

( 1 ) Don't put exclude_from_search parameter key as register_post_type argument parameter or if mention set it 'exclude_from_search' => false. By default it is set false if not mentioned.

( 2 ) The taxonomy that will be use with the custom post type set 'taxonomies' => 'custom_taxonomy_name' as register_post_type argument parameter or
use register_taxonomy_for_object_type() directly. Custom taxonomies still need to be registered with register_taxonomy().

( 3 ) While querying within new WP_Query ($args)

i ) If not set in admin `static front page` use before `new WP_Query($args)` 

$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
 and use $query = new WP_Query( array( 'paged' => $paged ) );

ii ) If set in admin static front page use before 'new WP_Query($args)'
  $paged = ( get_query_var('page') ) ? get_query_var('page') : 1;
  and use $query = new WP_Query( array( 'page' => $paged ) );

Remember to use posts_per_page and paged parameter in new WP_Query($arg) argument array. If not set static front page then you should use page parameter in new WP_Query ($arg) argument array

( 4 ) Use Wordpress paginate_links( $args ) function like the example below to render pagination in archive template file.

<?php $big = 999999999; // need an unlikely integer
echo paginate_links( array(
                  'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
                  'format' => '?paged=%#%',  or   '/paged=%#%',  // if using pretty permalink
                   'current' => max( 1, get_query_var('paged') ),
                   'total' => $query->max_num_pages ) ); // Here $max_num_pages is the properties of  new WP_Query() object . It is total number of pages. Is the result of $found_posts / $posts_per_page
 ?>

( 5 ) The paginate_links() function output the ul li listing with page-numbers class. If you use bootstrap inject pagination class to the ul with the help of javascript or jquery and a nice fancy pagination will be output.

Hope you can now enjoy pagination in the taxonomy archive template without any 404 problem :-)

于 2016-02-01T12:55:26.490 回答
1

帮助我的是在阅读设置中将“博客页面最多显示”设置为 1。然后它就可以工作了。

使用默认值 10,它会在第 2 页引发 404 错误。在第 1 页,一切都很完美。

因此,鉴于此工作,解决方案是仅针对您需要的那些分类法或类别将“博客页面最多显示”设置为 1。您应该在functions.php中放置的代码在这里:https ://forums.envato.com/t/wordpress-custom-page-type-taxonomy-pagination/76549/12

于 2017-09-23T18:27:25.610 回答
0

基本上就是这样

get_query_var('page') 

保存页码

从工作代码:

global $query_string;

$page_index = max(1, get_query_var('page'));

parse_str($query_string, $queryParams);
$queryParams['paged'] = $page_index;
$queryParams['posts_per_page'] = $posts_per_page;
query_posts($queryParams);
于 2012-06-19T17:34:10.047 回答
0

使用 query_posts 而不是 wp_query。它应该工作。

这是我的代码。

  <?php             

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

             $args = array(

            'paged' => $paged,

            'orderby' => 'asc',

                'tax_query' => array(

                    array(

                        'taxonomy' => "$term->taxonomy",

                        'terms' => "$term->name",

                        'field' => 'slug'  

                    )

                )

            );

            $query = new WP_Query( $args );
            if($query->have_posts() ) :   
// your code
?>  

<?php endwhile; endif; 
             wp_pagenavi();?>
于 2014-06-21T09:02:04.123 回答
0


当您创建任何 CPT 时,您需要添加此功能。

flush_rewrite_rules();
它将管理/调整/重新分配您的记忆。
因此,请添加此行,您的代码将起作用。
预防。
1: Add this function : flush_rewrite_rules();
2:Your pagination function (wp_pagenavi()) will call just below the endwhile;
我敢肯定,您的分页会起作用,如果不起作用,请告诉我。
感谢您。

于 2015-07-06T08:04:30.007 回答