2015-11-03 163 views
0

我已經創建了作爲自定義帖子類型的投資組合。我想使永久只爲自定義文章類型,如: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 
     )); 
    } 
} 
+1

的可能的複製[WordPress的永久鏈接與自定義後的類型和自定義分類(http://stackoverflow.com/questions/10296959/wordpress-permalink-with-custom-post-type-and-custom-taxonomy) – vard

+0

不工作,其定製後類型類別/職位 – Ni3

+0

CPT類別=分類 – vard

回答

0

我建議嘗試以下更改所有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如何處理這些新的URL。我建議先嚐試一下上面的代碼,看看你是否有這些新的URL。如果是這樣,您將不得不添加一些重寫規則或使用pre_get_posts操作。

相關問題