Home  >  Article  >  CMS Tutorial  >  How to use the function query posts in wordpress

How to use the function query posts in wordpress

藏色散人
藏色散人Original
2020-01-02 09:41:432495browse

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:

<?php 
//定义要显示的文章范围查询 
query_posts(); 
//文章回圈 
if ( have_posts() ) : while ( have_posts() ) : the_post(); 
?> 
<!–这边是当判断符合时列出的文章清单,你可以用< ?php the_xxx(); >系列来显示相关的文章资讯–> 
<?php 
endwhile; else: 
?> 
<!–这边是显示抓无资料时要跑出来的错误讯息–> 
<?php 
endif; 
//重置查询(这是为了避免之后的查询资料因为上面这段查询而混乱掉) 
wp_reset_query(); 
?>

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:

<?php 
//仅显示分类编号为4的文章(包括子分类文章) 
query_posts(‘cat=4′); 
//仅显示分类名称为Codex的文章(包括子分类文章) 
query_posts(‘category_name=Codex ‘); 
//显示多个分类内的文章(包括各子分类文章) 
query_posts(‘cat=2,6,17,38′); 
//除了分类编号为3的文章(包括子分类文章),其他文章都显示 
query_posts(‘cat=-3′); 
//显示同时有分类编号为2及6的文章 
query_posts(array(‘category__and’ => array(2,6))); 
//显示分类编号为6的文章(但不包括子分类文章) 
query_posts(array(‘category__in’ => array(6))); 
//除了分类编号为2及6的文章,子分类及其他分类文章都显示 
query_posts(array(‘category__not_in’ => array(2,6))); 
?>

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:

<?php 
//仅显示标签带有cooking的文章 
query_posts(‘tag=cooking’); 
//仅显示标签带有bread或baking的文章 
query_posts(‘tag=bread,baking’); 
//仅显示标签带有bread及baking及recipe的文章 
query_posts(‘tag=bread+baking+recipe’); 
//仅显示标签带有编号37及47的文章 
query_posts(array(‘tag__and’ => array(37,47)); 
//仅显示标签带有编号37或47的文章 
query_posts(array(‘tag__in’ => array(37,47)); 
//仅显示标签不带有编号37或47的文章 
query_posts(array(‘tag__not_in’ => array(37,47)); 
?>

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:

<?php 
//仅显示作者编号为3的文章 
query_posts(‘author=3′); 
//仅显示作者编号不为3的文章 
query_posts(‘author=-3′); 
//仅显示作者名称为Harriet的文章 
query_posts(‘author_name=Harriet’); 
?>

4. Sticky Post Parameters (sticky post parameters)

The code is as follows:

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

Displays articles, but does not display sticky articles.

The code is as follows:

<?php 
query_posts(array(“post__not_in” =>get_option(“sticky_posts”))); 
?>

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:

<?php 
query_posts(‘cat=6&posts_per_page=3&caller_get_posts=1′); 
?>

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

The code is as follows:

<?php 
//显示文章编号为27的文章 
‘p’ => 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 
?>

6. Time Parameters (time Parameters)

Display the list of articles published on December 20.

The code is as follows:

<?php 
query_posts(‘monthnum=12&day=20′); 
?>

Displays the list of articles published this week.

The code is as follows:

<?php 
$week = date(‘W’); 
$year = date(‘Y’); 
query_posts(‘year=’ . $year .‘&w=’ .$week ); 
?>

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

The code is as follows:

<pre name="code" class="php"><?php 
function filter_where($where = ”) { 
$where .= ” AND post_date > ‘” . date(‘Ym-d’, strtotime(‘-30 days’)) . “‘”; 
return $where; 
} 
add_filter(‘posts_where’, ‘filter_where’); 
query_posts($query_string); 
?>
 

7. Orderby Parameters (arrangement order parameters)

<?php 
//依照发表作者排列 
orderby=author 
//依照日期排列 
orderby=date 
//依照标题排列 
orderby=title 
//依照最后编辑时间排列 
orderby=modified 
//依照分页顺序排列(仅适用于分页) 
orderby=menu_order 
// (不知道XD…) 
orderby=parent 
//依照文章编号排列 
orderby=ID 
//随机排列 
orderby=rand 
//依照自订栏位数值排列 
orderby=meta_value 
//依照预设排列 
orderby=none 
//依照回响数排列 
orderby=comment_count 
?>

8. Pagination Parameters (pagination parameters)

The code is as follows:

<?php 
//当值设定true时则为不分页显示,直接显示全部文章 
nopaging=true 
//显示每页文章显示10篇 
posts_per_page=10 
//页数,例如当设定为6时则就表示跳到第6页 
paged=6 
//排列顺序,ASC为按时间顺序排列文章,若是DESC则是反向显示文章 
order=ASC 
?>

9. Combination application example

Displays articles with category number 3 and published in 2004.

The code is as follows:

<?php 
query_posts(‘cat=3&year=2004′); 
?>

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

<?php 
query_posts(array(‘category__and’=>array(1,3),‘posts_per_page’=>2,‘orderby’=>title,‘order’=>DESC)); 
?>

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

<?php 
if (is_home()) { 
query_posts($query_string . ‘&cat=13&monthnum=’ . date(‘n’,current_time(‘timestamp’))); 
} 
?>

Display articles with category number 1 and tag apples.

<?php 
query_posts(‘cat=1&tag=apples’); 
?>

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!

Statement:
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