wordpress手动调用随机文章,热门文章,最新文章代码

wordpress手动调用随机文章,热门文章,最新文章代码,很多朋友觉得wordpress插件使用后臃肿,便寻找手动修改wordpress调用随机文章,热门文章,最新文章的方法,以下是风酷seo整理的,希望能帮助到各位:

wordpress手动调用随机文章,热门文章,最新文章代码调用最新文章:
代码如下:

 <ul> <?php $post_query = new WP_Query(‘showposts=10′); while ($post_query->have_posts()) : $post_query->the_post(); $do_not_duplicate = $post->ID; ?> <li><a href=”<?php the_permalink(); ?>”><?php the_title(); ?></a></li> <?php endwhile;?> </ul>

 

调用热门文章:代码如下:

 <ul> <?php $post_num = 10; // 设置调用条数 $args = array( ‘post_password’ => ”, ‘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(); ?> <li><a href=”<?php the_permalink(); ?>” title=”<?php the_title(); ?>”><?php the_title(); ?></a></li> <?php } wp_reset_query();?> </ul>

 

调用随机文章:代码如下:

 <ul> <?php global $post; $postid = $post->ID; $args = array( ‘orderby’ => ‘rand’, ‘post__not_in’ => array($post->ID), ‘showposts’ => 10); $query_posts = new WP_Query(); $query_posts->query($args); ?> <?php while ($query_posts->have_posts()) : $query_posts->the_post(); ?> <li><a href=”<?php the_permalink(); ?>” rel=”bookmark” title=”<?php the_title_attribute(); ?>”><?php the_title(); ?></a></li> <?php endwhile; ?> </ul>

 

 

打开WordPress主题目录,打开index.php或home.php,搜索代码:
while (have_posts()) :
将其改成:
query_posts(‘orderby=rand’);while (have_posts()) :
接着将
<?php endwhile; ?>
改成:
<?php endwhile;wp_reset_query(); ?>

注意:以上方法会跟wp-pagenavi插件发生冲突,导致无法翻页,请将以上方法中的
query_posts(‘orderby=rand’); while (have_posts()) :
改成:
$paged = (get_query_var(‘paged’)) ? get_query_var(‘paged’) : 1;
$args=array(
‘orderby’=>rand,
‘paged’=>$paged,
);
query_posts($args);
while (have_posts()) :
使用WordPress函数query_posts,可以控制文章的输出方式,关于该函数详细用法,详见WordPress官方文档:http://codex.wordpress.org/Function_Reference/query_posts
— 完 —

 

侧栏增加随机小工具

//随机文章 class RandomPostWidget extends WP_Widget { function RandomPostWidget() { parent::WP_Widget('bd_random_post_widget', '随机文章', array('description' => '我的随机文章小工具') ); } function widget($args, $instance) { extract( $args ); $title = apply_filters('widget_title',empty($instance['title']) ? '随机文章' : $instance['title'], $instance, $this->id_base); if ( empty( $instance['number'] ) || ! $number = absint( $instance['number'] ) ) { $number = 10; } $r = new WP_Query(array('posts_per_page' => $number, 'no_found_rows' => true,'post_status' => 'publish', 'ignore_sticky_posts' => true, 'orderby' =>'rand')); if ($r->have_posts()) { echo $before_widget; if ( $title ) echo $before_title . $title . $after_title; ?> <ul> <?php while ($r->have_posts()) : $r->the_post(); ?> <li> <a href="<?php the_permalink() ?>" title="<?php echo esc_attr(get_the_title() ? get_the_title() : get_the_ID()); ?>"><?php if ( get_the_title() ) the_title(); else the_ID(); ?></a> </li> <?php endwhile; ?> </ul> <?php echo $after_widget; wp_reset_postdata(); } } function update($new_instance, $old_instance) { $instance = $old_instance; $instance['title'] = strip_tags($new_instance['title']); $instance['number'] = (int) $new_instance['number']; return $instance; } function form($instance) { $title = isset($instance['title']) ? esc_attr($instance['title']) : ''; $number = isset($instance['number']) ? absint($instance['number']) : 10;?> <p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> <input id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" /></p><p><label for="<?php echo $this->get_field_id('number'); ?>"><?php _e('文章显示数量:'); ?></label> <input id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" size="3" /></p> <?php } } add_action('widgets_init', create_function('', 'return register_widget("RandomPostWidget");'));

转载请注明出处::【风酷SEO】 » wordpress手动调用随机文章,热门文章,最新文章代码

评论 0


Fatal error: Call to undefined function hui_user_avatar() in D:\wwwroot\fengkunet\wwwroot\wp-content\themes\xiu\functions.xiu.php on line 788