Bài viết sẽ hướng dẫn cách để tạo một post type mới trong WordPress mà không cần sử dụng đến Plugin.

Code

Dưới đây là đoạn code để tạo một loại bài viết trong WordPress được gọi là “short-post”. Hãy lưu ý rằng, cần tùy chỉnh mã này để phù hợp với mục đích và yêu cầu cụ thể mà bạn đề ra.

<?php

function shortpost_create_post_type() {
  $labels = array(
    'name'               => 'Bài viết ngắn',
    'singular_name'      => 'Bài viết ngắn',
    'menu_name'          => 'Bài viết ngắn',
    'name_admin_bar'     => 'Bài viết ngắn',
    'add_new'            => 'Viết bài mới',
    'add_new_item'       => 'Viết bài mới',
    'edit_item'          => 'Chỉnh sửa',
    'new_item'           => 'Thêm mới',
    'view_item'          => 'Xem',
    'search_items'       => 'Tìm các bài viết',
    'not_found'          => 'Không tìm thấy bài viết',
    'not_found_in_trash' => 'Thùng rác rỗng',
    'all_items'          => 'Tất cả bài viết ngắn',
  );

  $args = array(
    'labels'              => $labels,
    'public'              => true,
    'rewrite'             => array( 'slug' => 'short-post/%year%/%monthnum%/%day%' ),
    'menu_icon'           => 'dashicons-admin-post',
    'taxonomies'          => array( 'category', 'post_tag' ),
    'supports'            => array( 'title', 'editor', 'thumbnail', 'excerpt', 'trackbacks', 'custom-fields', 'comments', 'revisions', 'author', 'page-attributes', 'post-formats' ),
    'show_in_rest'        => true,
    'has_archive'         => true,
    'show_ui'             => true,
  );

  register_post_type( 'short-post', $args );
}

add_action( 'init', 'shortpost_create_post_type' );

function shortpost_custom_post_type_rewrite_rule() {
  add_rewrite_rule(
    '^short-post/(\d+)/(\d+)/(\d+)/([^/]+)/?$',
    'index.php?post_type=short-post&name=$matches[4]',
    'top'
  );
}

add_action( 'init', 'shortpost_custom_post_type_rewrite_rule', 10, 0 );

function shortpost_flush_rewrite_rules_on_activation() {
  shortpost_create_post_type();
  flush_rewrite_rules();
}

register_activation_hook( __FILE__, 'shortpost_flush_rewrite_rules_on_activation' );

function shortpost_flush_rewrite_rules_on_deactivation() {
  flush_rewrite_rules();
}

register_deactivation_hook( __FILE__, 'shortpost_flush_rewrite_rules_on_deactivation' );

function shortpost_custom_post_type_permalink($permalink, $post, $leavename) {
  if ($post->post_type == 'short-post') {
    $permalink = str_replace('%year%', date('Y', strtotime($post->post_date)), $permalink);
    $permalink = str_replace('%monthnum%', date('m', strtotime($post->post_date)), $permalink);
    $permalink = str_replace('%day%', date('d', strtotime($post->post_date)), $permalink);
    $permalink = str_replace('%postname%', $post->post_name, $permalink);
  }
  
  return $permalink;
}

add_filter( 'post_type_link', 'shortpost_custom_post_type_permalink', 10, 3 );

?>

Tích hợp vào WordPress

Chèn vô tập tin functions.php của theme đang sử dụng.

Khuyến nghị, nên chèn vào đoạn code này vào functions.php của giao diện con (child theme), thay vì giao diện gốc (parent theme).

Trong trường hợp theme mà bạn đang sử dụng không có child theme đi cùng, bạn có thể tạo ra bằng cách thủ công. Việc tạo một giao diện con trong WordPress thật sự không quá khó.

Truy cập vào Appearance → Theme File Editor, sau đó chọn giao diện (nên sử dụng giao diện con – child theme) đang sử dụng. Lưu ý, nếu website WordPress của bạn là multisite thì phải truy cập vào Network Admin → Themes → Theme File Editor.

Sau đó, hãy bổ sung đoạn code tạo post type trên vào tập tin functions.php của giao diện.

Tạo một post type trong WordPress - Thêm code tạo post type vào functions.php.
Thêm code tạo post type vào functions.php.

Kiểm tra tính năng đã hoạt động hay chưa?

Nếu bạn thấy một mục mới xuất hiện trong menu quản trị của WordPress, điều đó chứng tỏ việc tạo post type đã thành công.

Mục “Bài viết ngắn” xuất hiện trên menu quản trị của WordPress.

Lời kết

Tóm lại, cách này cho phép bạn tạo một loại bài viết mới trong WordPress mà không cần sử dụng Plugin. Tuy nhiên, nó có thể phức tạp hơn so với việc sử dụng một Plugin hỗ trợ. Lựa chọn cuối cùng là tùy thuộc vào bạn!

Được phân loại:

Được gắn thẻ:

,