Hiển thị bài viết liên quan bằng code trong WordPress

Bài viết liên quan ( Similar Post hay Related Post ) là một trong những thành phần khá phổ biến trong các blog hiện nay , làm tăng sự thân thiện của blog với người đọc đồng thời cũng góp phần làm cho lượng xem được tăng lên đáng kể . có rất nhiều plugin cho phép hiển thị bài viết liên quan , nhưng đối với mình , mình hạn chế dùng plugin thì càng tốt , vì nó thật sự làm giảm tốc độ của blog của bạn rất là nhiều . mình xin phép giới thiệu cho các bạn tạo danh sách các bài viết liên quan mà không dùng plugin :

HIỂN THỊ BÀI VIẾT LIÊN QUAN

Hiển thị bài viết liên quan theo Tags :

 <?php $tags = wp_get_post_tags($post->ID);

        if ($tags) { 

            $tag_ids = array();

            foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;

            $args=array( 'tag__in' => $tag_ids, 'post__not_in' => array($post->ID), 'showposts'=>5, // Số bài viêt liên quan muốn hiển thị 'caller_get_posts'=>1 );

            $my_query = new wp_query($args);

        if( $my_query->have_posts() ) { echo '<h3>Related Posts</h3><ul>';

             while ($my_query->have_posts()) { $my_query->the_post();

                  ?> <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute();

                        ?>"><?php the_title(); ?></a></li> <?php } echo '</ul>'; } } ?> 

Hiển thị bài viết liên quan theo Categories :

<?php
$categories = get_the_category($post->ID);
if ($categories) {
	$category_ids = array();
	foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;

	$args=array(
		'category__in' => $category_ids,
		'post__not_in' => array($post->ID),
		'showposts'=>5, // Số bài viêt liên quan muốn hiển thị
		'caller_get_posts'=>1
	);
$my_query = new wp_query($args); 

if( $my_query->have_posts() ) { echo '<h3>Related Posts</h3><ul>'; while ($my_query->have_posts()) { $my_query->the_post(); ?> <li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li> <?php } echo '</ul>'; } } 

Các bạn có thể tùy chỉnh lại cách hiển thị của các bài liên quan trong file custom.css

nguồn: vaanresblog.com

Bài viết liên quan:

Share |
digg delicious stumbleupon technorati Google live facebook Sphinn Mixx newsvine reddit yahoomyweb
1 Star2 Stars3 Stars4 Stars5 Stars (Đánh giá bài viết)
Loading ... Loading ...