Thứ Hai, 17 tháng 3, 2014

Quá dễ để hiển thị bài viết liên quan trong wordpress, ở đây mình xin giới thiệu 2 cách là dùng plugins và không dùng plugins.

Sử dụng plugins có sẵn:

Bạn cần cài đặt plugins có tên Yet Another Related Posts Plugin (YARPP) . Sau khi bật plugins các bạn vào Settings » YARPP để configure .
Configue YARPP to only show related pages
Thật quá dễ dàng phải không các bạn, hiện nay có rất nhiều plugins làm điều này các bạn có thể vào trang chủ wp mà tải về chọn cái ưng ý nhất nhé.

Sử dụng code:

Cũng rất đơn giản, các bạn mở file funtions.php và thêm vào đoạn code sau thẻ <?php
01function wpb_related_pages() {
02$orig_post $post;
03global $post;
04$tags = wp_get_post_tags($post->ID);
05if ($tags) {
06$tag_ids array();
07foreach($tags as $individual_tag)
08$tag_ids[] = $individual_tag->term_id;
09$args=array(
10'post_type' => 'page',
11'tag__in' => $tag_ids,
12'post__not_in' => array($post->ID),
13'posts_per_page'=>5
14);
15$my_query new WP_Query( $args );
16if$my_query->have_posts() ) {
17echo '<div id="relatedpages"><h3>Related Pages</h3><ul>';
18while$my_query->have_posts() ) {
19$my_query->the_post(); ?>
20<li><div class="relatedthumb"><a href="<? the_permalink()?>"rel="bookmark" title="<?php the_title(); ?>"><?php the_post_thumbnail('thumb'); ?></a></div>
21<div class="relatedcontent">
22<h3><a href="<? the_permalink()?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
23<?php the_time('M j, Y') ?>
24</div>
25</li>
26<? }
27echo '</ul></div>';
28else {
29echo "No Related Pages Found:";
30}
31}
32$post $orig_post;
33wp_reset_query();
34}
Đoạn code này sẽ hiển thị bài viết liên quan cùng tags.
Bước tiếp theo các bạn mở file single.php thêm vào đoạn code sau dưới nội dung bài viết hoặc nơi nào mà các bạn tùy thích:
1<?php wpb_related_pages(); ?>
Đoạn code này chưa đẹp đâu, các bạn cần phải thêm css để hiển thị phù hợp với giao diện mình đang dùng.
Sưu tầm và chia sẽ
Categories: ,

0 nhận xét:

Đăng nhận xét

Subscribe to RSS Feed Follow me on Twitter!