非插件实现随机文章、网站信息等功能

这几天戈戈的免费主机似乎不太给力,好像线路上出了点问题。备用免费空间是一个500M的英国空间,速度很不错,不过内存使用限制为32M,wordpress装上去各种蛋疼。

看了篇文章,说是首页增加随机文章对于搜索引擎快照更新比较友好,对这方面没研究,加上也不错,老文章不应该就此埋没。恩,下面是代码:

使用 WordPress 默认函数 get_posts 中的 orderby=rand 属性来随机选取文章链接。多篇文章并以列表形式展示,则代码如下:

<?php $rand_post = get_posts(‘numberposts=20&orderby=rand’); 
foreach( $rand_post as $post ) : ?> 
<li><a href=”<?php the_permalink(); ?>” title=”<?php the_title(); ?>”><?php the_title(); ?></a></li> 
<?php endforeach; ?>

以上代码随机选择 20 篇文章,列表样式可以根据需要自定义。
另一个版本 ,用query_posts生成随机文章列表。

<?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>&nbsp;<?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; ?>

另外我还在footer增加了最后更新信息,下面是不用插件实现WordPress网站统计信息的代码:

日志总数:<?php $count_posts = wp_count_posts(); echo $published_posts = $count_posts->publish; ?>
草稿数目:<?php $count_posts = wp_count_posts(); echo $draft_posts = $count_posts->draft; ?>
评论总数:<?php echo $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments");?>
成立时间:<?php echo floor((time()-strtotime("2008-8-18"))/86400); ?>
标签总数:<?php echo $count_tags = wp_count_terms('post_tag'); ?>
页面总数:<?php $count_pages = wp_count_posts('page'); echo $page_posts = $count_pages->publish; ?>
分类总数:<?php echo $count_categories = wp_count_terms('category'); ?>
链接总数:<?php $link = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->links WHERE link_visible = 'Y'"); echo $link; ?>
用户总数:<?php $users = $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->users"); echo $users; ?>
最后更新:<?php $last = $wpdb->get_results("SELECT MAX(post_modified) AS MAX_m FROM $wpdb->posts WHERE (post_type = 'post' OR post_type = 'page') AND (post_status = 'publish' OR post_status = 'private')");$last = date('Y-n-j', strtotime($last[0]->MAX_m));echo $last; ?>

不建议多加,免得无谓增加数据库查询。

非插件实现随机文章、网站信息等功能》上有4个想法

发表回复