Home > CMS Tutorial > WordPress > body text

How to use the function query posts in wordpress

藏色散人
Release: 2020-01-02 09:41:43
Original
2397 people have browsed it

How to use the function query posts in wordpress

#How to use the query posts function in wordpress?

query posts is a very easy-to-use function for calling articles. It can display a variety of articles of a specific range on the same page. The following is a detailed introduction to the usage of powerful query posts in WordPress. Friends who like it can refer to

Recommendation: "wordpress tutorial"

query posts is a very easy to use function to call articles, you can do Display multiple specific ranges of articles on the same page. For example, you can call a list of articles in different ranges such as a certain category, tag, date, and author. These article lists can greatly enrich the content of WordPress pages and benefit SEO. Second-hand scientists have sorted out the functions used by query posts to call articles. They are explained below.

First of all, the general way of writing query posts. Usually, the query is defined first, then the article looping code is added, and then the query is reset.

The code is as follows:

 
系列来显示相关的文章资讯–> 
 
 
Copy after login

All the functions below are based on the above framework. Just enter the corresponding parameters in query_posts(); to display the range of articles you want.

1. Category Parameters (article classification parameters)

cat - Enter the category number to display the articles in the category

category_name - Enter the category name to display the category Articles within

category__and – Displays articles that are included in multiple categories at the same time (only enter the category number to identify)

category__in – Displays articles within this category, but does not include subcategories Articles (only the category number can be entered to identify)

category__not_in - Except for articles in a certain category, articles in other categories and sub-categories are displayed (only the category number can be entered to identify)

The code is as follows:

 array(2,6))); 
//显示分类编号为6的文章(但不包括子分类文章) 
query_posts(array(‘category__in’ => array(6))); 
//除了分类编号为2及6的文章,子分类及其他分类文章都显示 
query_posts(array(‘category__not_in’ => array(2,6))); 
?>
Copy after login

2. Tag Parameters

tag - Enter the tag name to display the articles containing the tag

tag_id - Enter the tag number to display the articles containing the tag Article

tag__and – Displays articles that match multiple specific tags (limited to enter tag number to identify)

tag__in – Displays articles that match one of the specific tags (limited to enter tag number to identify) Identification)

tag__not_in – Articles with this tag will not be displayed (limited to entering the tag number to identify)

tag_slug__and – Displays articles that contain multiple specific tags (limited to entering the tag name) to identify)

tag_slug__in – Display articles that match one of the specific tags (limited to enter the tag name to identify)

The code is as follows:

 array(37,47)); 
//仅显示标签带有编号37或47的文章 
query_posts(array(‘tag__in’ => array(37,47)); 
//仅显示标签不带有编号37或47的文章 
query_posts(array(‘tag__not_in’ => array(37,47)); 
?>
Copy after login

3, Author Parameters (author Parameters)

author - Enter the author number to display the articles published by the author

author_name - Enter the author name to display the articles published by the author

The code is as follows:

Copy after login

4. Sticky Post Parameters (sticky post parameters)

The code is as follows:

get_option(‘sticky_posts’)) 
//将文章的置顶属性清除掉,以正常文章顺序排序(例如发表日期)显示出来 
caller_get_posts=1 
?>
Copy after login

Displays articles, but does not display sticky articles.

The code is as follows:

get_option(“sticky_posts”))); 
?>
Copy after login

Display articles with category number 6, display 3 articles per page, clear the top attribute of articles under this category, and sort in the normal order of articles (for example publication date) is displayed.

The code is as follows:

Copy after login

5. Post & Page Parameters (article & paging parameters)

The code is as follows:

 27 
//显示文章代称为about-my-life的文章 
‘name’ => ‘about-my-life’ 
//显示分页编号为7的分页 
‘page_id’ => 7 
//显示分页代称为about的分页 
‘pagename’ => ‘about’ 
//当文章超过5篇时就仅显示5篇文章并且搭配换页程式码显示换页连结,设为-1则不换页全部显示。 
‘posts_per_page’ => 5 
//当设定为6时就显示6篇文章,设为-1则显示范围内的全部文章。 
‘showposts’ => 6 
//仅显示文章编号为5,12,2,14,7的这5篇文章 
‘post__in’ => array(5,12,2,14,7) 
//仅显示文章编号不为5,12,2,14,7的其他全部文章 
‘post__not_in’ => array(6,2,8) 
//显示文章类型为分页的文章,预设值为post (文章),可以使用的数值有attachment(媒体档页面), page(分页), post(文章),或revision(修订)。 
‘post_type’ => ‘page’ 
//显示文章状态为公开性质的文章,可以使用的数值有pending(审核中), draft(草稿), future(排程), private(私人), trash(垃圾) 。 
‘post_status’ => ‘publish’ 
//显示文章范围内的第93页 
‘post_parent’ => 93 
?>
Copy after login

6. Time Parameters (time Parameters)

Display the list of articles published on December 20.

The code is as follows:

Copy after login

Displays the list of articles published this week.

The code is as follows:

Copy after login

Displays a list of articles published in the last 30 days.

The code is as follows:

 ‘” . date(‘Ym-d’, strtotime(‘-30 days’)) . “‘”; 
return $where; 
} 
add_filter(‘posts_where’, ‘filter_where’); 
query_posts($query_string); 
?>
 
Copy after login

7. Orderby Parameters (arrangement order parameters)

Copy after login

8. Pagination Parameters (pagination parameters)

The code is as follows:

Copy after login

9. Combination application example

Displays articles with category number 3 and published in 2004.

The code is as follows:

Copy after login

Displays articles with category numbers 1 and 3 and two articles per page, arranged in reverse order by title.

array(1,3),‘posts_per_page’=>2,‘orderby’=>title,‘order’=>DESC)); 
?>
Copy after login

is only displayed on the homepage, and the article is published in the month with category number 13.

Copy after login

Display articles with category number 1 and tag apples.

Copy after login

The above is the detailed content of How to use the function query posts in wordpress. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!