管理中的自定义帖子类型查看按钮出现400错误

时间:2014-09-05 作者:Joe

我有一个自定义的帖子类型,项目:

/*******************************
Projects Custom Post Type
*******************************/
add_action( \'init\', \'register_cpt_project\' );

function register_cpt_project() {

$labels = array( 
    \'name\' => _x( \'Projects\', \'project\' ),
    \'singular_name\' => _x( \'Project\', \'project\' ),
    \'add_new\' => _x( \'Add New\', \'project\' ),
    \'add_new_item\' => _x( \'Add New Project\', \'project\' ),
    \'edit_item\' => _x( \'Edit Project\', \'project\' ),
    \'new_item\' => _x( \'New Project\', \'project\' ),
    \'view_item\' => _x( \'View Project\', \'project\' ),
    \'search_items\' => _x( \'Search Projects\', \'project\' ),
    \'not_found\' => _x( \'No projects found\', \'project\' ),
    \'not_found_in_trash\' => _x( \'No projects found in Trash\', \'project\' ),
    \'parent_item_colon\' => _x( \'Parent Project:\', \'project\' ),
    \'menu_name\' => _x( \'Projects\', \'project\' ),
);

$args = array( 
    \'labels\' => $labels,
    \'hierarchical\' => true,

    \'supports\' => array( \'title\', \'editor\', \'excerpt\' ),
    \'taxonomies\' => array( \'project_categories\' ),
    \'public\' => true,
    \'show_ui\' => true,
    \'show_in_menu\' => true,


    \'show_in_nav_menus\' => true,
    \'publicly_queryable\' => true,
    \'exclude_from_search\' => false,
    \'has_archive\' => true,
    \'query_var\' => true,
    \'can_export\' => true,
    //\'rewrite\' => true,
    \'rewrite\' => array(\'slug\' => \'our-work/%project_categories%\', \'with_front\' => false),
    \'capability_type\' => \'post\'
);

    register_post_type( \'project\', $args );
}
和分类法,项目类别:

/*******************************
Projects Category Taxonomy
 *******************************/
add_action( \'init\', \'register_taxonomy_project_categories\' );

function register_taxonomy_project_categories() {

$labels = array( 
    \'name\' => _x( \'Project Categories\', \'project_categories\' ),
    \'singular_name\' => _x( \'Project Category\', \'project_categories\' ),
    \'search_items\' => _x( \'Search Project Categories\', \'project_categories\' ),
    \'popular_items\' => _x( \'Popular Project Categories\', \'project_categories\' ),
    \'all_items\' => _x( \'All Project Categories\', \'project_categories\' ),
    \'parent_item\' => _x( \'Parent Project Category\', \'project_categories\' ),
    \'parent_item_colon\' => _x( \'Parent Project Category:\', \'project_categories\' ),
    \'edit_item\' => _x( \'Edit Project Category\', \'project_categories\' ),
    \'update_item\' => _x( \'Update Project Category\', \'project_categories\' ),
    \'add_new_item\' => _x( \'Add New Project Category\', \'project_categories\' ),
    \'new_item_name\' => _x( \'New Project Category\', \'project_categories\' ),
    \'separate_items_with_commas\' => _x( \'Separate project categories with commas\', \'project_categories\' ),
    \'add_or_remove_items\' => _x( \'Add or remove project categories\', \'project_categories\' ),
    \'choose_from_most_used\' => _x( \'Choose from the most used project categories\', \'project_categories\' ),
    \'menu_name\' => _x( \'Project Categories\', \'project_categories\' ),
);

$args = array( 
    \'labels\' => $labels,
    \'public\' => true,
    \'show_in_nav_menus\' => true,
    \'show_ui\' => true,
    \'show_tagcloud\' => true,
    \'show_admin_column\' => false,
    \'hierarchical\' => true,

    //\'rewrite\' => true,
    \'rewrite\' => array(\'slug\' => \'our-work\'),
    \'query_var\' => true
);

    register_taxonomy( \'project_categories\', array(\'project\'), $args );
}


add_action( \'init\', \'register_cpt_leadership\' );
在我的分类法归档模板中,我构建了包含分类法术语的URL。因此,如果“项目A”属于“很棒的东西”类别,则URL显示为:

[我的领域]/我们的工作/很棒的东西/项目a

所有这些在前端都非常有效。不起作用的是从管理端单击按钮查看项目。如果我正在编辑“项目A”,请单击永久链接部分下的查看项目按钮,或单击右侧的预览更改按钮。

View Project Button

它返回400错误请求页面,并将浏览器发送到:

[我的领域]/我们的工作/%project\\u类别%/project-a

如何使管理中的预览/查看按钮转到:

[我的领域]/我们的工作/project\\u类别/project-a

该URL是有效的,而且由于预览链接没有绑定到特定的分类法,因此这将是首选的解决方案。

EDIT:我不确定我的解释是否正确。我不需要改变整个站点处理永久链接的方式。我所需要的只是一种连接到管理部分中任何链接的方法(上图中的按钮、预览按钮和列表页上的查看链接)。如果链接是通过管理员生成的,我需要将%project\\u categories%变量设置为“project\\u categories”。

1 个回复
最合适的回答,由SO网友:Joe 整理而成

我在这里找到了一个解决方案:

Custom post types, taxonomies, and permalinks

死亡医生的解决方案很有魅力。

function filter_post_type_link($link, $post)
{
if ($post->post_type != \'project\')
    return $link;

if ($cats = get_the_terms($post->ID, \'project_categories\'))
    $link = str_replace(\'%project_categories%\', array_pop($cats)->slug, $link);
return $link;
}
add_filter(\'post_type_link\', \'filter_post_type_link\', 10, 2);
这将检查与项目关联的项目类别,并使用第一个类别构建项目的永久链接。

结束

相关推荐