Instructions for custom post type in wordpress from a to z

Tutorials 0 lượt xem

Today we will learn one of the features that make wordpress the most powerful CMS in the world, which is custom post type in wordpress.

So what is custom post type in wordpress ? Why is it important? How to use it but how? To answer the above questions we will learn today’s article.

What is custom post type in wordpress?

When installing wordpress, we will see that by default there is a post section, this section will manage all the articles of the website. For blogs or instant news websites but that’s enough…

But in case I want to make a sales website, I need to add an area to post products. So the website only has 1 article section as above is not enough, we need to create another area to post products.

Instructions for custom post type in wordpress from a to z

Creating an area to post products with the same function as the post is called a custom post type . And that new area is called the product post type.

And of course, wordpress already provides functions and tools for us to create 1 or more custom post types.

This feature has helped wordpress not only stop at a CMS to make a blog, but we can also do many other types of websites such as: Website sales, real estate, companies, services, management …

How to create custom post type in wordpress

To create a custom post type in wordpress we use the following code, insert this code into the functions.php file of the theme you are using.

Syntax

function tao_custom_post_type(){
    /*
     * Biến $label để chứa các text liên quan đến tên hiển thị của Post Type trong Admin
     */
    $label = array(
        'name' => 'Sản phẩm', //Tên post type dạng số nhiều
        'singular_name' => 'Sản phẩm' //Tên post type dạng số ít
    );
    /*
     * Biến $args là những tham số quan trọng trong Post Type
     */
    $args = array(
        'labels' => $label, //Gọi các label trong biến $label ở trên
        'description' => 'Post type đăng sản phẩm', //Mô tả của post type
        'supports' => array(
            'title',
            'editor',
            'excerpt',
            'author',
            'thumbnail',
            'comments',
            'trackbacks',
            'revisions',
            'custom-fields'
        ), //Các tính năng được hỗ trợ trong post type
        'taxonomies' => array( 'category', 'post_tag' ), //Các taxonomy được phép sử dụng để phân loại nội dung
        'hierarchical' => false, //Cho phép phân cấp, nếu là false thì post type này giống như Post, true thì giống như Page
        'public' => true, //Kích hoạt post type
        'show_ui' => true, //Hiển thị khung quản trị như Post/Page
        'show_in_menu' => true, //Hiển thị trên Admin Menu (tay trái)
        'show_in_nav_menus' => true, //Hiển thị trong Appearance -> Menus
        'show_in_admin_bar' => 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' => 'dashicons-cart', //Đường dẫn tới icon sẽ hiển thị
        'can_export' => true, //Có thể export nội dung bằng Tools -> Export
        'has_archive' => true, //Cho phép lưu trữ (month, date, year)
        'exclude_from_search' => false, //Loại bỏ khỏi kết quả tìm kiếm
        'publicly_queryable' => true, //Hiển thị các tham số trong query, phải đặt true
        'capability_type' => 'post' //
    );
    register_post_type('sanpham', $args); //Tạo post type với slug tên là sanpham và các tham số trong biến $args ở trên
}
add_action('init', 'tao_custom_post_type');
<div>
<span>1</span><span>2</span><span>3</span><span>4</span><span>5</span><span>6</span><span>7</span><span>8</span><span>9</span><span>10</span><span>11</span><span>12</span><span>13</span><span>14</span><span>15</span><span>16</span><span>17</span><span>18</span><span>19</span><span>20</span><span>21</span><span>22</span><span>23</span><span>24</span><span>25</span><span>26</span><span>27</span><span>28</span><span>29</span><span>30</span><span>31</span><span>32</span><span>33</span><span>34</span><span>35</span><span>36</span><span>37</span><span>38</span><span>39</span><span>40</span><span>41</span><span>42</span><span>43</span>
</div>

Some note:

  • The register_post_type function takes two values, the slug and the $args . parameter
  • The slug in the register_post_type function will be used to get the post type’s data to the frontend
  • In the $args parameter there is a ‘supports’ element, which are the bars that will be supported in the post type sanpham . If you don’t need to use them all, you can remove some elements to make our post type neater.
  • menu_icon is the display icon, when a new post type is registered, you can choose one of the icons here: List of menu icons in wordpress admin

After creating a successful custom post type, go to the admin and you will see a product menu as shown below.

Instructions for custom post type in wordpress from a to z

Create custom post type in wordpress with plugin:

Above is how to create a custom post type with code. WordPress has a lot of plugins that solve this problem for example

Display posts of custom post type in wordpress

We will use get post loop in wordpress to get posts of custom post type. Please use the code below.

Display syntax using New wp_query

<?php $args = array(
    'post_status' => 'publish',
    'posts_per_page' =&gt; -1,
    'post_type'      =&gt; 'sanpham'
  );
  $the_query = new WP_Query( $args );
?&gt;
<?php if( $the_query->have_posts() ): ?&gt;
<?php while( $the_query->have_posts() ) : $the_query-&gt;the_post(); ?&gt;
  // Thông tin cần lấy của 1 bài viết
<?php endwhile; ?><?php endif; ?><?php wp_reset_query(); ?><div>
<span>1</span><span>2</span><span>3</span><span>4</span><span>5</span><span>6</span><span>7</span><span>8</span><span>9</span><span>10</span><span>11</span><span>12</span><span>13</span><span>14</span>
</div>

As you have seen, compared to get regular posts get posts of post type only one change is: ‘post_type’ => ‘sanpham’ sanpham here is the slug of the post type that we declared in creating custom post type, everything else is similar to post.

Name the file to get the list of posts, the detail page of the custom post type:

To get a list of all posts in the newly created custom post type, we run the link: domain.com/sanpham => sanpham is the slug when registering the post type, when running that path they will call file: archive-sanpham. php

For detail page of custom post type it will receive file: single-sanpham.php

Summary:

So today I showed you how to create custom post types, this is one of the best features of wordpress that has made wordpress a powerful cms and used by many people.

The strong wordpress idea is that you can customize many things, making wp multipurpose. In the following articles, I will guide you to customize some other components of wordpress

Wish you success in learning wordpress theme programming , hello everyone!

Bài viết liên quan