一、默认方法:将下面代码放到主题文章页面single模板或者边栏sidebar模板适当位置即可(当前栏目)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<ul> <?php $cat = get_the_category(); foreach($cat as $key=>$category){ $catid = $category->term_id; } $args = array('orderby' => 'rand','showposts' => 8,'cat' => $catid ); $query_posts = new WP_Query(); $query_posts->query($args); while ($query_posts->have_posts()) : $query_posts->the_post(); ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endwhile;?> </ul> |
方法一:采用wordpress内置函数,在需要的时候直接调用以下代码:
这种方法最简洁也最快速,但其中用到了get_posts函数,那么网页中记录当前文章的信息就被替换掉了,如果后面再出现调用当前文章信息的话就会出现问题,都会以这里调用的随机文章为准,这些代码放在页面的最后部分,就没问题(比如侧边栏调用随即文章),但如果放在页面中,那么后面的评论、上一篇、下一篇、标签、作者等等需要知道当前文章信息的地方都会发生错误。
1 2 3 4 5 6 7 8 |
<ul> <?php $rand_posts = get_posts('numberposts=5&orderby=rand'); foreach( $rand_posts as $post ) : ?> <li> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a> </li> <?php endforeach; ?> </ul> |
方法三:在函数模版function.php中添加函数,然后调用。在function.php文件中添加以下代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php function random_posts($posts_num=8,$before='<li>',$after='</li>'){ global $wpdb; $sql = "SELECT ID, post_title,guid FROM $wpdb->posts WHERE post_status = 'publish' "; $sql .= "AND post_title != '' "; $sql .= "AND post_password ='' "; $sql .= "AND post_type = 'post' "; $sql .= "ORDER BY RAND() LIMIT 0 , $posts_num "; $randposts = $wpdb->get_results($sql); $output = ''; foreach ($randposts as $randpost) { $post_title = stripslashes($randpost->post_title); $permalink = get_permalink($randpost->ID); $output .= $before.'<a href="' . $permalink . '" rel="bookmark" title="'; $output .= $post_title . '">' . $post_title . '</a>'; $output .= $after; } echo $output; }//random_posts()参数有$posts_num即文章数量,$before开始标签默认<li>,$after=结束标签默认</li> ?> |
然后在需要调用随机文章的地方插入下面的代码:
1 |
<?php random_posts(); ?> |
WordPress实现相关文章展示
相关文章的获取思路是:Tags 标签相关>同分类下文章,也就是说,先获取标签相同的文章,如果还达不到数量,就调用该分类下的文章补足。获取方法貌似最初来自 Willin Kan 大师,然后我参考了倡萌的写法,加上了日期的字段。我先贴一下实现的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
<ul> <?php $post_num = 8; // 默认展示8篇文章,可以自行修改~ $exclude_id = $post->ID; $posttags = get_the_tags(); $i = 0; if ( $posttags ) { $tags = ''; foreach ( $posttags as $tag ) $tags .= $tag->term_id . ','; $args = array( 'post_status' => 'publish', 'tag__in' => explode(',', $tags), '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(); ?> <li><a rel="bookmark" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" target="_blank"><?php the_title(); ?></a><span class="pub-time"><?php the_time('m月d日') ?></span></li> <?php $exclude_id .= ',' . $post->ID; $i ++; } wp_reset_query(); } if ( $i < $post_num ) { $cats = ''; foreach ( get_the_category() as $cat ) $cats .= $cat->cat_ID . ','; $args = array( 'category__in' => explode(',', $cats), '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(); ?> <li><a rel="bookmark" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" target="_blank"><?php the_title(); ?></a><span class="pub-time"><?php the_time('m月d日') ?></span></li> <?php $i++; } wp_reset_query(); } if ( $i == 0 ) echo '<li>没有相关文章!</li>'; ?> </ul> |
总共0 条评论。