使用wordpress 自带函数实现随机文章和相关文章
获取相关文章的代码如下:
<li id="relative" class="widget-container"> <h3 class="widget-title">Relative Posts</h3> <ul> <?php $tags = wp_get_post_tags($post->ID); foreach ($tags as $tagg) { $tags_id[] = $tagg->term_id; }; if ($tags) { $args=array( 'tag__in' => $tags_id, 'post__not_in' => array($post->ID), 'showposts'=>10, 'caller_get_posts'=>1 ); $my_query = new WP_Query($args); if( $my_query->have_posts() ) { while ($my_query->have_posts()) : $my_query->the_post(); ?> <li><a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></li> <?php endwhile; } } ?> </ul> </li> |
随机文章的代码如下:
<li id="random" class="widget-container"> <h3 class="widget-title">Random Posts</h3> <ul> <?php $randomPosts = $wpdb->get_results("SELECT ID, post_title FROM $wpdb->posts WHERE ID != $post->ID AND post_status = 'publish' AND post_type = 'post' ORDER BY rand() LIMIT 10"); foreach($randomPosts as $randomPost) { ?> <li><a href="<?php echo get_permalink($randomPost->ID) ?>" title="<?php echo $randomPost->post_title; ?>"><?php echo $randomPost->post_title; ?></a></li> <?php } ?> </ul> </li> |