自定义帖子类型和分类法分页 404 错误

Posted

技术标签:

【中文标题】自定义帖子类型和分类法分页 404 错误【英文标题】:Custom Post Type and Taxonomy pagination 404 error 【发布时间】:2012-01-06 08:52:24 【问题描述】:

分页不适用于 taxonomy.php。这是我注册自定义帖子类型和分类的代码

add_action('init', 'ep_add_equipment');
function ep_add_equipment()

$labels = array(
'name' => _x('Equipments', 'post type general name', 'epanel'),
'singular_name' => _x('Equipments', 'post type singular name', 'epanel'),
'add_new' => _x('Add New Equipment', 'slide', 'epanel'),
'add_new_item' => __('Add New Equipment', 'epanel'),
'edit_item' => __('Edit Equipment', 'epanel'),
'new_item' => __('New Equipment', 'epanel'),
'view_item' => __('View Equipments', 'epanel'),
'search_items' => __('Search Equipments', 'epanel'),
'not_found' => __('No Equipments found', 'epanel'),
'not_found_in_trash' => __('No Equipments found in Trash', 'epanel'),
'parent_item_colon' => ''
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => array('slug' => 'products'),
'capability_type' => 'post',
'exclude_from_search' => true,
'hierarchical' => true,
'menu_icon' => get_template_directory_uri() .'/images/equipment.png',
'menu_position' => 5,
'can_export' => true,
'supports' => array('title','editor','thumbnail', 'comments','page-attributes','excerpt'/*,'author','excerpt'*/),
);
register_post_type('equipment',$args);

register_taxonomy('equipments', 'equipment', array('hierarchical' => true, 'show_in_nav_menus' => true, 'show_ui' => true,
'query_var' => true, 'labels' => array('name' => __( 'Equipments Categories' ), 'singular_label' => __('Category'), 'add_new_item' => __( 'Add New Category' ), 'search_items' => __( 'Search Categories' )), 'rewrite' => array('slug' => 'equipments')));



function add_menu_admin_bar() 
global $wp_admin_bar;

if ( !is_super_admin() || !is_admin_bar_showing() )
exit;

// $wp_admin_bar->add_menu( array( 'id' => 'theme_options', 'title' =>__( 'E-Panel', 'epanel' ), 'href' => admin_url('admin.php')."?page=epanel_settings" ) );
$wp_admin_bar->add_menu( array( 'id' => 'equipment', 'title' =>__( 'Equipments', 'epanel' ), 'href' => admin_url('edit.php')."?post_type=equipment" ) );

$wp_admin_bar->add_menu( array( 'parent' => 'equipment', 'title' =>__( 'Add New Equipment', 'epanel' ), 'href' => admin_url('post-new.php')."?post_type=equipment" ) );
$wp_admin_bar->add_menu( array( 'parent' => 'equipment', 'title' =>__( 'Equipment Categories', 'epanel' ), 'href' => admin_url('edit-tags.php')."?taxonomy=equipment_categories&post_type=equipment" ) );


add_action( 'admin_bar_menu', 'add_menu_admin_bar' , 70);

taxonomy.php 的循环代码

<?php
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args=array(
'taxonomy' => $term->taxonomy,
'term' => $term->slug,
'post_type' => 'equipment',
'paged'=> $paged,
/*'posts_per_page' => 10,*/
'caller_get_posts'=> 1
);
?>
<?php query_posts( $args ); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>

【问题讨论】:

也许我是盲人,但我在任何一个链接上都看不到分页。 嘿杰里米我很抱歉实际上我禁用了分页。如果你能解决它,请现在检查。感谢回复 【参考方案1】:

您可能需要启用搜索才能启用分页

在声明自定义分类时,您应该禁用搜索排除。

exclude_from_search => 假

这解决了我的问题。我很难找到这个解决方案。希望对大家有所帮助。

我的代码是:

register_post_type( 'lifestyle',
                array( 
                'label' => __('Lifestyle', 'tmi'), 
                'public' => true, 
                'show_ui' => true,
                'show_in_nav_menus' => true,
                'rewrite' => true,
                'hierarchical' => true,
                'menu_position' => 5,
                'exclude_from_search' =>false,
                'supports' => array(
                                     'title',
                                     'editor',
                                     'thumbnail',
                                     'excerpt',
                                     'revisions')
                    ) 
                );

    register_taxonomy('lifestylecat', __('lifestyle', 'tmi'),array('hierarchical' => true, 'label' =>  __('Categories', 'tmi'), 'singular_name' => __('Category', 'tmi'))
    );

【讨论】:

谢谢!!!正在拉扯我的头发,试图理解为什么这突然停止工作......这是 exlude_from_search 论点...... OMG 2020 仍然有问题,这在拉我的头发 3 小时后救了我【参考方案2】:
// get the global query object
global $wp_query;
// get the correct page var
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
// create the page argument
$args=  array('paged'=> $paged);
// merge the page argument array with the original query array
$args = array_merge( $wp_query->query, array( 'post_type' => 'equipment' ) );
// Re-run the query with the new arguments
query_posts( $args );

【讨论】:

您好 Espen,感谢您的回复...实际上我已经通过删除 'exclude_from_search' => true 解决了这个问题,同时注册了自定义帖子类型

以上是关于自定义帖子类型和分类法分页 404 错误的主要内容,如果未能解决你的问题,请参考以下文章

wordpress 3.8.1 类别第 2 页错误 404 未找到 / 自定义帖子类型

自定义帖子类型和页面层次结构 - 错误 404

wordpress中自定义帖子类型的分页

使用移动浏览器请求自定义帖子类型时出现 Wordpress REST API 404 错误

自定义分类页面不起作用

Wordpress 自定义帖子类型和自定义分类