当前位置: 首页 > 图文教程 > 网页制作 > CMS技巧 > 实现WordPress文章页面的相关文章列表代码

CMS技巧
dede(织梦)CMS常见问题及解决方法
织梦内容管理系统(dede)模板标签代码参考
网站内容管理系统CMS在国内都有哪些?
建站选择CMS一定要谨慎
适合搜索优化(SEO)的几个CMS介绍
从SEO视角判断CMS系统的好坏
简单把Wordpress打造成CMS
帝国ecms教程:一些常用的技巧大全
帝国ecms:实现google的全站sitemap制作教程
dedecms发布分页问题完全解决方案
使用织梦网站管理(DEDECMS)架设网站全面分析
百度、谷歌搜索引擎原理及新网站应对
织梦网站管理系统(DedeCms2007)将于11月底发布
关于blog系统中最合适做优化的程序
给zblog加上运行代码功能
修改z-blog分页页码样式
给z-blog博客添加链接点击统计
LBS增加引用地址和永久地址点击复制功能
LBS功能:图片的自适应实现代码
ZBLOG增加文章里的关键字替换

CMS技巧 中的 实现WordPress文章页面的相关文章列表代码


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-01-03   浏览: 77 ::
收藏到网摘: n/a

在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