Home WordpressWordPress cơ bản Hướng dẫn dùng template tags trong loop

Hướng dẫn dùng template tags trong loop

by admincp

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 khi sử dụng
      ...
      next_post();	//chuyển post sau. Note: các phiên bản wordpress mới không dùng nữa.
endwhile;

– 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 format
<p>Time posted: <?php the_time('g:i a'); ?></p>

//Displays the time using the 24 hours format parameter string 'G:i' (ex: 17:52).
<p>Time posted: <?php the_time('G:i'); ?></p>

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.
<div><?php the_time('F j, Y'); ?></div>

//Displays the date and time.
<p>Posted: <?php the_time('F j, Y'); ?> at <?php the_time('g:i a'); ?></p>
#return: Posted: July 17, 2007 at 7:19 am

Sau đây là một số định dạng 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:

<?php the_time('l, F jS, Y') ?>

Chi tiết: http://codex.wordpress.org/Formatting_Date_and_Time

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();	//class="postclass"

/*post title*/
//thường dùng trong thuộc tính alt="",ie: alt="<?php the_title_attribute();?>"
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;
while(have_posts()):the_post();
//lấy post thumbnail (img tag) với kích thước thumbnail. Xem cách <a href="https://www.hoangweb.com/wordpress/su-dung-featured-images-hay-post-thumbnails-trong-wordpress">thay đổi kích thước thumbnail</a>.
	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 xác định 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 thuộc tính ảnh, tham số 2 xác định 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 bộ ảnh của post

$attachments=get_posts(array(
		'post_type'=>'attachment',
		'post_parent'=>$post->ID,
		'numberposts'=>-1
	));
if ( $attachments ) {
        foreach ( $attachments as $attachment ) {
           echo '<li>';
           echo wp_get_attachment_image( $attachment->ID, 'full' );
           echo '<p>';
           echo apply_filters( 'the_title', $attachment->post_title );
           echo '</p></li>';
          }
     }

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ý thuộc tính của ảnh.

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

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.

<a href="<?php echo get_author_posts_url( get_the_author_meta( 'ID' ) ); ?>"><?php the_author_meta( 'display_name' ); ?></a>

Trong đó: get_the_author_meta(‘ID’); //trả về ID cua user bài viết

– Hiển thị liên kết categories, mỗi category được ngăn cách bởi dấu “,”.

<p>This post is in: <?php the_category(', '); ?></p>

Separated by Arrow.

<p>Categories: <?php the_category(' &gt; '); ?></p>

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 về post.

	echo get_the_category_list( ', ' );	//ie: category1,category2,category3,..
//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.

<?php
$posttags = get_the_tags();
if ($posttags) {
  foreach($posttags as $tag) {
    echo $tag->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 '<img src="http://example.com/images/' . $tag->term_id . '.jpg" 
alt="' . $tag->name . '" />'; 
  }
}
?>

Trên đây là tổng hợp template tags để lấy dữ liệu post data. Mình xin hêt !

Nếu bạn thích bài viết này, hãy ủng hộ chúng tôi bằng cách đăng ký nhận bài viết mới ở bên dưới và đừng quên chia sẻ kiến thức này với bạn bè của bạn nhé. Bạn cũng có thể theo dõi blog này trên TwitterFacebook

Liên hệ

Công ty chuyên Thiết kế website uy tín nhất Miền Bắc: http://vinastar.net

Hotline tư vấn: 0989 48 3456

Nguồn: Sưu tầm trên internet

You may also like

Leave a Comment