Sử Dụng
<?php wp_get_recent_posts( $args, $output ) ?>
Sử Dụng Mặc định
<?php $args = array(
'numberposts' => 10,
'offset' => 0,
'category' => 0,
'orderby' => 'post_date',
'order' => 'DESC',
'include' => ,
'exclude' => ,
'meta_key' => ,
'meta_value' =>,
'post_type' => 'post',
'post_status' => 'draft, publish, future, pending, private',
'suppress_filters' => true );
$recent_posts = wp_get_recent_posts( $args, ARRAY_A );?>
Trở Về giá trị Tăng
- $posts (array)
- Danh Sách mảng bài viết (mặc định) hay 1 đối tượng trên $output
Thông Số
- $args
- (array) (optional)
- Default: array
- $output
- (string) (optional) Constant OBJECT, ARRAY_A
- Default: ARRAY_A
Ví Dụ
Ví dụ sử dụng hàm wp_get_recent_posts() function xuất ra danh sách 10 posts.
<h2>Recent Posts</h2>
<ul>
<?php
$recent_posts = wp_get_recent_posts();
foreach( $recent_posts as $recent ){
echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . $recent["post_title"].'</a> </li> ';
}
?>
</ul>
Nếu bạn muốn phân định ít nhiều bài viết gần đây bạn phải đặt số lượng trong tham số chức năng như ví dụ dưới đây:
<h2>Recent Posts</h2>
<ul>
<?php
$args = array( 'numberposts' => '5' );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . $recent["post_title"].'</a> </li> ';
}
?>
</ul>
To exclude posts with a certain post format, you can use Class_Reference/WP_Query#Taxonomy_Parameters like this next example, which excludes all posts with the 'aside' and 'image' formats:
<h2>Recent Posts</h2>
<ul>
<?php
$args = array( 'numberposts' => '5', 'tax_query' => array(
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => 'post-format-aside',
'operator' => 'NOT IN'
),
array(
'taxonomy' => 'post_format',
'field' => 'slug',
'terms' => 'post-format-image',
'operator' => 'NOT IN'
)
) );
$recent_posts = wp_get_recent_posts( $args );
foreach( $recent_posts as $recent ){
echo '<li><a href="' . get_permalink($recent["ID"]) . '" title="Look '.esc_attr($recent["post_title"]).'" >' . $recent["post_title"].'</a> </li> ';
}
?>
</ul>
Ghi CHú
- sử dụng: wp_parse_args()
- sử dụng : get_posts()
- Only the value of ARRAY_A is checked for $output. Any other value or constant passed will return an array of objects.
- This function returns posts in an associative array (ARRAY_A) format which is compatible with WordPress versions below 3.1. To get output similar to get_posts(), use OBJECT as the second parameter: wp_get_recent_posts( $args, OBJECT );
Phiên Bản
Mã Nguồn FILE
Nguồn wordoress.com
0 nhận xét :