【问题标题】:custom post type category permalink to /%category%/%postname%/自定义帖子类型类别永久链接到 /%category%/%postname%/
【发布时间】:2015-11-03 10:23:12
【问题描述】:

我已经创建了投资组合作为自定义帖子类型。我只想为自定义帖子类型制作永久链接,例如:www.domain.com/custom-post-type-category/postname

当前固定链接是:www.domain.com/custom-post-type/postname

它完全是面向自定义帖子的。

if(!function_exists("pexeto_register_portfolio_post_type")){
    function pexeto_register_portfolio_post_type() {

        //the labels that will be used for the portfolio items
        $labels = array(
            'name' => _x('Portfolio', 'portfolio name', 'pexeto'),
            'singular_name' => _x('Portfolio Item', 'portfolio type singular name', 'pexeto'),
            'add_new' => _x('Add New', 'portfolio', 'pexeto'),
            'add_new_item' => __('Add New Item', 'pexeto'),
            'edit_item' => __('Edit Item', 'pexeto'),
            'new_item' => __('New Portfolio Item', 'pexeto'),
            'view_item' => __('View Item', 'pexeto'),
            'search_items' => __('Search Portfolio Items', 'pexeto'),
            'not_found' =>  __('No portfolio items found', 'pexeto'),
            'not_found_in_trash' => __('No portfolio items found in Trash', 'pexeto'), 
            'parent_item_colon' => ''
        );

        //register the custom post type
        register_post_type( PEXETO_PORTFOLIO_POST_TYPE,
            array( 'labels' => $labels,
             'public' => true,  
             'show_ui' => true,  
             'capability_type' => 'post',  
             'hierarchical' => false,  
             'rewrite' => array('slug'=>'portfolio'),
             'taxonomies' => array('portfolio_category'),
             'supports' => array('title', 'editor', 'thumbnail', 'comments', 'page-attributes') ) );
    }
}

if(!function_exists("pexeto_register_portfolio_category")){
    function pexeto_register_portfolio_category(){

        register_taxonomy("portfolio_category",
        array(PEXETO_PORTFOLIO_POST_TYPE),
        array(  "hierarchical" => true,
            "label" => "Portfolio Categories", 
            "singular_label" => "Portfolio Categories", 
            "rewrite" => true,
            "query_var" => true
        ));
    }
}

【问题讨论】:

标签: wordpress custom-post-type permalinks


【解决方案1】:

我建议尝试以下方法将所有 URL 从 :post-type:/:post-name: 更改为 :post-category:/:post-name:

function portfolio_post_link($post_link, $post, $leavename) {
    if($post->post_type == 'portfolio') {
        $category_slug = get_the_terms($post->ID, 'portfolio_category');
        $category_slug = is_array($category_slug) ? '/' . $category_slug[0]->slug . '/' : '/';
        $post_link = str_replace('/'.$post->post_type.'/', $category_slug, $post_link);
    }
    return $post_link;
}
add_filter('post_type_link', 'portfolio_post_link', 10, 3);

但这还不够——您需要告诉 wordress 如何处理这些新网址。我建议先尝试上面的代码,看看你是否得到带有这些新网址的 404。如果是这样,您将不得不添加一些重写规则或使用pre_get_posts 操作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-28
    • 1970-01-01
    • 1970-01-01
    • 2012-11-13
    • 1970-01-01
    • 2013-09-10
    相关资源
    最近更新 更多