2013-10-31 21 views
2

我已閱讀了所有可以找到的博客帖子,但沒有任何內容似乎適用於我。我需要做的就是讓WP自動爲我的自定義帖子類型「照片」分配默認分類/分類(「最新」),以便當用戶添加新照片時,「最新」類別已被選中並且分配(就像一個正常的博客文章「未分類」一樣)。如何在WP中爲自定義帖子類型設置默認分類(類別)

declare (encoding = 'UTF-8'); 

! defined('ABSPATH') and exit; 

add_action('init', array ('MCP_Photos', 'init')); 

class MCP_Photos 
{ 
    /** 
    * Creates a new instance. 
    * 
    * @wp-hook init 
    * @see __construct() 
    * @return void 
    */ 
    public static function init() 
    { 
     new self; 
    } 

    /** 
    * Constructor 
    */ 
    public function __construct() 
    { 
     $labels = array(
    'name' => 'Photography', 
    'singular_name' => 'Photo', 
    'add_new' => 'Add New', 
    'add_new_item' => 'Add New Photo', 
    'edit_item' => 'Edit Photo', 
    'new_item' => 'New Photo', 
    'all_items' => 'All Photos', 
    'view_item' => 'View Photo', 
    'search_items' => 'Search Photos', 
    'not_found' => 'No Photos found', 
    'not_found_in_trash' => 'No Photos found in Trash', 
    'parent_item_colon' => '', 
    'menu_name' => 'Photography' 
); 

     $args = array(
    'labels' => $labels, 
    'public' => true, 
    'publicly_queryable' => true, 
    'show_ui' => true, 
    'show_in_menu' => true, 
    'query_var' => true, 
      'rewrite' => array(
       'with_front' => false, 
       'slug' => "photo" 
      ), 
    'capability_type' => 'post', 
    'has_archive' => true, 
    'hierarchical' => true, 
    'menu_position' => null, 
    'supports' => array('title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments'), 
    'taxonomies' => array('post_tag') 
     ); 
     register_post_type("photos", $args); 

     // Prevent WordPress from sending a 404 for our new perma structure. 
     add_rewrite_rule(
     '^photo/(\d+)/[^/]+/?$', 
     'index.php?post_type=photos&p=$matches[1]', 
     'top' 
     ); 

     // Inject our custom structure. 
     add_filter('post_type_link', array ($this, 'fix_permalink'), 1, 2); 
    } 

    /** 
    * Filter permalink construction. 
    * 
    * @wp-hook post_type_link 
    * @param string $post_link default link. 
    * @param int $id Post ID 
    * @return string 
    */ 
    public function fix_permalink($post_link, $id = 0) 
    { 
     $post = &get_post($id); 
     if (is_wp_error($post) || $post->post_type != 'photos') 
     { 
      return $post_link; 
     } 
     // preview 
     empty ($post->slug) 
      and $post->slug = sanitize_title_with_dashes($post->post_title); 

     return home_url(
      user_trailingslashit("photo/$post->ID/$post->slug") 
     ); 
    } 
} 



// ----------------------------- add photography categories taxonomy ---------------------------------- 


function create_photo_categories() { 
    register_taxonomy(
     'photography', // name of the taxonomy 
     'photos', // for which post type it applies 
     array(
      'labels' => array(
       'name' => 'Categories', 
       'add_new_item' => 'Add New Category', 
       'new_item_name' => "New Category" 
      ), 
      'show_ui' => true, 
      'show_tagcloud' => false, 
      'hierarchical' => true 
     ) 
    ); 
} 

add_action('init', 'create_photo_categories', 0); 
+0

發佈您的register_post_type代碼爲您的自定義帖子類型 – codepixlabs

回答

0

尋找在這樣的事情你register_post_type功能的$args - >

taxonomies => array('categories'); 

刪除

要註冊您的自定義後類型分類使用功能

Register Taxonomy

+0

謝謝wordpresser。不幸的是,通過刪除該行,它刪除了我的自定義帖子類型標籤。我目前使用下面的函數來註冊自定義後類型分類: 'code' 功能create_photo_categories(){ register_taxonomy的分類 「照片」的( 「攝影」,//名,//用於該職位類型適用 陣列( '標籤'=>數組( '名稱'=> '類別', 'add_new_item'> '添加新類別', 'new_item_name'=> 「新類別」 ) 'show_ui' => true – rainerbrunotte

0

添加分類這樣的..

'taxonomies' => array('timeline','category',), 

總碼變成李ke此爲管理員:

// Admin 
      'capability_type' => 'post', 
      'menu_icon'  => 'dashicons-businessman', 
      'menu_position' => 10, 
      'query_var'  => true, 
      'show_in_menu' => true, 
      'show_ui'  => true, 
      'taxonomies' => array('timeline','category',), 
      'supports'  => array(
      'title', 
      'editor', 
      'custom_fields', 
      'timeline', 
      ), 
相關問題