Thứ Tư, 10 tháng 12, 2014

Wordpress - Khai báo Custom Post Type

Wordpress - Khai báo Custom Post Type


---o0o---


Link bài tham kh

Viết hàm sau vào file functions.php của theme

Hàm dưới đây khai báo 2 post type là: moro_product và moro_recipe


<?php>
function create_posttype() {
    register_post_type( 'moro_product',
        array(
            'labels' => array(
                'name'  => __( 'Products' ),
                'singular_name' => __( 'Product' ),
            ),
            'public' => true,
            'rewrite' => array('slug' => 'product'),
            'taxonomies' => array('product_category'),
            'supports' => array('title'),
        )
    );

    register_post_type( 'moro_recipe',
        array(
            'labels' => array(
                'name'  => __( 'Recipes' ),
                'singular_name' => __( 'Recipe' ),
            ),
            'public' => true,
            'rewrite' => array('slug' => 'recipes'),
            'taxonomies' => array('recipe_tags'),
            'supports' => array('title'),
        )
    );
}
?>
    

* Dòng đầu tiên thông báo cho wordpress biết khi tạo trang admin thì sẽ chạy nội dung trong hàm ‘create_posttype’.

* Hàm register_post_type() có 2 tham số

  • Tham số đầu là tên post type
  • Tham số thứ 2 là 1 array, array này bao gồm các định nghĩa cho label trong post type
* 'rewrite' => array('slug' => 'product'),

   Rewrite lại url khi query.

Không có nhận xét nào:

Đăng nhận xét