页面未找到分类自定义帖子类型

时间:2015-01-23 作者:Jeremy Love

我用分类法创建了一个自定义的帖子类型,当我转到分类法页面并查看其中一个分类法时,我会看到一个页面未找到错误,就像没有创建分类法一样。

有什么建议吗?

/*Products*/
$labels = array(
    \'name\' => _x(\'Products\', \'post type general name\'),
    \'singular_name\' => _x(\'Product\', \'post type singular name\'),
    \'add_new\' => _x(\'Add New\', \'Product\'),
    \'add_new_item\' => __("Add New Product"),
    \'edit_item\' => __("Edit Product"),
    \'new_item\' => __("New Product"),
    \'view_item\' => __("View Product"),
    \'search_items\' => __("Search Product"),
    \'not_found\' =>  __(\'No products found\'),
    \'not_found_in_trash\' => __(\'No products found in Trash\'), 
    \'parent_item_colon\' => \'\'
  );

$args = array(
    \'labels\' => $labels,
        \'singular_label\' => __(\'Products\'),
        \'public\' => true,
        \'show_ui\' => true,
        \'capability_type\' => \'post\',
        \'hierarchical\' => false,
        \'rewrite\' => true,
        \'query_var\' => \'products\',
        \'taxonomies\' => array(\'products-cat\'),
        \'supports\' => array(\'title\', \'editor\', \'custom-fields\', \'thumbnail\', \'excerpt\'),
        \'has_archive\' => true,
);
register_post_type( \'products\' , $args );

// Add to admin_init function
add_filter(\'manage_edit-products_columns\', \'add_new_products_columns\');
function add_new_products_columns($columns) {
$columns = array(
    \'cb\' => \'<input type="checkbox" />\',
        \'images\' => \'Images\',
    \'title\' => \'Product Name\',
        \'author\' => \'Author\',
        \'product-categories\' => \'Categories\',
        \'tags\' => \'Tags\',
        \'date\' => \'Date\',
); 
    return $columns;
}

add_action(\'manage_products_posts_custom_column\', \'manage_products_columns\', 10, 2);

function manage_products_columns($column_name, $id) {
    global $wpdb;
    switch ($column_name) {

    case \'images\':
            //echo get_the_post_thumbnail( $page->ID, array(50,50) ); 
    break;

    case \'product-categories\':
        echo get_the_term_list($post->ID, \'products-cat\', \'\', \', \',\'\');
    break;

        default:
        break;
    } 
}

add_action( \'init\', \'products_create_taxonomies\', 0 );

function products_create_taxonomies() 
{
    // Photo Categories
    register_taxonomy(\'products-cat\',array(\'products\'),array(
        \'hierarchical\' => true,
        \'label\' => \'Product Categories\',
        \'singular_name\' => \'Products Category\',
        \'show_ui\' => true,
        \'query_var\' => true,
        \'rewrite\' => array(\'slug\' => \'products\' )
    ));
}

/*End Products*/
我的档案页上有这个

归档产品。php

<?php get_header(); ?>

<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post();?>
                <?php the_title(); ?>   
<?php endwhile; endif; ?>

4 个回复
最合适的回答,由SO网友:Jeremy Love 整理而成

经过一些研究,我找到了一个博客,他实际上对这个问题有了答案。

下面是函数以及blog url.

function taxonomy_slug_rewrite($wp_rewrite) {
    $rules = array();
    // get all custom taxonomies
    $taxonomies = get_taxonomies(array(\'_builtin\' => false), \'objects\');
    // get all custom post types
    $post_types = get_post_types(array(\'public\' => true, \'_builtin\' => false), \'objects\');

    foreach ($post_types as $post_type) {
        foreach ($taxonomies as $taxonomy) {

            // go through all post types which this taxonomy is assigned to
            foreach ($taxonomy->object_type as $object_type) {

                // check if taxonomy is registered for this custom type
                if ($object_type == $post_type->rewrite[\'slug\']) {

                    // get category objects
                    $terms = get_categories(array(\'type\' => $object_type, \'taxonomy\' => $taxonomy->name, \'hide_empty\' => 0));

                    // make rules
                    foreach ($terms as $term) {
                        $rules[$object_type . \'/\' . $term->slug . \'/?$\'] = \'index.php?\' . $term->taxonomy . \'=\' . $term->slug;
                    }
                }
            }
        }
    }
    // merge with global rules
    $wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
add_filter(\'generate_rewrite_rules\', \'taxonomy_slug_rewrite\');

SO网友:Julien Menichini

我在本地nginx平台上遇到了同样的问题。在我更新了设置中的permalinks之后,一切都很好。设置>永久链接>保存更改(无需修改)

SO网友:Privateer

我至少推荐两件事:

更改您的products_create_taxonomies 操作至少为1,如果不是5或更高。(由于某些原因,我认为0对于操作优先级无效)

将帖子类型创建添加到init操作中,就像您以比分类法更早的优先级创建分类法一样(因为您是通过分类法连接它们的)。

如果这两个更改没有解决问题,请尝试同时设置这两个更改(不通过它们自己的结构将它们链接),然后以稍后的优先级调用以下内容(仍在初始化期间):

register_taxonomy_for_object_type( \'products-cat\', \'products\' );
这应该确保它们完全正确地联系在一起。

SO网友:Pieter Goosen

我想你的主要问题是taxonomy. 目前,您对分类法的重写如下所示

\'rewrite\' => array(\'slug\' => \'products\' )
这就是重写在您的custom post type. 以下内容

\'rewrite\' => true,
在自定义帖子类型中转换为此

\'rewrite\' => array(\'slug\' => \'products\' )
默认情况下,重写中的冲突将404您的分类请求。作为解决方案,您可以更改分类法的重写,或者查看this post 我最近在这个网站上做了一个更好的解决方法。

Important: 每次更新代码后刷新永久链接,将永久链接重置为新结构

结束

相关推荐