实现WordPress文章页面的相关文章列表代码
http://www.itjxue.com 2015-08-14 20:03 来源:未知 点击次数:
在single文章页使用相关文章功能的好处是显而易见的,可以增加网站的粘度的同时,更多地是更方便地为用户列出了他可能关心的内容。一般情况下我们是使用水煮鱼的WordPress Related Posts插件来实现的,那么,在尽量节约插件的使用数量的前提下,我们还可以手动添加代码来实现。
1,相关文章非插件实现方法
<?php //for use in the loop, list 5 post titles related to first tag on current post $tags = wp_get_post_tags($post->ID); if ($tags) { echo 'Related Posts'; $first_tag = $tags[0]->term_id; $args=array( 'tag__in' => array($first_tag), 'post__not_in' => array($post->ID), 'showposts'=>5, 'caller_get_posts'=>1 ); $my_query = new WP_Query($args); if( $my_query->have_posts() ) { while ($my_query->have_posts()) : $my_query->the_post(); ?> <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p> <?php endwhile; } } ?> |
2,创建相关文章的发布短代码
把下面的代码写入到你的主题文件夹中的function.php中
function related_posts_shortcode( $atts ) { extract(shortcode_atts(array( 'limit' => '5', ), $atts)); global $wpdb, $post, $table_prefix; if ($post->ID) { $retval = '<ul>'; // 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 .= '</ul> '; return $retval; } return; } add_shortcode('related_posts', 'related_posts_shortcode'); |
以后,你就可以在博客的任意位置插入下面的短代码,来实现相关文章的调用显示了。
[related_posts] |
3,同一分类下的相关文章
实现了相关文章的调用后,有的人又希望列出的文章和原文是同一分类下的,那么如何实现呢?
首先,同样把下面的代码写入的主题文件夹中的function.php中
/** * related post with category * @param: int $limit limit of posts * @param: bool $catName echo category name * @param: string $title string before all entries * Example: echo fb_cat_related_posts(); */ if ( !function_exists('fb_get_cat_related_posts') ) { function fb_get_cat_related_posts( $limit = 5, $catName = TRUE, $title = '<h3>Recent Pages</h3>' ) { if ( !is_single() ) return; $limit = (int) $limit; $output = ''; $output .= $title; $category = get_the_category(); $category = (int) $category[0]->cat_ID; if ( $catName ) $output .= __( 'Kategorie: ' ) . get_cat_name($category) . ' '; $output .= '<ul>'; $args = array( 'numberposts' => $limit, 'category' => $category, ); $recentposts = get_posts( $args ); foreach($recentposts as $post) { setup_postdata($post); $output .= '<li><a href="' . get_permalink($post->ID) . '">' . get_the_title($post->ID) . '</a></li>'; } $output .= '</ul>'; return $output; } } |
之后,调用自定义的函数fb_get_cat_related_posts
就可以了,该函数包括3个参数
- $limit (int) 定义显示文章数,int型数据;
- $catName (bool) 输出分类名称,bool型数据( TRUE or FALSE)
- $title (string) String for a text before all entries