• 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 / Tạo template trong plugin wordpress

Tạo template trong plugin wordpress

26 July, 2017 by Hoàng Thức Leave a Comment

Thông thường để tạo template cho trang bạn phải vào theme để tạo template. Nhưng vấn đề ở đây là khi bạn muốn thay đổi theme thì template vẫn không thay đổi. Giải pháp đưa ra là bạn tạo template trong plugin để tách ra khỏi theme. Như vầy dù bạn có thay đổi theme thì template vẫn giữ nguyên. Tiêu biễu như plugin Woocommerce

Các loại template các bạn dùng custom post, archive và page.

Các hook được sử dụng: single_template, archive_template và page_template

Tiến hành code.

1.Custom post

Đối với custom post ta dung hook single_template. ‘ YOUR_CUSTOM_POST ‘ là tên custom post của bạn dùng. 

function get_custom_post_type_single($single_template) {
    global $post;
    if ($post->post_type == 'YOUR_CUSTOM_POST') {
        $single_template = dirname( __FILE__ ) . '/single-YOUR_CUSTOM_POST.php';
    }
    return $single_template;
}
add_filter( 'single_template', 'get_custom_post_type_single' );

2.Archive

Đối với trang archive ta dung hook archive_template. ‘ YOUR_TAXONOMY ‘ là tên taxonomy của bạn dùng.

function get_custom_archive($archive_template){
    global $wpdb;
    $archive =$wpdb->last_result;
    if ( $archive[0]->taxonomy==  'YOUR_TAXONOMY' ) {
        $archive_template = dirname( __FILE__ ) . '/archive-YOUR_TAXONOMY.php';
    }
    return $archive_template;
}
add_filter( "archive_template", "get_custom_archive" ) ;

3.Page Template

Đối với page bạn phải chọn slug chính xác thì template mới được sử dụng.

add_filter( 'page_template', 'wpa_page_template' );
function wpa_page_template( $page_template )
{
if ( is_page( YOUR_SLUG ) ) {
$page_template = dirname( __FILE__ ) . '/templates/template-YOUR_SLUG.php';
}
return $page_template;
}

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

Bài viết khác

  • 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
  • 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
  • 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
  • Lỗi redirect khi phân trang trong single wordpressLỗi redirect khi phân trang trong single wordpress
  • 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: Plugin wordpress, Wordpress, Wordpress nâng cao Tagged With: plugin wordpress, Taxonomy wordpress, Thủ thuật wordpress, wordpress nâng cao

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

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