Hin th các bài liên quan trong WordPress không dùng
plugin
Các bài viết liên quan hin nay đã tr thành chc năng ph
biến đối vi bt k trang web hay blog nào. Nó giúp chúng
ta có th tìm thy nhiu thông tin hơn v cùng mt ch đề
khi theo dõi mt bài viết trên mng. Đôi khi chính nh
nhng “Related Post” này mà mình li phát hin ra nhng
th mà trước đây tìm mãi không ra đấy
Vi WordPress thì có vài plugin đảm nhn chuyn này ri,
các bn có th tham kho các plugin sau:
- Yet Another Related Posts Plugin
- Similar Post
- WordPress Related Post
Tuy nhiên nhiu bn li không mun dùng nhiu plugin vì
lý do nng blog, vi li nhiu khi cách hin th ca plugin
li không như mong mun ca mình. Hôm nay mình xin
chia s đon code đơn gin hin th các bài liên quan mà
không dùng ti plugin Code này ch ly các bài cùng
chuyên mc hoc các bài có cùng tag hin th mà thôi. Bt
đầu nào:
Cách1:
Nếu mun hin th các bài có cùng tag thì ta dùng đon
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 mun hin 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 mun hin th các bài có cùng chuyên mc thì ta ch
vic đổi code đi 1 to 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 mun hin 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>’;
}
}
?>
Vic cui cùng là bn hay m file single.php ra, tìm ch
nào bn mun hin th các bài liên quan, chèn 1 trong 2
đon code trên vào thôi.
Còn đây là thêm s lượt views tng 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 mun hin 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();