Hàm lấy được Post Type trong khi query get_post_type()

7th Jul 2021
Table of contents

Đó là hàm get_post_type()

Ví dụ

/**
 * Setup query to show the ‘services’ post type with ‘8’ posts.
 * Output the title with an excerpt.
 */
    $args = array(  
        'post_type' => 'services',
        'post_status' => 'publish',
        'posts_per_page' => 8, 
        'orderby’ => 'title', 
        'order’ => 'ASC', 
    );

    $loop = new WP_Query( $args ); 
        
    while ( $loop->have_posts() ) : $loop->the_post(); 

        print the_title(); 

        the_excerpt(); 

        echo get_post_type();

    endwhile;

    wp_reset_postdata(); 

A More Advanced Example

/**
 * Setup query to show the ‘services’ post type with all posts filtered by 'home' category.
 * Output is linked title with featured image and excerpt.
 */
   
    $args = array(  
        'post_type' => 'services',
        'post_status' => 'publish',
        'posts_per_page' => -1, 
        'orderby' => 'title', 
        'order' => 'ASC',
        'cat' => 'home',
    );

    $loop = new WP_Query( $args ); 
        
    while ( $loop->have_posts() ) : $loop->the_post(); 
        $featured_img = wp_get_attachment_image_src( $post->ID );
        print the_title();
        if ( $feature_img ) {
           
        }
        the_excerpt(); 
    endwhile;

    wp_reset_postdata(); 

Parameters

There are many parameters you can use to customize the requested posts with WP_Query. We cover some of the commonly used parameters below but for a more thorough list, take a look at the class reference WordPress Codex on parameters.

  • cat - filters posts by a specific category id
  • tag - filters posts by a specific tag slug
  • tax_query - filters posts by specific taxonomy parameters
  • s - filters posts by a search keyword
  • Author - filters posts by a particular author

Template Tags

>> Manhattan Beach, California - US

>> Hướng dẫn Load More Posts with AJAX. Step by Step Tutorial

>> Pacific Surfliner, California - US

>> Vienna, Austria

Within your custom post type loop, there are many Template Tags you can use inside the loop to output information dynamically. Some examples of other template tags you can use inside your loop:

  • the_content() - Displays the content of a post
  • the_permalink() - Displays URL of a post
  • get_the_ID() - Retrieves the ID of a post
  • home_url() - Retrieves the home URL

Now that you understand the basics of WP_Query and requesting and fetching your custom post type, you can use what you’ve learned to develop your own custom post type templates with an array of different parameters and template tags to display your custom posts easily to visitors.

Bạn thấy bài viết này như thế nào?
0 reactions

Add new comment

Image CAPTCHA
Enter the characters shown in the image.
Câu nói tâm đắc: “Điều tuyệt với nhất trong cuộc sống là làm được những việc mà người khác tin là không thể!”

Related Articles

Nếu bạn muốn liệt kê tất cả các danh mục có sẵn cho một loại bài đăng tùy chỉnh, đoạn mã này có thể giúp bạn.

WP_Query là một lớp mạnh mẽ và cung cấp nhiều bộ lọc và hành động mà bạn có thể sử dụng để thay đổi cách vòng lặp WordPress hiển thị dữ liệu và cách truy vấn truy xuất dữ liệu.

If you want to build a simple, similar post list – in a single page view – when you query the latest or random posts from a post type you can use WP_Query.