Hoàng Web

Thiết Kế Website WordPress

  • Kho giao diện
  • Dịch Vụ
    • Thiết kế web giá rẻ
    • Thiết kế website WordPress
    • Hosting Miễn Phí 100GB
    • Tích hợp thanh toán MoMo, ViettelPay, Vietcombank, MB..
    • Tối ưu Google PageSpeed
    • Sửa lỗi nâng cấp website
    • Viết plugin WordPress
    • Code Tool theo yêu cầu
  • Bảng giá
  • Quy trình làm việc
  • Giới thiệu
  • Liên Lạc
Trang chủ » Wordpress » Hướng dẫn dùng template tags trong loop

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

Thứ Năm, 03/07/2014 by Hoàng Quách

Nội dung

  • 1 Post publish time
  • 2 Post content
  • 3 Get post images (featured image/post thumbnails)
    • 3.1 Lấy toàn bộ ảnh của post
  • 4 Post Custom field
  • 5 Get Post author
  • 6 Get the post Category
  • 7 Get Post tags
  • shares
  • Facebook
  • Facebook Messenger
  • Gmail
  • Viber
  • Skype

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;

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 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 \a\t g:i A – 2010/11/06 at 12:50 AM
  • Y/m/d \a\t 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

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

Get post images (featured image/post thumbnails)

while(have_posts()):the_post();
//lấy post thumbnail (img tag) với kích thước thumbnail. Xem cách <a href="/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.

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.

<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

Get the post Category

– 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,..

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.

<?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 !

Hãy cho mình biết suy nghĩ của bạn trong phần bình luận bên dưới bài viết này. Hãy theo dõi kênh chia sẻ kiến thức WordPress của Hoangweb trên Twitter và Facebook

  • shares
  • Facebook
  • Facebook Messenger
  • Gmail
  • Viber
  • Skype

Chuyên mục: Wordpress

Tôi giúp gì cho bạn?

HOÀNG WEB

Địa chỉ: Tây Sơn, Phường Quang Trung, Quận Đống Đa, Hà Nội

Hotline: 0987 342 124 – 0868 292 303 (8h:00 – 21h:00)

Email: [email protected]

Website: www.hoangweb.com

KẾT NỐI VỚI TÔI

  • Facebook
  • GitHub
  • YouTube

SẢN PHẨM

  • Plugin Thanh Toán Quét Mã QR Code Tự Động
  • WP2Speed – Tối ưu Google Speed
  • 23WebHost – Hosting Miễn Phí 100GB

LIÊN KẾT

  • Có nên thuê thiết kế website giá rẻ?
  • Hướng dẫn thanh toán
  • Chính sách hoàn tiền
  • Trung tâm hỗ trợ

Copyright © 2023 | All rights reserved | HOANG WEB
Mọi hình thức sao chép nội dung trên website này mà chưa được sự đồng ý đều là trái phép.