<?php if( $posts ) : ?>,wordpress函数get_posts()用法示例

Skip to note content

You must log in to vote on the helpfulness of this noteVote results for this note: 4You must log in to vote on the helpfulness of this note

Contributed by Nicola Mustone

Example to get the latest 10 posts in the blog:

$args = array(

'numberposts' => 10

);

$latest_posts = get_posts( $args );

You can also pass the post_type argument if you want to get posts from a Custom Post Type, like:

$args = array(

'numberposts' => 10,

'post_type' => 'book'

);

$latest_books = get_posts( $args );

Access all post data

Some post-相关函数 data is not available to get_posts by default, such as post content through the_content(), or the numeric ID. This is resolved by calling an internal function setup_postdata(), with the $post array as its argument:

$lastposts = get_posts( array(

'posts_per_page' => 3

) );

if ( $lastposts ) {

foreach ( $lastposts as $post ) :

setup_postdata( $post ); ?>

<?php the_title(); ?>

endforeach;

wp_reset_postdata();

}

To access a post’s ID or content without calling setup_postdata(), or in fact any post-specific data (data retained in the posts table), you can use $post->COLUMN, where COLUMN is the table column name for the data. So $post->ID holds the ID, $post->post_content the content, and so on. To display or print this data on your page use the PHP echo command, like so:

<?php echo $post->ID; ?>

Custom Field Parameters

Show posts associated with a certain custom field. Following example displays posts from the ‘product’ post type that have meta key ‘featured’ with value ‘yes’, using ‘meta_query’:

$args = array(

'post_type' => 'product',

'meta_query' => array(

array(

'key' => 'featured',

'value' => 'yes',

)

)

);

$postslist = get_posts( $args );

Refer to the custom fields parameters section of the WP_Query documentation for more examples.

Posts with Previous Next Navigation

You can also using the custom queries to make the post with Previous and Next Post Navigation. Here is the following method to make it workable.

$post_list = get_posts( array(

'orderby' => 'menu_order',

'sort_order' => 'asc'

) );

$posts = array();

foreach ( $post_list as $post ) {

$posts[] += $post->ID;

}

$current = array_search( get_the_ID(), $posts );

$prevID = $posts[ $current-1 ];

$nextID = $posts[ $current+1 ];

?>

Reset after Postlists with offset

If you need after the loop, the post you had before joining the foreach, you can use this:

global $post;

$myposts = get_posts( array(

'posts_per_page' => 5,

'offset' => 1,

'category' => 1

) );

if ( $myposts ) {

foreach ( $myposts as $post ) :

setup_postdata( $post ); ?>

<?php the_title(); ?>

endforeach;

wp_reset_postdata();

}

?>

orderby also accepts the value post__in. (Note two underscores between post and in.) If you used include to retrieve specific posts, the posts will be supplied in the order you supplied to include. For example:

$posts = get_posts( array(

'include' => '3,8,1,17',

'post_type' => 'attachment',

'orderby' => 'post__in',

) );

You can check all the parameters that can be used on get_posts on https://codex.wordpress.org/Class_Reference/WP_Query#Parameters

Latest posts ordered by title

To show the last ten posts sorted alphabetically in ascending order, the following will display their post date, title and excerpt:

$postslist = get_posts( array(

'posts_per_page' => 10,

'order' => 'ASC',

'orderby' => 'title'

) );

if ( $postslist ) {

foreach ( $postslist as $post ) :

setup_postdata( $post );

?>

endforeach;

wp_reset_postdata();

}

Random posts

Display a list of 5 posts selected randomly by using the MySQL RAND() function for the orderby parameter value:

$rand_posts = get_posts( array(

'posts_per_page' => 5,

'orderby' => 'rand'

) );

if ( $rand_posts ) {

foreach ( $rand_posts as $post ) :

setup_postdata( $post );

?>

<?php the_title(); ?>

endforeach;

wp_reset_postdata();

}

?>

Show all attachments

Do this outside any Loops in your template.

$attachments = get_posts( array(

'post_type' => 'attachment',

'posts_per_page' => 500,

'post_status' => 'any',

'post_parent' => null

) );

if ( $attachments ) {

foreach ( $attachments as $post ) {

setup_postdata( $post );

the_title();

the_attachment_link( $post->ID, false );

the_excerpt();

}

wp_reset_postdata();

}

?>

Show attachments for the current post

Do this inside The Loop (where $post->ID is available).

$attachments = get_posts( array(

'post_type' => 'attachment',

'posts_per_page' => -1,

'post_status' => 'any',

'post_parent' => $post->ID

) );

if ( $attachments ) {

foreach ( $attachments as $attachment ) {

echo apply_filters( 'the_title' , $attachment->post_title );

the_attachment_link( $attachment->ID , false );

}

}

Get a post by its slug

Allows you to get a post ID by post slug.

$the_slug = 'my-slug';

$args=array(

'name' => $the_slug,

'post_type' => 'post',

'post_status' => 'publish',

'posts_per_page' => 1

);

$my_posts = get_posts( $args );

if ( $my_posts ) {

printf( __( 'ID on the first post found %s', 'textdomain' ), esc_html( $my_posts[0]->ID ) );

}

Taxonomy Parameters

Show posts associated with certain taxonomy. If specifying a taxonomy registered to a custom post type then instead of using ‘category’ you would use ‘{custom_taxonomy_name}’. For instance, if you had a custom taxonomy called “genre” and wanted to only show posts from the “jazz” genre you would use the below code.

$show_albums = get_posts( array(

'posts_per_page' => 8,

'orderby' => 'rand',

'post_type' => 'albums',

'genre' => 'jazz',

'post_status' => 'publish'

) );

Following example displays posts tagged with ‘jazz’, under ‘genre’ custom taxonomy, using ‘tax_query’:

$args = array(

'tax_query' => array(

array(

'taxonomy' => 'genre',

'field' => 'slug',

'terms' => 'jazz'

)

)

);

$postslist = get_posts( $args );

Refer to the taxonomy parameters section of the WP_Query documentation for more examples.

It is not considered an error condition if no posts are found based on the specified and default parameter values. Instead, an empty array (“array()“) is returned by the function.

Returns an array of WP_Post objects with attributes,

WP_Post Object

(

[ID] =>

[post_author] =>

[post_date] =>

[post_date_gmt] =>

[post_content] =>

[post_title] =>

[post_excerpt] =>

[post_status] =>

[comment_status] =>

[ping_status] =>

[post_password] =>

[post_name] =>

[to_ping] =>

[pinged] =>

[post_modified] =>

[post_modified_gmt] =>

[post_content_filtered] =>

[post_parent] =>

[guid] =>

[menu_order] =>

[post_type] =>

[post_mime_type] =>

[comment_count] =>

[filter] =>

)

Posts list with offset

If you have your blog configured to show just one post on the front page, but also want to list links to the previous five posts in category ID 1, you can use this:

$myposts = get_posts( $array(

'posts_per_page' => 5,

'offset' => 1,

'category' => 1

) );

if ( $myposts ) {

foreach ( $myposts as $post ) :

setup_postdata( $post );

?>

<?php the_title(); ?>

endforeach;

wp_reset_postdata();

}

?>

Note: With use of the offset, the above query should be used only on a category that has more than one post in it, otherwise there’ll be no output.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值