怎么实现“相关文章”功能
有实例最好了

解决方案 »

  1.   

    不用插件实现wordpress相关文章功能,可以提高页面载入速度,有助于网站静态化的效果。主要还是通过文章的标签来实现相关文章的匹配。代码如下:view sourceprint?
    01
    <ul id="tags_related">
    02
    <?php
    03
    $post_tags = wp_get_post_tags($post->ID);
    04
    if ($post_tags) {
    05
    foreach ($post_tags as $tag)
    06
    {
    07
        
    // 获取标签列表
    08
        $tag_list[] .= $tag->term_id;
    09
    }
    10
    // 随机获取标签列表中的一个标签
    11
    $post_tag = $tag_list[ mt_rand(0, count($tag_list) - 1) ];
    12
    // 该方法使用 query_posts() 函数来调用相关文章,以下是参数列表
    13
    $args = array(
    14
            'tag__in' => array($post_tag),
    15
            'category__not_in' => array(NULL),     
    // 不包括的分类ID
    16
            'post__not_in' => array($post->ID),
    17
            'showposts' => 10,              
    // 显示相关文章数量
    18
            'caller_get_posts' => 1
    19
        );
    20
    query_posts($args);
    21
    if (have_posts()) :
    22
        while (have_posts()) : the_post(); update_post_caches($posts); ?>
    23
    <li><a href="<?php the_permalink(); ?>" rel="book" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
    24
    <?php endwhile; else : ?>
    25
    <!-- 没有相关文章的话随机抽取文章  -->
    26
    <?php $rand_posts = get_posts('numberposts=10&orderby=rand');  foreach( $rand_posts as $post ) : ?>
    27
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    28
    <?php endforeach; ?>
    29
    <?php endif; wp_reset_query(); } ?>
    30
    </ul>