Bài trước mình có giới thiệu cách lấy posts có điều kiện, sử dụng query_posts, get_posts,..
Bài hôm này mình sẽ hướng dẫn làm sao để trích xuất post. Chúng ta sử dụng vòng lặp áp dụng cho WP_Query, query_posts và sử dụng trực tiếp trong template page.
while(have_posts()): the_post(); //cấu hình post trước lúc sử dụng ... next_post(); //chuyển post sau. Note: các phiên bản wordpress mới không được dùng nữa. endwhile;
Post publish time
– Display post time.
while(have_posts()):the_post(); //Displays the time of the current post. the_time('l F d, Y'); //echo thời gian post //giống the_time, hàm này trả về chứ không echo. get_the_time(); /*format time*/ //Time as AM/PM VS. 24H formatTime posted:
//Displays the time using the 24 hours format parameter string 'G:i' (ex: 17:52).Time posted:
endwhile;
– Display post date.
/*post date*/ //get post date the_date();//echo get_the_date(); //return without echo //Date as Month Day, Year. ex: December 2, 2004, thay vì sử dụng the_date.//Displays the date and time.Posted: at
#return: Posted: July 17, 2007 at 7:19 am
Sau này là một số định hình ngày tháng (date format):
-
F j, Y g:i a
– November 6, 2010 12:50 am -
F j, Y
– November 6, 2010 -
F, Y
– November, 2010 -
g:i a
– 12:50 am -
g:i:s a
– 12:50:48 am -
l, F jS, Y
– Saturday, November 6th, 2010 -
M j, Y @ G:i
– Nov 6, 2010 @ 0:50 -
Y/m/d at g:i A
– 2010/11/06 at 12:50 AM -
Y/m/d at g:ia
– 2010/11/06 at 12:50am -
Y/m/d g:i:s A
– 2010/11/06 12:50:48 AM -
Y/m/d
– 2010/11/06
Kết hợp cả ngày và giờ vào hàm the_time
, vd:
Chi tiết: http://codex.wordpress.org/Formatting_Date_and_Time
Post content
while(have_posts()):the_post(); //global current post object $post; //post ID the_ID(); //post id, ie: 124 get_the_ID();//return post id without echo post_class(); // /*post title*/ //thường dùng trong thuộc tính alt="",ie: alt=" " the_title_attribute(); the_title_attribute('echo=0'); //return without echo //get post title the_title(); //post title get_the_title(); /*get post excerpt*/ the_excerpt(); //echo post excerpt get_the_excerpt(); //return post excerpt get_excerpt_by_id($post-> ID,30); //get post excerpt by id with max length. /*get post content*/ the_content(); //echo full post content get_the_content(); //return full post content endwhile;
Get post images (featured image/post thumbnails)
while(have_posts()):the_post(); //lấy post thumbnail (img tag) với kích cỡ thumbnail. Xem cách thay đổi kích thước thumbnail. echo get_the_post_thumbnail( get_the_ID(), 'thumbnail'); //chỉ định kích thước thumbnail bởi mảng (width,height), tham số 3 định vị thêm thuộc tính cho thẻ img. get_the_post_thumbnail($post-> ID,array(80,60),array('border'=> '0',..)); //get post thumbnail id get_post_thumbnail_id($post-> ID); endwhile;
Lấy chi tiết tính chất ảnh, tham số 2 định vị kích thước ảnh. Xem cú pháp hàm wp_get_attachment_image_src.
wp_get_attachment_image_src(get_post_thumbnail_id($post-> ID),array(80,60));
Hàm trả về mảng gồm 4 phần tử:
[0] => url
[1] => width
[2] => height
[3] => boolean: true if $url is a resized image, false if it is the original.
Lấy toàn album ảnh của post
$attachments=get_posts(array( 'post_type'=> 'attachment', 'post_parent'=> $post-> ID, 'numberposts'=> -1 )); if ( $attachments ) { foreach ( $attachments as $attachment ) { echo '
'; echo apply_filters( 'the_title', $attachment-> post_title ); echo '
Nhớ rằng trong ảnh là đối tượng có kiểu attachment, cũng giống post có title, ngoài ra có description.
attachment có filter ‘wp_get_attachment_image_attributes’ để sử lý tính chất của ảnh.
Post Custom field
Lấy custom post field.
//get specific field. echo get_post_meta($post-> ID,"field1",true); //get post meta fields $meta=get_post_custom($post-> ID); echo $meta['custom_field1'][0];
Get Post author
the_author_posts_link(); //echo author url tác giả của bài viết get_the_author(); //return post author name the_author(); //echo post author name
Display the link of the author page for the author of the current post.
Trong đó: get_the_author_meta(‘ID’); //trả về ID cua user bài viết
Get the post Category
– Hiển thị liên kết categories, mỗi category được ngăn cách bởi dấu “,”.
This post is in:
Separated by Arrow.
Categories:
Tương tự cho hàm get_the_category nhưng trả về giá trị.
Mặc định hiển thị child category, nếu muốn hiển thị multi-category thì tham khảo thêm: http://codex.wordpress.org/Function_Reference/the_category
– Liệt kê danh sách toàn bộ category thuộc sở hữu post.
echo get_the_category_list( ', ' ); //ie: category1,category2,category3,..
Get Post tags
//syntax: get_the_tag_list( $before, $sep, $after ); $tags_list=get_the_tag_list( '', ', ' ); echo $tags_list;
Lấy dữ liệu mảng tags của post.
name . ' '; } } ?>
Ý tưởng Sử dụng images tag, tên ảnh là chỉ số tag (term_id).
<?php $posttags = get_the_tags(); if ($posttags) { foreach($posttags as $tag) { echo 'term_id . '.jpg" alt="' . $tag-> name . '" /> '; } } ?>
Trên này là tổng hợp template tags để lấy dữ liệu post data. Mình xin hêt !
Bài viết Hướng dẫn dùng template tags trong loop được tổng hợp và biên tập bởi: dinhthuanit.com. Mọi ý kiến đóng góp và phản hồi vui lòng gửi Liên Hệ cho dinhthuanit.com để điều chỉnh. dinhthuanit.com xin cảm ơn.