Tải bản đầy đủ (.pdf) (9 trang)

Hiển thị các bài liên quan trong WordPress không dùng plugin docx

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (141.18 KB, 9 trang )

Hiển thị các bài liên quan trong WordPress không dùng
plugin
Các bài viết liên quan hiện nay đã trở thành chức năng phổ
biến đối với bất kỳ trang web hay blog
nào. Nó giúp chúng
ta có thể tìm thấy nhiều thông tin hơn về cùng một chủ đề
khi theo dõi một bài viết trên mạng. Đôi khi chính nhờ
những “Related Post” này mà mình lại phát hiện ra những
thứ mà trước đây tìm mãi không ra đấy
Với WordPress thì có vài plugin đảm nhận chuyện này rồi,
các bạn có thể tham khảo các plugin sau:
- Yet Another Related Posts Plugin
- Similar Post

- WordPress Related Post

Tuy nhiên nhiều bạn lại không muốn dùng nhiều plugin vì
lý do nặng blog, với lại nhiều khi cách hiển thị của plugin
lại không như mong muốn của mình. Hôm nay mình xin
chia sẻ đoạn code đơn giản hiển thị các bài liên quan mà
không dùng tới plugin
Code này chỉ lấy các bài cùng
chuyên mục hoặc các bài có cùng tag hiển thị mà thôi. Bắt
đầu nào:
Cách1:
Nếu muốn hiển thị các bài có cùng tag thì ta dùng đoạn
code sau:
<?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 muốn hiển thị.
‘caller_get_posts’=>1
);
$my_query = new wp_query($args);
if( $my_query->have_posts() ) {
echo ’<h3>Bài liên quan</h3><ul>’;
while ($my_query->have_posts()) {
$my_query->the_post();
?>
<li><a href=”<?php the_permalink() ?>” rel=”bookmark” t
itle=”Permanent Link to <?php the_title_attribute(); ?>”><
?php the_title(); ?></a></li>
<?php
}
echo ’</ul>’;
}
}
?>
Nếu muốn hiển thị các bài có cùng chuyên mục thì ta chỉ
việc đổi code đi 1 tẹo thôi
<?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 muốn hiển thị.
‘caller_get_posts’=>1
);
$my_query = new wp_query($args);
if( $my_query->have_posts() ) {
echo ’<h3>Bài liên quan</h3><ul>’;
while ($my_query->have_posts()) {
$my_query->the_post();
?>
<li><a href=”<?php the_permalink() ?>” rel=”bookmark” t
itle=”Permanent Link to <?php the_title_attribute(); ?>”><
?php the_title(); ?></a></li>
<?php
}
echo ’</ul>’;
}
}
?>
Việc cuối cùng là bạn hay mở file single.php ra, tìm chỗ
nào bạn muốn hiển thị các bài liên quan, chèn 1 trong 2
đoạn code trên vào thôi.
Còn đây là thêm số lượt views từng bài liên quan
<?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’=>10, // Số bài viết muốn hiển thị.
‘caller_get_posts’=>1
);
$my_query = new wp_query($args);
if( $my_query->have_posts() ) {
echo ‘<h4>Bài viết liên quan</h4><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><font color=”red”>-><?php
the_views(); ?></font></li>
<?php
}
echo ‘</ul>’;
}
}
?><p/>
Chú ý: Trên đây chỉ là code để lấy ra bài liên quan. Muốn
hiển thị các bài liên quan này cân đối thì bạn phải thêm thẻ
div, thêm các class hoặc id nữa, rồi chỉnh trong CSS tùy
theo theme của các bạn nhé.
Cách 2:
Chỉ đơn giản copy và paste đoạn code sau vào cuối file

functions.php (custom_functions.php nếu bạn sử dụng
thesis)
/***** Bai viet lien quan *****/
function related_posts_shortcode( $atts ) {
extract(shortcode_atts(array(
‘limit’ => ’10′, //So bai viet hien thi
), $atts));
global $wpdb, $post, $table_prefix;
if ($post->ID) {
$retval = ‘<ol>’;
// Get tags
$tags = wp_get_post_tags($post->ID);
$tagsarray = array();
foreach ($tags as $tag) {
$tagsarray[] = $tag->term_id;
}
$tagslist = implode(‘,’, $tagsarray);
// Do the query
$q = “SELECT p.*, count(tr.object_id) as count
FROM $wpdb->term_taxonomy AS tt, $wpdb-
>term_relationships AS tr, $wpdb->posts AS p WHERE
tt.taxonomy =’post_tag’ AND tt.term_taxonomy_id =
tr.term_taxonomy_id AND tr.object_id = p.ID AND
tt.term_id IN ($tagslist) AND p.ID != $post->ID
AND p.post_status = ‘publish’
AND p.post_date_gmt < NOW()
GROUP BY tr.object_id
ORDER BY count DESC, p.post_date_gmt DESC
LIMIT $limit;”;
$related = $wpdb->get_results($q);

if ( $related ) {
foreach($related as $r) {
$retval .= ‘<li><a title=”‘.wptexturize($r->post_title).’”
href=”‘.get_permalink($r->ID).’”>’.wptexturize($r-
>post_title).’</a></li>’;
}
} else {
$retval .= ‘<li>No related posts found</li>’;
}
$retval .= ‘</ol>’;
return $retval;
}
return;
}
add_shortcode(‘related_posts’, ‘related_posts_shortcode’);
function htr_relate($content){
$content=$content;
if (is_single()){
$content.=’<h3>Bài viết liên quan</h3>’;
$content.=’

×