• Về tôi
  • Chính sách riêng tư
  • Liên hệ

HoangThuc.com

Website Developer

  • Trang chủ
  • WordPress
    • Plugin wordpress
  • Lập trình
    • Php
  • Thủ thuật máy tính
You are here: Home / Wordpress / Wordpress nâng cao / Wordpress nâng cao 1: Custom post type

WordPress nâng cao 1: Custom post type

9 May, 2016 by Hoàng Thức

Trong wordpress có 2 loại custom post đã được mặc định sẳn là post và page. Post cho bài viết và page cho trang .Vậy nếu muốn tạo thêm 1 custom post ta làm thế nào. Bài viết sau sẽ hướng dẫn các bạn tạo 1 custom post đợn giản nhất.

Trước hết mình xin giới thiệu cách tạo custom post đơn giản nhất có thể dành cho người không chuyên về code đó là dùng plugin. Các plugin cho phép bạn tạo mọi thứ về custom post như taxonomy, field , post type,…

Custom post type UI

custom-post-type-UI

Type

types

Đây là 2 plugin mình đã sử dụng qua và theo đánh giá của mình thì rất đầy đủ.

Và cách thứ 2 mình muốn hướng dẫn đó là code. Các bạn có thể tích hợp trong plugin, hoặc theme.

Khởi tạo custom post type

<?php
 if( ! function_exists( 'Domain_create_post_type' ) ) :
 function Domain_create_post_type() {
 $labels = array(
 'name' => 'Domain',
 'singular_name' => 'Domain',
 'add_new' => 'Add Domain',
 'all_items' => 'All Domain',
 'add_new_item' => 'Add Domain',
 'edit_item' => 'Edit Domain',
 'new_item' => 'New Domain',
 'view_item' => 'View Domain',
 'search_items' => 'Search Domain',
 'not_found' => 'No Domain found',
 'not_found_in_trash' => 'No Domain found in trash',
 'parent_item_colon' => 'Parent Domain'
 //'menu_name' => default to 'name'
 );
 $args = array(
 'labels' => $labels,
 'public' => true,
 'has_archive' => true,
 'publicly_queryable' => true,
 'query_var' => true,
 'rewrite' => true,
 'capability_type' => 'post',
 'hierarchical' => false,
 'supports' => array(
 'title',
 'editor',
 ),
 'menu_position' => 5,
 'menu_icon' => 'dashicons-clipboard',
 'exclude_from_search' => false,
 'register_meta_box_cb' => 'domain_add_post_type_metabox'
 );
 register_post_type( 'domain', $args );

register_taxonomy( 'domain_category', // register custom taxonomy - category
 'domain',
 array(
 'hierarchical' => true,
 'labels' => array(
 'name' => 'Domain category',
 'singular_name' => 'Domain category',
 )
 )
 );
 register_taxonomy( 'domain_tag', // register custom taxonomy - tag
 'domain',
 array(
 'hierarchical' => false,
 'labels' => array(
 'name' => 'Domain tag',
 'singular_name' => 'Domain tag',
 )
 )
 );
 }
 add_action( 'init', 'Domain_create_post_type' );
endif;

 function domain_add_post_type_metabox() { 
 add_meta_box( 'domain_metabox', 'Information', 'domain_metabox', 'domain', 'normal' );
 }


 function domain_metabox() {
 global $post;
 $registrar = get_post_meta($post->ID, 'registrar', true);

echo '<div class="form-horizontal" > 
<div class="form-group"> 
<label for="inputEmail3" class="col-sm-2 control-label">Registrar</label> 
<div class="col-sm-10"> <input type="text" class="form-control" name="registrar" value="'.$registrar .'" > 
</div> </div>
</div>'; 

function domain_post_save_meta( $post_id, $post ) { 
 $domain_post_meta['registrar'] = strtotime($_POST['registrar']);
 foreach( $domain_post_meta as $key => $value ) { 
 $value = implode(',', (array)$value); 
 if( get_post_meta( $post->ID, $key, FALSE ) ) { 
 update_post_meta($post->ID, $key, $value);
 } else { 
 add_post_meta( $post->ID, $key, $value );
 }
 if( !$value ) { 
 delete_post_meta( $post->ID, $key );
 }
 }
 }
 add_action( 'save_post', 'domain_post_save_meta', 1, 2 ); 
?>

Trong đây mình có làm phần tích hợp meta box nếu các bạn thấy không cần thiết thì bỏ ra nhé.

Chúc các bạn thành công !

 

Bài viết khác

  • Sử dụng media của wordpress để tải hình ảnhSử dụng media của wordpress để tải hình ảnh
  • Plugin lấy tin tự động version 2.1Plugin lấy tin tự động version 2.1
  • Tạo template trong plugin wordpressTạo template trong plugin wordpress
  • Xóa slug trên custom post và custom taxonomy: Phần 2Xóa slug trên custom post và custom taxonomy: Phần 2
  • Xóa slug trên custom post và custom taxonomy: Phần 1Xóa slug trên custom post và custom taxonomy: Phần 1
  • Hiển thị thời gian kể từ ngày đăng trong wordpressHiển thị thời gian kể từ ngày đăng trong wordpress
Share

Bình luận

Bình luận

Filed Under: Wordpress, Wordpress nâng cao Tagged With: wordpress nâng cao

Trackbacks

  1. Plugin furniture images tạo album, slideshow theo danh mục, tag - HoangThuc says:
    20 May, 2016 at 4:46 pm

    […] được viết trên dựa theo Custom post nên các bạn có thể yên tâm quản lý 1 cách vô cùng đơn […]

Bài viết mới

> 2021/10/11 19:10:10 [INFO] acme: Registering account for admin@X.X.X.X>

  • Dữ liệu JSON trong MySQL
  • Chuyển thư mục public sang public_html trong Laravel khi deploy
  • Fix “Requested icu-uc 50.1 but version of icu-uc is” trên DirectAdmin

FOLLOW ME

  • Facebook
  • Google+
  • Twitter

Danh mục

  • Blog
  • Lập trình
  • Laravel
  • MYSQL
  • Php
  • Thủ thuật máy tính
  • Vps
    • DirectAdmin
  • Wordpress
    • Plugin wordpress
    • Wordpress căn bản
    • Wordpress nâng cao
      • Custom post wordpress

Archives

  • October 2021
  • January 2021
  • December 2020
  • November 2020
  • September 2020
  • August 2020
  • September 2019
  • August 2019
  • July 2019
  • June 2018
  • August 2017
  • July 2017
  • June 2017
  • May 2017
  • March 2017
  • November 2016
  • September 2016
  • July 2016
  • June 2016
  • May 2016
  • April 2016
  • March 2016
  • January 2016
  • December 2015
  • May 2015
  • April 2015
Copyright © 2022 · HoangThuc.com · Genesis Framework