Wordpress - 自定义分类分页 [英] Wordpress - Custom Taxonomy Pagination
问题描述
我正在使用:
- WordPress 3.4
- WP-PageNavi 2.82
我注册自定义分类法(目录)
I register custom taxonomy (catalog)
<?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'
)
)
);
}
?>
我注册自定义帖子类型(产品)
I register custom post type (product)
<?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
)
);
}
?>
然后我为tax创建一个新文件-> taxonomy-catalog.php
Then I create a new file for tax -> taxonomy-catalog.php
在此文件中,我查询指定目录(tax)中的所有产品(自定义帖子类型):
In this file, I query all products (custom post type) from specified catalog (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 错误.
Pagination is displayed correctly but when I click on page 2 or over I have a 404 error.
- 作品 -> mywebsite.com/catalog/woman
- 无效 -> mywebsite.com/catalog/woman/page/2
我已经刷新了永久链接.
I already refreshed permalinks.
有什么办法解决这个问题吗?谢谢
Any idea to fix this ? thanks
推荐答案
将其放入您的functions.php",然后重新生成永久链接.它对我有用!
Put this into your "functions.php" and then regenerate permalinks. It works to me!
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"到自定义分类的重写规则中.
The key is to replace "paged" to "page" into the rewrite rule for your custom taxonomy.
这是我在这里的第一个贡献.希望能帮到你.
This is my first contribution here. Hope I help you.
这篇关于Wordpress - 自定义分类分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!