Hàm lấy được Post Type trong khi query get_post_type()
7th Jul 2021Đó 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
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