Wordpress主题中显示随机文章PHP代码
WP主题里显示随机文章的PHP代码和插件一文中,我们介绍了两种方式来让WP主题显示随机文章,因为当时给出的代码是我自己用的(我当时是结合CSS列出表格),所以看看代码有点臃肿的感觉,而且当时给出的更简洁的代码不算是特别合理,所以现在给出整理后的代码供童鞋们观摩。
<?php
query_posts(array('orderby' => 'rand', 'showposts' => 2));
if (have_posts()) :
while (have_posts()) : the_post();?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a> <?php comments_number('', '(1)', '(%)'); ?><br />
<?php endwhile;endif; ?>
如果你还想显示含有标题和文章摘要的随机文章,可以这样写
<?php
query_posts(array('orderby' => 'rand', 'showposts' => 1));
if (have_posts()) :
while (have_posts()) : the_post();
the_title(); //这行去掉就不显示标题,你当然不会这么做
the_excerpt(); //去掉这个就不显示摘要了
endwhile;
endif; ?>
不过还是建议使用给出的第一段代码,然后根据自己需要写CSS。完稿,在下节您将看到:WP主题里根据tag显示相关文章。
没有评论▼