一直在寻找WordPress相关文章的插件,但用起来都差强人意,有眼缘的插件,又都需要收费。本来很倾向无觅的插件,但是似乎停止的开发。百度推荐配置起来很麻烦。百度上搜索到了一个高仿无觅的代码实现方法。拿来用起来很方便。具体方法如下:
准备工作
1、下载相关文件: timthumb.php 【点击下载】,放到主题根目录下。timthumb是一个PHP截图插件,具体功能请自行谷歌。
2、在你的主题根目录下新建一个文件夹为cache,文件夹的权限设置为777。
一、将下列代码放到需要显示相关文章的地方,我只放到了single.php里
<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>
二、在主题的funtions.php的最后一个 ?> 前添加下面的代码:一定要在最后添加,我本想单独写一个文件,再在functions.php中引用,一直出错。
//添加特色缩略图支持 devework.com if ( function_exists('add_theme_support') )add_theme_support('post-thumbnails'); //输出缩略图地址 devework.com function post_thumbnail_src(){ global $post; if( $values = get_post_custom_values("thumb") ) { //输出自定义域图片地址 $values = get_post_custom_values("thumb"); $post_thumbnail_src = $values [0]; } elseif( has_post_thumbnail() ){ //如果有特色缩略图,则输出缩略图地址 $thumbnail_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID),'full'); $post_thumbnail_src = $thumbnail_src [0]; } else { $post_thumbnail_src = ''; ob_start(); ob_end_clean(); $output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches); $post_thumbnail_src = $matches [1] [0]; //获取该图片 src if(empty($post_thumbnail_src)){ //如果日志中没有图片,则显示随机图片 $random = mt_rand(1, 10); echo get_bloginfo('template_url'); echo '/images/'.$random.'.jpg'; //如果日志中没有图片,则显示默认图片 //echo '/images/default_thumb.jpg'; } }; echo $post_thumbnail_src; }
三、在主题style.css中添加样式代码
/* Devework.com 相关文章 */ .same_cat_posts a {color: #555; text-decoration: none} .same_cat_posts {width:670px; height: 160px; margin: 25px 0px 5px 0px; } .same_cat_posts h3 {margin-bottom: 10px; font-weight: bolder; font-size: 16px;border-bottom:2px solid #08A5E0;padding-bottom:12px} .same_cat_posts ul {list-style: none; margin-left: -43px} .same_cat_posts ul li {float: left; padding: 5px; border-right: 1px solid #CCC;height: 168px;overflow:hidden} .same_cat_posts ul li:hover {background: #C3E99E} .same_cat_posts ul li img {width: 113px; height: 113px; padding: 2px; border: 1px solid #CCCCCC} .same_cat_posts ul li .same_cat_posts_tittle { margin-left: 2px; width: 113px;font-size:12px}
需要自己根据主题修改相关位置代码,适应主题显示效果
刷新页面,效果非常好。把第一部分封装成函数调用,会更加方便。