ฉันมีประเภทโพสต์ที่กำหนดเองที่เรียกว่าportfolio
และฉันพยายามที่จะเพิ่มแท็ก taxonomy ลงไปฉันจะทำอย่างไร
ฉันมีประเภทโพสต์ที่กำหนดเองที่เรียกว่าportfolio
และฉันพยายามที่จะเพิ่มแท็ก taxonomy ลงไปฉันจะทำอย่างไร
คำตอบ:
เช่นนี้: (ที่มันบอกว่า "ผลงาน" เป็นที่ที่คุณลงทะเบียน taxonomy เป็นประเภทโพสต์
add_action( 'init', 'create_tag_taxonomies', 0 );
//create two taxonomies, genres and tags for the post type "tag"
function create_tag_taxonomies()
{
// Add new taxonomy, NOT hierarchical (like tags)
$labels = array(
'name' => _x( 'Tags', 'taxonomy general name' ),
'singular_name' => _x( 'Tag', 'taxonomy singular name' ),
'search_items' => __( 'Search Tags' ),
'popular_items' => __( 'Popular Tags' ),
'all_items' => __( 'All Tags' ),
'parent_item' => null,
'parent_item_colon' => null,
'edit_item' => __( 'Edit Tag' ),
'update_item' => __( 'Update Tag' ),
'add_new_item' => __( 'Add New Tag' ),
'new_item_name' => __( 'New Tag Name' ),
'separate_items_with_commas' => __( 'Separate tags with commas' ),
'add_or_remove_items' => __( 'Add or remove tags' ),
'choose_from_most_used' => __( 'Choose from the most used tags' ),
'menu_name' => __( 'Tags' ),
);
register_taxonomy('tag','portfolio',array(
'hierarchical' => false,
'labels' => $labels,
'show_ui' => true,
'update_count_callback' => '_update_post_term_count',
'query_var' => true,
'rewrite' => array( 'slug' => 'tag' ),
));
}
?>
หรือเพียงเพิ่ม:
'taxonomies' => array('post_tag')
อาร์เรย์ของ taxonomies ที่ลงทะเบียนเช่น
category
หรือpost_tag
ที่จะใช้กับโพสต์ประเภทนี้ สามารถใช้แทนการโทรregister_taxonomy_for_object_type()
โดยตรง taxonomiesregister_taxonomy()
กำหนดเองยังคงต้องลงทะเบียนกับ
กับ$args
อาร์เรย์ในfunctions.php
แฟ้ม: register_post_type()
ที่คุณสร้างประเภทโพสต์ที่กำหนดเองด้วย
ใช้สิ่งนี้:
add_action( 'init', 'gp_register_taxonomy_for_object_type' );
function gp_register_taxonomy_for_object_type() {
register_taxonomy_for_object_type( 'post_tag', 'portfolio' );
};
เพียงใช้รหัสนี้อาจช่วยคุณได้
add_action( 'init', 'create_client_tax' );
function create_client_tax() {
register_taxonomy(
'client_tag', //your tags taxonomy
'client', // Your post type
array(
'hierarchical' => false,
'label' => __( 'Tags', CURRENT_THEME ),
'singular_name' => __( 'Tag', CURRENT_THEME ),
'rewrite' => true,
'query_var' => true
)
);
}
วิธีที่ 1 (จากคำตอบด้านบน: https://wordpress.stackexchange.com/a/106211/38771 )
ประเภทโพสต์ที่ต่างกันจะใช้ข้อมูล "แท็ก" ที่เหมือนกันของประเภท "โพสต์" เริ่มต้น
ประโยชน์ที่ได้รับ
ทุกอย่าง (ควร) ทำงานนอกกรอบด้วยหนึ่งบรรทัด
แท็กทั้งหมดของคุณในที่เดียวใน CMS
ข้อเสีย
วิธีที่ 2 (จากคำตอบด้านบน: https://wordpress.stackexchange.com/a/62263/38771 )
โพสต์ประเภทต่างๆใช้การกำหนดค่า / ข้อมูล "แท็ก" ที่แตกต่างกัน
ประโยชน์ที่ได้รับ
ถ่วงดุลอำนาจ
อีกเล็กน้อยรหัสการติดตั้ง
อันที่จริงพวกเขาเป็นวิธีที่ทรงพลังอย่างยิ่งในการจัดกลุ่มรายการต่าง ๆ ในทุกรูปแบบ https://codex.wordpress.org/Taxonomies#Custom_Taxonomies