Trước hết thông tin được lấy từ The Loop, dưới đây là một số code Nguyễn Sơn thường dùng mình xin tổng hợp lại bao giờ cần copy cho nhanh đỡ phải code nhiều đau dầu.
The Loop mặc định trong Wordpreess
Mặc định thì Loop trong WordPress sẽ như thế này
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
//
// Post Content here
//
<?php endwhile; else : ?>
<p><?php esc_html_e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
Nhưng mình thích dùng kiểu này hơn:
<?php
// the query
$the_query = new WP_Query( 'posts_per_page=12' ); ?>
<ul>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail(); ?>
<p><?php the_title(); ?></p>
</a>
</li>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
</ul>
Thuộc tính WP_Query
array(
'post_type'=>'post',
'post_status'=>'publish',
'posts_per_page'=>-1
‘cat’ =>
)
Thuộc tính Post
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] =>
)