首页 » Wordpress » WordPress技巧:令你的站点保持更新的方法 — 伪更新 — 随机更新

WordPress技巧:令你的站点保持更新的方法 — 伪更新 — 随机更新

22152 35

独立博客的排名提升,无非是不断的更新高质量的文章内容。可是如果想要保持一定频率的更新速度,对于一个新站来说,可能很容易,因为那是站长还有激情,兴奋等因素驱动,但对于其他的一些,比如已经经营了半年或者更长时间的草根站长来说,可能会是一件很繁琐的事情,没有一定的耐心的人往往会半途而废。面对这样的一个情况,你是否准备放弃更新博客了吗?

在这里,tiandi教你一个好用的wordpress技巧,让你的博客持续保持更新,当然这种更新是属于伪更新,它只是随机把你的文章显示在首页第一篇文章之前而已。

1.首先打开你的function文件,加入以下代码:

function randomtopicfornoupdate($count = 24) {  //如果不传递时间参数,默认为24小时
  global $wpdb;
  $last = $wpdb->get_results("SELECT MAX(post_date) AS MAX_m FROM $wpdb->posts WHERE (post_type = 'post' ) AND (post_status = 'publish' )");
  $laststamp = strtotime($last[0]->MAX_m);
  $hours = (time()+3600*8 - $laststamp)/3600;
  if( $hours > $count) {  //如果当前时间距离最后一次更新文章的时间已经超过指定的时间,则执行伪更新
	  if((time()+3600*8 - get_option("rtfu_time"))/3600 > $count) {
		update_option("rtfu_time",time()+3600*8);
		$rand_query = new WP_Query("showposts=1&orderby=rand");
		if($rand_query->have_posts()){
			while($rand_query->have_posts()){
				$rand_query->the_post();
				global $id;
				update_option("rtfu_postid",$id);
				show_the_index();
			}
		}
	  }	
	  else {
		  $rndpostid = get_option("rtfu_postid");
		  $rand_query = new WP_Query("p=" .$rndpostid);
		  if($rand_query->have_posts()){
			while($rand_query->have_posts()){
				$rand_query->the_post();
				show_the_index();  //调用主题的式样显示随机的这篇文章
			}
		}

	  }
  }
} 
//这里是我使用的主题Freshblog的式样,每个人根据自己的主题调整。基本上是把index.php里的内容搬过来就可以了。
function show_the_index() {?>    
	<div class="undefined">
		<div class="postbox">
			<div class="thumb">
				<a href="<?php the_permalink() ?>" rel="bookmark"> <img src="<?php bloginfo('template_directory'); ?>/includes/timthumb.php?src=<?php get_thumbnail($post->ID, 'full'); ?>&amp;h=150&amp;w=150&amp;zc=1" alt="<?php the_title(); ?>" /> </a>
			</div>
			<!--end: thumb-->
			<div class="boxmeta left">
				<h2> <a href="<?php the_permalink(); ?>" rel="bookmark">
				<?php the_title(); ?>
				</a> </h2>
				<span class="by">Posted by
				<?php the_author_posts_link(); ?>
				 on
				<?php the_time('F j, Y'); ?>
				<?php if (function_exists('getPostViews')) echo getPostViews(get_the_ID()); ?>
			</span> </div>
			<!--end: boxmeta-->
			<div class="post-content">
				<?php tj_content_limit('250'); ?>
				 <div class="readmore">分类: <?php the_category(', '); ?></div>	
			</div> <!--end: post-content-->
		</div>
		 <!--end: postbox-->
	</div>
	<!--end: undefined-->
<?php
}

2. 接着打开你的index.php文件,在if(have_posts))之前加入以下代码:

//括号里的单位是小时,这里12的意思即如果12小时内没有新文章的话,则执行伪更新,自动随机调用一篇已发布的文章放在首页第一篇文章之前。
if(function_exists('randomtopicfornoupdate')) randomtopicfornoupdate(12); 

效果如下图所示:
wordpress技巧随机更新

文章评分2次,平均分5.0

本文原始地址:https://www.tiandiyoyo.com/2013/05/keep-update-by-random-post/
本站所有文章,除了特别注明外,均为本站原创,转载请注明出处来自www.tiandiyoyo.com

您可能还会对以下文章感兴趣:

评论前先开启评论开关:


35 Comments

  1. 神父 :

    你的图片都挂了

  2. 我还是喜欢自己写原创。

  3. 很想自己改 开始自己的技术不行!

载入分页评论...