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

CMS技巧
Drupal CMS可以实现的16种类型网站
Shopex技巧:去除shopex版权
最好的开源SNS程序Dolphind几大功能介绍
Z-Blog留言评论显示IP地址和性能优化
WordPress文章摘要功能简单代码
WordPress博客技巧:图片用单独域名储存
.htaccess文件优化WordPress页面加载速度
ExpressionEngine CMS系统的网站设计实例
让WordPress的Post和Page支持<!–more–>标签
WordPress制作中小企业网站的思路和注意事项
Discuz!7.1升级Discuz!7.2
DEDECMS伪原创插件使用说明和插件下载
小型网站可以选择的网站内容管理系统(CMS)
PHP168 CMSV6.5架构定型 侧重高性能
Drupal建站:整理世界上著名大学的网站
三种直接备份 WordPress 博客的方法
WordPress博客数据库自动备份插件
WordPress插件和主题的一些特别用处
JTBC网站内容管理系统免费的开源的CMS
Wordpress博客防评论机器人

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2010-01-03   浏览: 82 ::
收藏到网摘: 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