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

Written by admin
Posted on Wed, 07/07/2021 - 05:21
28 views

Share Everywhere

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.

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
8 + 6 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
Profile picture for user admin
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ể!”