Sử dụng get_posts lấy một mảng các bài viết dựa trên các thông tin cho trước. Tạo ra danh sách bài viết để hiển thị.
Các thông số của get_posts tương tự như của get_pages nhưng được thực hiện khá khác nhau, và nên được sử dụng trong các tình huống thích hợp. get_posts sử dụng WP_Query, trong khi get_pages truy vấn cơ sở dữ liệu trực tiếp hơn. Mỗi thông số phản ánh sự khác biệt trong việc thực hiện.
<?php $args = array( 'posts_per_page' => 5, // số bài viết 'offset' => 0, 'category' => '', // danh mục 'category_name' => '', // tên danh mục 'orderby' => 'date', // thứ tự 'order' => 'DESC', // sắp xếp 'include' => '', 'exclude' => '', 'meta_key' => '', 'meta_value' => '', 'post_type' => 'post', // dạng post 'post_mime_type' => '', 'post_parent' => '', 'post_status' => 'publish', //trạng thái 'suppress_filters' => true ); $posts_array = get_posts( $args ); ?>
Tạo danh sách bài viết
<ul> <?php $args = array( 'posts_per_page' => 5, 'offset'=> 1, 'category' => 1 ); $myposts = get_posts( $args ); foreach ( $myposts as $post ) : setup_postdata( $post ); ?> <li> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> </li> <?php endforeach; wp_reset_postdata();?> </ul>
Lấy bài viết hiển thị bài viết trước và bài tiếp theo.
<?php
$postlist = get_posts( 'sort_column=menu_order&sort_order=asc' );
$posts = array();
foreach ( $postlist as $post ) {
$posts[] += $post->ID;
}
$current = array_search( get_the_ID(), $posts );
$prevID = $posts[$current-1];
$nextID = $posts[$current+1];
?>
<div class="navigation">
<?php if ( !empty( $prevID ) ): ?>
<div class="alignleft">
<a href="<?php echo get_permalink( $prevID ); ?>"
title="<?php echo get_the_title( $prevID ); ?>">Previous</a>
</div>
<?php endif;
if ( !empty( $nextID ) ): ?>
<div class="alignright">
<a href="<?php echo get_permalink( $nextID ); ?>"
title="<?php echo get_the_title( $nextID ); ?>">Next</a>
</div>
<?php endif; ?>
</div><!-- .navigation -->
Sắp xếp bài viết theo tên
<?php $args = array( 'posts_per_page' => 10, 'order'=> 'ASC', 'orderby' => 'title' ); $postslist = get_posts( $args ); foreach ( $postslist as $post ) : setup_postdata( $post ); ?> <div> <?php the_date(); ?> <br /> <?php the_title(); ?> <?php the_excerpt(); ?> </div> <?php endforeach; wp_reset_postdata(); ?>
Lấy bài viết ngẫu nhiên
<ul> <?php $args = array( 'posts_per_page' => 5, 'orderby' => 'rand' ); $rand_posts = get_posts( $args ); foreach ( $rand_posts as $post ) : setup_postdata( $post ); ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endforeach; wp_reset_postdata(); ?> </ul>
Lấy file
<?php
$args = array( 'post_type' => 'attachment', 'posts_per_page' => -1, 'post_status' => 'any', 'post_parent' => null );
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $post ) {
setup_postdata( $post );
the_title();
the_attachment_link( $post->ID, false );
the_excerpt();
}
wp_reset_postdata();
}
?>
Hiễn thị file trong bài viết
</pre>
<pre><?php
$args = array( 'post_type' => 'attachment', 'posts_per_page' => -1, 'post_status' =>'any', 'post_parent' => $post->ID );
$attachments = get_posts( $args );
if ( $attachments ) {
foreach ( $attachments as $attachment ) {
echo apply_filters( 'the_title' , $attachment->post_title );
the_attachment_link( $attachment->ID , false );
}
}
?>
Lấy bài viết dựa vào slug
<?php
$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 ) {
echo 'ID on the first post found ' . $my_posts[0]->ID;
}
?>
Lấy bài viết dựa vào loại danh mục
ở đây thể loại jazz
<?php $args = array( 'posts_per_page' => 8, 'orderby' => 'rand', 'post_type' => 'albums', 'genre' => 'jazz', 'post_status' => 'publish' ); $show_albums = get_posts( $args ); ?>
Lấy bài viết có lọc theo custom field
$args = array( 'post_type' => 'product', 'meta_query' => array( array( 'key' => 'featured', 'value' => 'yes', ) ) ); $postslist = get_posts( $args );
“Thanks again for the blog article.Really looking forward to read more. Want more.”