WordPress纯代码仿无觅相关文章图文模式功能(增强版)

WordPress纯代码仿无觅相关文章图文模式功能(增强版)

之前在《WordPress纯代码高仿 无觅相关文章 图文模式功能》一文中分享的代码有两个缺点:一是文章相关度不够(该代码使用分类进行关联相关文章);二是可能会出现当前文章。于是,Jeff在大体看懂源代码的情况下结合着两段源代码进行修改,成功解决以上的两个问题。下面分享最新版本的代码。

本方法实现的相关文章原理是通过获取该文章分类,找到同一分类的文章,这些文章即为“相关文章”。而图片的话,采用的是timthumb 截图(好处是节约空间,提高加载速度),默认的话是截取文章的第一张图片,文章没有图片的话就使用随机图片。

将下面的代码替换 《WordPress纯代码高仿 无觅相关文章 图文模式功能》的第一段代码,其他照原来的即可。

<div class="same_cat_posts">
    <h3>亲,意犹未尽?来看更多:</h3>
<ul class="same_cat_posts_ul">
<?php
$post_num = 5; // 數量設定.
$exclude_id = $post->ID; // 單獨使用要開此行 //zww: edit
$posttags = get_the_tags(); $i = 0;
if ( $posttags ) {
    $tags = ''; foreach ( $posttags as $tag ) $tags .= $tag->term_id . ','; //zww: edit
    $args = array(
        'post_status' => 'publish',
        'tag__in' => explode(',', $tags), // 只選 tags 的文章. //zww: edit
        'post__not_in' => explode(',', $exclude_id), // 排除已出現過的文章.
        'caller_get_posts' => 1,
        'orderby' => 'comment_date', // 依評論日期排序.
        'posts_per_page' => $post_num
    );
    query_posts($args);
    while( have_posts() ) { the_post();//edit by Jeff at DeveWork.com
        $thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id(), 'large');?>
    <li>
         <?php if ( has_post_thumbnail() ) { ?><a class="same_cat_posts_img" href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
         <img src="<?php echo $thumbnail[0]; ?>" alt="<?php the_title(); ?>" /></a>
         <?php } else { ?>
    <a class="same_cat_posts_img" href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
    <img src="<?php bloginfo('template_directory'); ?>/timthumb.php?src=<?php echo post_thumbnail_src(); ?>&h=114&w=114&zc=1" alt="<?php the_title(); ?>" 
    class="thumbnail"/></a>
         <?php } ?>
         <p class="same_cat_posts_tittle"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
    <?php the_title(); ?></a></p>
    </li>
 
    <?php
        $exclude_id .= ',' . $post->ID; $i ++;
    } wp_reset_query();
}
if ( $i < $post_num ) { // 當 tags 文章數量不足, 再取 category 補足.
    $cats = ''; foreach ( get_the_category() as $cat ) $cats .= $cat->cat_ID . ',';
    $args = array(
        'category__in' => explode(',', $cats), // 只選 category 的文章.
        'post__not_in' => explode(',', $exclude_id),
        'caller_get_posts' => 1,
        'orderby' => 'comment_date',
        'posts_per_page' => $post_num - $i
    );
    query_posts($args);
    while( have_posts() ) { the_post(); //edit by Jeff at DeveWork.com
     $thumbnail = wp_get_attachment_image_src(get_post_thumbnail_id(), 'large');?>
     <li>
    <?php if ( has_post_thumbnail() ) { ?><a class="same_cat_posts_img" href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
    <img src="<?php echo $thumbnail[0]; ?>" alt="<?php the_title(); ?>" /></a>
    <?php } else { ?>
    <a class="same_cat_posts_img" href="<?php the_permalink() ?>" title="<?php the_title(); ?>">
    <img src="<?php bloginfo('template_directory'); ?>/timthumb.php?src=<?php echo post_thumbnail_src(); ?>&h=114&w=114&zc=1" alt="<?php the_title(); ?>" 
    class="thumbnail"/></a>
    <?php } ?>
         <p class="same_cat_posts_tittle"><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
    <?php the_title(); ?></a></p>
    </li>
    <?php $i++;
    } wp_reset_query();
}
if ( $i  == 0 )  echo '<li>没有相关文章!</li>';
?>
</ul>
</div>
评分:
当前平均分 0.00 (0%) - 0 个投票
9 条 评论
  1. jeff,请问哈这个是不是必须要配合timthumb插件啊?因为我想要能显示图片 :neutral:

    10年前 回复
    • “而图片的话,采用的是timthumb 截图”文中有说

      10年前 回复
  2. 我想给你个图片你这发不了。。。。。。。 :eek: http://roco-photo.bcs.duapp.com/2013/12/suijiwenzhang1.jpg

    10年前 回复
  3. 相关文章调的是同分类的。。我右边随即文章代码是这样的,昨天搞了一大堆脑袋昏,无法设置数量,因为我本来分类里面的文章就少(新手),而右边随机文章又无法设置数量 所以不怎么好看,希望帮我纠正下随机代码,下面是我昨天复制的,忘了在那高的了。。。。。推荐文章

    ”,
    ‘post_status’ => ‘publish’, // 只选公开的文章.
    ‘post__not_in’ => array($post->ID),//排除当前文章
    ‘caller_get_posts’ => 1, // 排除置頂文章.
    ‘orderby’ => ‘comment_count’, // 依評論數排序.
    ‘posts_per_page’ => $post_num
    );
    $query_posts = new WP_Query();
    $query_posts->query($args);
    while( $query_posts->have_posts() ) { $query_posts->the_post(); ?>
    <a rel="bookmark" href="”>

    10年前 回复
    • post_num=3 这个就是文章数量设置

      10年前 回复
  4. 兄弟在不在,相关文章正常了,随机文章无法调节数量,我一左一右的 求解

    10年前 回复
    • 数量在第五行代码处修改

      10年前 回复
  5. 为什么我用了这个方法显示不正常?会对下面的模块重叠!

    11年前 回复
    • 重叠的话是css的问题。

      11年前 回复
发表评论