Taxonomy

Post Type

Theo mặc định, SkillDo đi kèm với post (bài viết), bạn có thể tạo bao nhiêu loại nội dung tùy chỉnh tùy thích và các loại nội dung tùy chỉnh này được gọi là custom post type .

Một Custom Post Type sẽ hoàn toàn có đủ những chức năng giống hệt như post type mặc định, nghĩa là bạn có thể sử dụng category, tag, custom field, featured image,….và có thể gọi hiển thị ra ngoài trang chủ cùng lúc với Post.

Add

Thêm một post type vào hệ thống

static function Taxonomy::addPost($post_type, $args);

#Parameters

$post_type Post type key

$args là một mảng gồm nhiều key và được giải thích ở phần dưới đây.

Parameter Value Description
labels array

name : tên của post type dạng số nhiều

singular_name : tên của post type dạng số ít

public true | false Kích hoạt sử dụng post type
show_in_nav_menus true | false Nếu có giá trị TRUE thì sẽ hiển thị bên trang quản lý menu. admin/theme/menu
show_in_nav_admin true | false Hiển thị trên thanh Admin bar bên trái.
menu_position int thứ tự vị trí hiển thị trong menu (tay trái)
menu_icon string icon hoặc hình ảnh đại diện cho post type
supports array

Các metabox và trường dữ liệu hỗ trợ post type sử dụng.

Group: info, media, seo, theme

Field: title, excerpt, content, image, public, slug, seo_title, seo_keywords, seo_description, theme_layout, theme_view

capabilities array

Các quyền mà post type cung cấp cho người dùng

  • view
  • add
  • edit
  • delete

#Examplte

function tao_custom_post_type() {

    $label = array(
        'name' => 'Các dự án', //Tên post type dạng số nhiều
        'singular_name' => 'Dự án' //Tên post type dạng số ít
    );

    $args = array(
        'labels' => $label, //Gọi các label trong biến $label ở trên
        'supports' => array(
            'group'  => array( 'info', 'media', 'seo', 'theme' ),
            'field'  => array(
                'title', 'excerpt', 'content', 'image', 'public', 'slug',
                'seo_title', 'seo_keywords', 'seo_description', 'theme_layout', 'theme_view'),
        ), //Các trường dữ liệu hỗ trợ
        'taxonomies' => array('category', 'post_tag'), //cate type được phép sử dụng
        'public' => true, //Kích hoạt post type
        'show_in_nav_menus' => true, //hiển thị bên trang quản lý menu.
        'show_in_nav_admin' => true, //Hiển thị trên thanh Admin bar màu đen.
        'menu_position'     => 5,    //Thứ tự vị trí hiển thị trong menu (tay trái)
        'menu_icon'         => '',   //Đường dẫn tới icon sẽ hiển thị
        'capabilities' => array(
            'view'      => 'view_posts',
            'add'       => 'add_posts',
            'edit'      => 'edit_posts',
            'delete'    => 'delete_posts',
        ),
    );

    Taxonomy::addPost('project', $args);
}

/* Kích hoạt hàm tạo custom post type */
add_action('init', 'tao_custom_post_type');

Get

Lấy thông tin một post type

static function Taxonomy::getPost($post_type);

#Examplte

Taxonomy::getPost('post');
    /* Array
(
    [labels] => Array
        (
            [name] => Bài viết
            [singular_name] => Bài viết
            [add_new_item] => Thêm bài viết
            [edit_item] => Thêm bài viết
        )

    [public] => 1
    [menu_position] => 3
    [menu_icon] =>
    [cate_type] => post_categories
    [taxonomies] => Array
        (
            [0] => post_categories
        )

    [supports] => Array
        (
            [group] => Array
                (
                    [0] => info
                    [1] => media
                    [2] => seo
                    [3] => theme
                )

            [field] => Array
                (
                    [0] => title
                    [1] => excerpt
                    [2] => content
                    [3] => image
                    [4] => public
                    [5] => slug
                    [6] => seo_title
                    [7] => seo_keywords
                    [8] => seo_description
                    [9] => theme_layout
                    [10] => theme_view
                )

        )

    [capabilities] => Array
        (
            [view] => view_posts
            [add] => add_posts
            [edit] => edit_posts
            [delete] => delete_posts
        )

    [exclude_from_search] => 1
    [show_in_nav_menus] => 0
    [show_in_nav_admin] => 1
) */

Has

Kiểm tra post type có được đăng ký.

static function Taxonomy::hasPost($post_type);

#Examplte

Taxonomy::hasPost('post');
//True

Remove

Gở bỏ post type khỏi hệ thống.

static function Taxonomy::removePost($post_type);

#Examplte

Taxonomy::removePost('post');
//True

Category type

Taxonomy là tập hợp những thể loại giống như category. Mặc định khi bạn cài đặt SkillDo sẽ có taxonomies mặc định đó là Category.

Add

Thêm một cate type vào hệ thống

static function Taxonomy::addCategory($cate_type, $post_type, $args);

#Parameters

$cate_type là tên của taxonomy cần tạo, chúng ta nên đặt chữ cái thường không dấu và tiếng Anh kế hợp với ký tự gạch dưới. Ví dụ category_project, brand

$post_type Tên của custom post type mà taxonomy thuộc về, trong hệ thống gồm có thể loại post

$args là một mảng gồm nhiều key và được giải thích ở phần dưới đây.

Parameter Value Description
labels array

name : tên của cate type dạng số nhiều

singular_name : tên của cate type dạng số ít

public true | false Kích hoạt sử dụng cate type
show_in_nav_menus true | false Nếu có giá trị TRUE thì sẽ hiển thị bên trang quản lý menu. admin/theme/menu
show_in_nav_admin true | false Hiển thị trên thanh Admin bar bên trái.
menu_position int hứ tự vị trí hiển thị trong menu (tay trái)
menu_icon string icon hoặc hình ảnh đại diện cho post type
parent true | false Nếu true cate type sẽ có phân cấp danh mục.
supports array

Các metabox và trường dữ liệu hỗ trợ post type sử dụng.

Group: info, media, seo, theme

Field: title, excerpt, content, image, public, slug, seo_title, seo_keywords, seo_description, theme_layout, theme_view

capabilities array

Các quyền mà post type cung cấp cho người dùng

  • edit
  • delete

#Examplte

function tao_taxonomy() {
    $labels = array(
        'name' => 'Các loại dự án',
        'singular' => 'Loại dự án',
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'show_admin_column' => true,
        'show_in_nav_menus' => true,
        'parent' => true,
        'capabilities' => array(
            'edit'      => 'manage_categories',
            'delete'    => 'delete_categories',
        ),
    );

    Taxonomy::addCategory('category_project', 'project', $args);
}

// Hook into the 'init' action
add_action( 'init', 'tao_taxonomy', 0 );

Get

Lấy thông tin một cate type

static function Taxonomy::getCategory($cate_type);

#Examplte

Taxonomy::getCategory('post_categories');
    /* Array
(
    [labels] => Array
        (
            [name] => Chuyên mục bài viết
            [singular_name] => Chuyên mục
            [add_new_item] => Thêm chuyên mục
        )

    [public] => 1
    [menu_position] => 3
    [menu_icon] =>
    [parent] => 1
    [show_admin_column] => 1
    [post_type] => post
    [supports] => Array
        (
            [group] => Array
                (
                    [0] => info
                    [1] => media
                    [2] => seo
                    [3] => theme
                    [4] => category
                )

            [field] => Array
                (
                    [0] => name
                    [1] => excerpt
                    [2] => content
                    [3] => image
                    [4] => public
                    [5] => slug
                    [6] => seo_title
                    [7] => seo_keywords
                    [8] => seo_description
                    [9] => theme_layout
                    [10] => theme_view
                    [11] => parent_id
                )

        )

    [capabilities] => Array
        (
            [edit] => manage_categories
            [delete] => delete_categories
        )

    [show_in_nav_menus] => 1
    [exclude_from_search] => 1
    [show_in_nav_admin] => 1
) */

Get By Post Type

Lấy danh sách cate type thuộc một post type

static function Taxonomy::getCategoryByPost($post_type);

#Examplte

Taxonomy::getCategoryByPost('post');
    /* Array
(
    [0] => post_categories
) */

Has

Kiểm tra cate type có được đăng ký.

static function Taxonomy::hasCategory($cate_type);

#Examplte

Taxonomy::hasCategory('post_categories');
//True

Remove

Gở bỏ cate type khỏi hệ thống.

static function Taxonomy::removeCategory($cate_type);

#Examplte

Taxonomy::removeCategory('post_categories');
//True