wordpress强大的get_posts
和WP_Query
以及query_posts
函数都可以按照特定的条件输出文章,在主循环中使用query_posts
会影响到主循环,所以用get_posts
和WP_Query
输出特定文章,尤其是在CMS主题当中最好不过了。
其中他们通用的参数有
<code>
$args = array(
'posts_per_page' => 5,
'offset' => 0,
'category' => '',
'orderby' => 'post_date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'post_status' => 'publish',
'suppress_filters' => true );
</code>
$args = array(
'posts_per_page' => 5,
'offset' => 0,
'category' => '',
'orderby' => 'post_date',
'order' => 'DESC',
'include' => '',
'exclude' => '',
'meta_key' => '',
'meta_value' => '',
'post_type' => 'post',
'post_mime_type' => '',
'post_parent' => '',
'post_status' => 'publish',
'suppress_filters' => true );
</code>
加入我们想输出阅读量最高的文章,由于viwes被保存在post_mtea字段,所以用SQL查询出文章ID,然后输出文章相关信息这样操作麻烦,所以我们用的是meta_key
和orderby
这个组合 orderby
中有一个值是meta_value_num
,这样我们就可以进行输出了,现在我们要输出阅读量最高的文章,其代码大致如下。
<code>
<?php //Get the most viwes posts
global $posts,$post;
$args = array(
'posts_per_page' => 5,//输出数量5
'meta_key' => 'viwes',
'orderby' => 'meta_value_num'
);
$postslist = get_posts( $args );
foreach ( $postslist as $post ) :
setup_postdata( $post ); ?>
<li class="clearfix">><a rel="bookmark" class="title" href="<?php the_permalink();?>"><?php the_title();?></a><small><?php echo get_post_meta($post->ID,'viwes',true)?> 人喜欢</small></li>
<?php endforeach;
wp_reset_postdata();?>
</code>
<?php //Get the most viwes posts
global $posts,$post;
$args = array(
'posts_per_page' => 5,//输出数量5
'meta_key' => 'viwes',
'orderby' => 'meta_value_num'
);
$postslist = get_posts( $args );
foreach ( $postslist as $post ) :
setup_postdata( $post ); ?>
<li class="clearfix">><a rel="bookmark" class="title" href="<?php the_permalink();?>"><?php the_title();?></a><small><?php echo get_post_meta($post->ID,'viwes',true)?> 人喜欢</small></li>
<?php endforeach;
wp_reset_postdata();?>
</code>
还值得一提的是date_query参数,调取一定时间范围的文章,非常方便,给出一些例子.
<code>
// Get the 10 most recent posts made
// between 9AM and 5PM on weekdays
$some_posts = new WP_Query( array(
'date_query' => array(
array(
'hour' => 9,
'compare' => '>=',
),
array(
'hour' => 17,
'compare' => '<=',
),
array(
'dayofweek' => array( 2, 6 ),
'compare' => 'BETWEEN',
),
),
'posts_per_page' => 10,
) );
// Get all posts from this summer
// June 1st to August 31st, inclusive
// Note that strtotime()-compatible strings can be used
$some_posts = new WP_Query( array(
'date_query' => array(
array(
// String via strtotime()
'after' => 'June 1st, 2013',
// Or if you want, an array
'before' => array(
'year' => 2013,
'month' => 8,
'day' => 31,
),
'inclusive' => true,
),
),
'posts_per_page' => -1,
) );
// Any posts made over a year ago
// but modified in the past month
$some_posts = new WP_Query( array(
'date_query' => array(
array(
'column' => 'post_date_gmt',
'before' => '1 year ago',
),
array(
'column' => 'post_modified_gmt',
'after' => '1 month ago',
)
),
'posts_per_page' => -1,
) );</code>
// Get the 10 most recent posts made
// between 9AM and 5PM on weekdays
$some_posts = new WP_Query( array(
'date_query' => array(
array(
'hour' => 9,
'compare' => '>=',
),
array(
'hour' => 17,
'compare' => '<=',
),
array(
'dayofweek' => array( 2, 6 ),
'compare' => 'BETWEEN',
),
),
'posts_per_page' => 10,
) );
// Get all posts from this summer
// June 1st to August 31st, inclusive
// Note that strtotime()-compatible strings can be used
$some_posts = new WP_Query( array(
'date_query' => array(
array(
// String via strtotime()
'after' => 'June 1st, 2013',
// Or if you want, an array
'before' => array(
'year' => 2013,
'month' => 8,
'day' => 31,
),
'inclusive' => true,
),
),
'posts_per_page' => -1,
) );
// Any posts made over a year ago
// but modified in the past month
$some_posts = new WP_Query( array(
'date_query' => array(
array(
'column' => 'post_date_gmt',
'before' => '1 year ago',
),
array(
'column' => 'post_modified_gmt',
'after' => '1 month ago',
)
),
'posts_per_page' => -1,
) );</code>
这个参数在get_comments中也同样有效
<code>
//All comments from post ID 123
// that are within the past week
$some_comments = get_comments( array(
'post_ID' => 123,
'date_query' => array(
array(
'after' => '1 week ago',
),
),
) );</code>
//All comments from post ID 123
// that are within the past week
$some_comments = get_comments( array(
'post_ID' => 123,
'date_query' => array(
array(
'after' => '1 week ago',
),
),
) );</code>
所有参数
<code>
'date_query' => array(
'column' => 'optional, column to query against, default is post_date',
'compare' => 'optional, see WP_Date_Query::get_compare()',
'relation' => 'optional, OR or AND, how the sub-arrays should be compared, default is AND',
array(
'column' => 'see above',
'compare' => 'see above',
'after' => 'string or array, see WP_Date_Query::build_mysql_datetime()',
'before' => 'string or array, see WP_Date_Query::build_mysql_datetime()',
'inclusive' => 'boolean, for after/before, whether exact value should be matched or not',
'year' => '4 digit int',
'month' => 'int, 1-12',
'week' => 'int, 0-53',
'day' => 'int, 1-31',
'hour' => 'int, 0-23',
'minute' => 'int, 0-60',
'second' => 'int, 0-60',
),
array(
...
),
..
),</code>
'date_query' => array(
'column' => 'optional, column to query against, default is post_date',
'compare' => 'optional, see WP_Date_Query::get_compare()',
'relation' => 'optional, OR or AND, how the sub-arrays should be compared, default is AND',
array(
'column' => 'see above',
'compare' => 'see above',
'after' => 'string or array, see WP_Date_Query::build_mysql_datetime()',
'before' => 'string or array, see WP_Date_Query::build_mysql_datetime()',
'inclusive' => 'boolean, for after/before, whether exact value should be matched or not',
'year' => '4 digit int',
'month' => 'int, 1-12',
'week' => 'int, 0-53',
'day' => 'int, 1-31',
'hour' => 'int, 0-23',
'minute' => 'int, 0-60',
'second' => 'int, 0-60',
),
array(
...
),
..
),</code>
文章转自大发利用get_posts获取特定文章 。