Skip to content

Hi You, I am Tade

  • About
  • Donate
  • Privacy policy
    • About
    • Donate
    • Privacy policy

Lấy bài viết trong wordpress

  • Hoàng Thức
  • Apr 26, 2015

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 );

 

 

    Share

Post navigation

Next:→

46 thoughts on “Lấy bài viết trong wordpress”

  1. eebest8 says:
    8 July, 2016 at 10:02 am

    “Thanks again for the blog article.Really looking forward to read more. Want more.”

  2. Pingback: Thai Massage
  3. Pingback: ทัวร์เกาะหลีเป๊ะ
  4. Pingback: พูลวิลล่าพัทยา
  5. Pingback: ufazeed
  6. Pingback: เช่ารถตู้พร้อมคนขับ
  7. Pingback: shower room bangkok
  8. Pingback: Saphir Thailand
  9. Pingback: ตกแต่งภายในหาดใหญ่
  10. Pingback: รับสร้างบ้านเชียงใหม่
  11. Pingback: login dultogel
  12. Pingback: faw99
  13. Pingback: ปั่นสล็อตฟรี ครบทุกค่าย
  14. Pingback: เครื่องล้างจาน
  15. Pingback: ร้านเค้กวันเกิดใกล้ฉัน
  16. Pingback: สติ๊กเกอร์ไดคัท
  17. Pingback: rca77
  18. Pingback: ezybet
  19. Pingback: Jaxx Liberty
  20. Pingback: lucabet
  21. Pingback: สล็อตแตกง่าย
  22. Pingback: Merinque
  23. Pingback: PHUKET VILLA
  24. Pingback: โรงงานผลิตสายรัดพลาสติก
  25. Pingback: รับนำเข้าสินค้าจากจีน
  26. Pingback: ตรวจสลากกินแบ่งรัฐบาล
  27. Pingback: โคมไฟ
  28. Pingback: แทงบอลออนไลน์เกาหลี
  29. Pingback: visit site
  30. Pingback: kc9
  31. Pingback: https://bloglandcentral.preferati.net/land-university-blog/exploring-the-world-of-pocket-option-a/
  32. Pingback: nagatop situs scam
  33. Pingback: ยาแก้ร้อนใน
  34. Pingback: ของพรีเมี่ยม
  35. Pingback: โรงงานเฟอร์นิเจอร์จีน
  36. Pingback: รีวิวเกมสล็อต ค่าย PG SLOT
  37. Pingback: http://aviatorwebsiteindia.com/
  38. Pingback: เซรั่มหอมแดง
  39. Pingback: Thai restaurant Oregon
  40. Pingback: เว็บสล็อตใหม่ล่าสุด
  41. Pingback: พิมพ์โปสการ์ด
  42. Pingback: denmyl.com
  43. Pingback: เว็บพนันออนไลน์เว็บตรง
  44. Pingback: บ้านพักคนชรา
  45. Pingback: สั่งสินค้าจากจีน
  46. Pingback: สินค้าจีน

Comments are closed.

©2025 Hi You, I am Tade / Designed & Built by WPFound