WordPress网站query_posts是用于从网站按照条件查询得到需要的结果,常用于WordPress 实现通过自定义字段查询。
query_posts()查询函数决定了哪些文章出现在WordPress 主 循环(loop)中,正因为如此,query_posts函数仅用于修改主页循环(Loop),而不是在页面上生成次级循环。
当我们做网站时,使用query_posts查询得到的结果很多的情况下就需要进行分页。实现query_posts查询结果分页的代码如下:
<?php
//分页
$paged = get_query_var(paged) ? get_query_var(paged) : 1;
//常规排序方法
$args=array(
post_type => post,
post_status=>publish,
cat => $cat, // 分类ID
meta_key => paixu,
orderby => meta_value_num,
order => ASC,
paged => $paged,
posts_per_page => $posts_per_page, // 显示篇数
);
//查询文章
$query = new WP_Query( $args );
while ($query->have_posts()) : $query->the_post(); ?>
<li class="clearfix"><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
在循环参数里添加paged=>get_query_var(paged),可以调用网站后台设置的每页显示条数,如果要分页还要进行以下的操作:
先将下的函数放到functions.php 里;
//分页
$indexlistid = page_id=.get_option(wpd_indexlist);
$searchlistid = page_id=.get_option(wpd_searchlist);
function lingfeng_custom_pagenavi( $custom_query,$range = 4 ) {
global $paged,$wp_query;
if ( !$max_page ) {
$max_page = $custom_query->max_num_pages;
}
if( $max_page >1 ) {
echo "<div class=pagination>";
if( !$paged ){
$paged = 1;
}
if( $paged != 1 ) {
echo "<a href=".str_replace(array($indexlistid,$searchlistid),,get_pagenum_link(1))." class=extend title=跳转到首页><<</a>";
}previous_posts_link(<span><</span>);
if ( $max_page >$range ) {
if( $paged <$range ) {
for( $i = 1; $i <= ($range +1); $i++ ) {
echo "<a href=".str_replace(array($indexlistid,$searchlistid),,str_replace(array($indexlistid,$searchlistid),,get_pagenum_link($i))) ."";
if($i==$paged) echo " class=current";echo ">$i</a>";
}
}elseif($paged >= ($max_page -ceil(($range/2)))){
for($i = $max_page -$range;$i <= $max_page;$i++){
echo "<a href=".str_replace(array($indexlistid,$searchlistid),,get_pagenum_link($i)) ."";
if($i==$paged)echo " class=current";echo ">$i</a>";
}
}elseif($paged >= $range &&$paged <($max_page -ceil(($range/2)))){
for($i = ($paged -ceil($range/2));$i <= ($paged +ceil(($range/2)));$i++){
echo "<a href=".str_replace(array($indexlistid,$searchlistid),,get_pagenum_link($i)) ."";if($i==$paged) echo " class=current";echo ">$i</a>";
}
}
}else{
for($i = 1;$i <= $max_page;$i++){
echo "<a href=".str_replace(array($indexlistid,$searchlistid),,get_pagenum_link($i)) ."";
if($i==$paged)echo " class=current";echo ">$i</a>";
}
}
next_posts_link(<span>></span>);
if($paged != $max_page){
echo "<a href=".str_replace(array($indexlistid,$searchlistid),,get_pagenum_link($max_page))." class=extend title=跳转到最后一页>>></a>";
}
echo <span>共[.$max_page.]页</span>;
echo "</div>\n";
}
}
然后在分页位置,使用下面的标签调用分页按钮。
<?php
//调用分页
lingfeng_custom_pagenavi($query);
// 重置请求数据
wp_reset_postdata();
?>
除了使用之外,还可以使用WP_Query函数查询。代码如下:
<?php $myqueryargss = array(
post_type => post,
posts_per_page => 9(每页的条数)
,
orderby=> date,
category_name=>promotion,
(分类名称)
order => ASC,
); ?>
<?php $myquerys= new WP_Query
( $myqueryargss );?>
<?php if ( $myquerys->have_posts() ): ?>
<?php while ( $myquerys->have_posts() ) : $myquerys->the_post(); ?>
<?php if ( $myquerys->current_post < 9) : ?>
<li
> <b
>·
</b
><a href
="<?php the_permalink(); ?>" target
="_blank"> <?php echo mb_strimwidth(get_the_title
(), 0, 26, ...); ?></a
></li
>
<?php endif; ?>
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_postdata
();?>
wp_query是一个wordpress用于复杂请求的的一个类,看到query懂开发的人就会反应这个是数据库查询的一个类,这个类可谓是特别有用的,可以帮助我们做很多复杂的查询。
wp_query的使用方法也很简单:
查询单个作者的文章
$query = new WP_Query( author=123 );
根据用户名查找
$query = new WP_Query( author_name=rami );
查询多个人的文章
$query = new WP_Query( author=2,6,17,38 );
查询不属于某个人的文章 可以通过减号“-”来排除某位作者。
$query = new WP_Query( author=-12 );
按分类ID
$query = new WP_Query( cat=4 );
查询某个分类下的文章(包含它的子分类)
$query = new WP_Query( category_name=staff );
查询某个分类下的文章(不包含它的子分类)
$query = new WP_Query( category__in=4 );
ID 类似的,查询多个分类下的文章
$query = new WP_Query( cat=2,6,17,38 );
slug 类似的,查询多个分类下的文章
$query = new WP_Query( category_name=staff,news );
不包含某个分类
$query = new WP_Query( cat=-12,-34,-56 );
查询同时属于多个分类的文章
$query = new WP_Query( array( category__and => array( 2, 6 ) ) ); // 2 and 6
$query = new WP_Query( array( category__in => array( 2, 6 ) ) ); // 2 or 6
通过标签查询
可通过标签查询的条件包括:tag, tag_id, tag__and, tag__in, tag__not_in, tag_slug__and, tag_slug__in。
对应到上面分类的查询方法,不难理解每个条件如何使用,我不一一举例了。(注意:名字中没有slug的,用tag的id查询)。
简单使用示例:
<?php
// 调用的分类4,可以修改分类id 显示的前两篇文章,可以修改显示篇数
$where = array(cat =>4,posts_per_page =>2,);
$the_query = new WP_Query($where);
// 开始循环
if ( $the_query->have_posts() ) {//如果找到了结果,便输出以下内容
echo <ul>;
while ( $the_query->have_posts() ) {//再次判断是否有结果
$the_query->the_post();//不用问为什么,每次都要写这个;
?>
<li><a href="<?php the_permalink();?>"><?php the_title();?></a></li>
<?php
}
echo </ul>;
} else {
// 如果没有找到任意结果,就输出这个
}
wp_reset_postdata();//不用问为什么,每次都记得写就好
?>
简单示例:
<?php
$args = array(
// 用于查询的参数或者参数集合
);
// 自定义查询
$the_query = new WP_Query( $args );
// 判断查询的结果,检查是否有文章
if ( $the_query->have_posts() ) :
// 通过查询的结果,开始主循环
while ( $the_query->have_posts() ) :
$the_query->the_post(); //获取到特定的文章
// 要输出的内容,如标题、日期等
endwhile;
endif;
// 重置请求数据
wp_reset_postdata();
?>
wp_query常用的一些参数:
作者参数
author => 1,2,3,, //(int) - use author id [use minus (-) to exclude authors by ID ex. author => -1,-2,-3,]
author_name => luetkemj, //(string) - use user_nicename (NOT name)
author__in => array( 2, 6 ), //(array) - use author id (available with Version 3.7).
author__not_in => array( 2, 6 ), //(array) - use author id (available with Version 3.7).
分类参数
cat => 5,//(int) - use category id.
category_name => staff, news, //(string) - Display posts that have these categories, using category slug.
category_name => staff+news, //(string) - Display posts that have "all" of these categories, using category slug.
category__and => array( 2, 6 ), //(array) - use category id.
category__in => array( 2, 6 ), //(array) - use category id.
category__not_in => array( 2, 6 ), //(array) - use category id.
标签参数
tag => cooking, //(string) - use tag slug.
tag_id => 5, //(int) - use tag id.
tag__and => array( 2, 6), //(array) - use tag ids.
tag__in => array( 2, 6), //(array) - use tag ids.
tag__not_in => array( 2, 6), //(array) - use tag ids.
tag_slug__and => array( red, blue), //(array) - use tag slugs.
tag_slug__in => array( red, blue), //(array) - use tag slugs.
分类参数(自定义分类法)
tax_query => array( //(array) - use taxonomy parameters (available with Version 3.1).
relation => AND, //(string) - Possible values are AND or OR and is the equivalent of running a JOIN for each taxonomy
array(
taxonomy => color, //(string) - Taxonomy.
field => slug, //(string) - Select taxonomy term by (id or slug)
terms => array( red, blue ), //(int/string/array) - Taxonomy term(s).
include_children => true, //(bool) - Whether or not to include children for hierarchical taxonomies. Defaults to true.
operator => IN //(string) - Operator to test. Possible values are IN, NOT IN, AND.
),
array(
taxonomy => actor,
field => id,
terms => array( 103, 115, 206 ),
include_children => false,
operator => NOT IN
)
),
文章和页面参数
name => hello-world, //(string) - use post slug.
page_id => 1, //(int) - use page id.
pagename => sample-page, //(string) - use page slug.
pagename => contact_us/canada, //(string) - Display child page using the slug of the parent and the child page, separated ba slash
post_parent => 1, //(int) - use page id. Return just the child Pages. (Only works with heirachical post types.)
post_parent__in => array(1,2,3) //(array) - use post ids. Specify posts whose parent is in an array. NOTE: Introduced in 3.6
post_parent__not_in => array(1,2,3), //(array) - use post ids. Specify posts whose parent is not in an array.
post__in => array(1,2,3), //(array) - use post ids. Specify posts to retrieve. ATTENTION If you use sticky posts, they will be included (prepended!) in the posts you retrieve whether you want it or not. To suppress this behaviour use ignore_sticky_posts
post__not_in => array(1,2,3), //(array) - use post ids. Specify post NOT to retrieve.
查询设置了密码的文章
has_password => true, //(bool) - available with Version 3.9
post_password => multi-pass, //(string) - show posts with a particular password (available with Version 3.9)
类型状态参数
post_type => array( //(string / array) - use post types. Retrieves posts by Post Types, default value is post;
post, // - a post.
page, // - a page.
revision, // - a revision.
attachment, // - an attachment. The default WP_Query sets post_status=>published, but atchments default to post_status=>inherit so youll need to set the status to inherit or any.
my-post-type, // - Custom Post Types (e.g. movies)
),
post_type => any, // - retrieves any type except revisions and types with exclude_from_search set to true.
post_status => array( //(string / array) - use post status. Retrieves posts by Post Status, default value ipublish.
publish, // - a published post or page.
pending, // - post is pending review.
draft, // - a post in draft status.
auto-draft, // - a newly created post, with no content.
future, // - a post to publish in the future.
private, // - not visible to users who are not logged in.
inherit, // - a revision. see get_children.
trash // - post is in trashbin (available with Version 2.9).
),
post_status => any, // - retrieves any status except those from post types with exclude_from_search set to true.