首页 百科问答 正文

wordpress如何调取最新文章、随机文章、最热文章

...

大家在使用WordPress程序的时候,如果需要修改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>1234567

获取热门文章

<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>


友情提示:在修改网站模板之前记得要先备份一下你的网站源文件哦。