当前位置: 菠菜博 > WordPress > 文章正文

WordPress 短代码相关文章实现(非插件)

时间: 2009-10-27 / 分类: WordPress / 浏览次数: 501 次 / 8个评论 发表评论

在single文章页使用相关文章功能的好处是显而易见的,可以增加网站的粘度的同时,更多地是更方便地为用户列出了他可能关心的内容。一般情况下我们是使用水煮鱼的WordPress Related Posts插件来实现的,那么,在尽量节约插件的使用数量的前提下,我们还可以手动添加代码来实现,所以,这里就为你提供了一种不错的解决方法。

1,相关文章非插件实现方法
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(); ?>
 
<a title="Permanent Link to &lt;?php the_title_attribute(); ?&gt;" rel="bookmark" href="&lt;?php the_permalink() ?&gt;"></a>
2,创建相关文章的发布短代码

把下面的代码写入到你的主题文件夹中的function.php中

function related_posts_shortcode( $atts ) {
 extract(shortcode_atts(array(
 'limit' =&gt; '5',
 ), $atts));
 
 global $wpdb, $post, $table_prefix;
 
 if ($post-&gt;ID) {
 $retval = '
<ul>';
 // Get tags
 $tags = wp_get_post_tags($post-&gt;ID);
 $tagsarray = array();
 foreach ($tags as $tag) {
 $tagsarray[] = $tag-&gt;term_id;
 }
 $tagslist = implode(',', $tagsarray);
 
 // Do the query
 $q = "SELECT p.*, count(tr.object_id) as count
 FROM $wpdb-&gt;term_taxonomy AS tt, $wpdb-&gt;term_relationships AS tr, $wpdb-&gt;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-&gt;ID
 AND p.post_status = 'publish'
 AND p.post_date_gmt &lt; NOW()
 GROUP BY tr.object_id
 ORDER BY count DESC, p.post_date_gmt DESC
 LIMIT $limit;";
 
 $related = $wpdb-&gt;get_results($q);
 if ( $related ) {
 foreach($related as $r) {
 $retval .= '
	<li><a title="'.wptexturize($r-&gt;post_title).'" href="'.get_permalink($r-&gt;ID).'">'.wptexturize($r-&gt;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]-&gt;cat_ID;
 
 if ( $catName )
 $output .= __( 'Kategorie: ' ) . get_cat_name($category) . ' ';
 
 $output .= '
<ul>';
 
 $args = array(
 'numberposts' =&gt; $limit,
 'category' =&gt; $category,
 );
 
 $recentposts = get_posts( $args );
 foreach($recentposts as $post) {
 setup_postdata($post);
 $output .= '
	<li><a href="' . get_permalink($post-&gt;ID) . '">' . get_the_title($post-&gt;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
Del.icio.us 添加到Digg! 添加到Digg! 分享家:Addthis中文版

8个评论

  1. wulinfo
    2010/01/29 于 14:50:58

    好心你,贴个代码都贴错

  2. chancat
    2009/11/20 于 23:50:38

    谢谢分享 收藏了

  3. imengs
    2009/11/18 于 22:03:29

    代码看得头大,不知道能不能直接拿来用,研究~

  4. 创意无限
    2009/10/31 于 16:55:00

    宝贝,收藏了!

  5. 漠天
    2009/10/29 于 21:40:54

    非常不错,我感觉自己添加代码更自由些。

  6. 万戈
    2009/10/28 于 11:37:03

    这个很不错,还是我第一次看见,收藏之!

  7. neo
    2009/10/28 于 11:25:49

    代码都乱了

发表评论

您的昵称 *

您的邮箱 *

您的网站