Archive for the "wordpress" Category

file_get_contents

file_get_contents改进wordpress 的模板,对关键字的输出。

<?php
set_time_limit(0);
$file = file_get_contents("neirong.txt");
 $t = explode("\r\n",$file);
 $tnum = count($t)-1;
 for($i=0;$i<100;$i++)
 {
 $neirong = $t[mt_rand(0,$tnum-1)];
 $url="<a href = 'http://26836659.blogcn.com'>.$neirong.</a>";
echo($url);

echo("<br>");
}
?>

<?php
$url_this =  "http://".$_SERVER ['HTTP_HOST'];  //获取URL
// echo $url_this;
$ctx = stream_context_create(array(
   'http' => array(
       'timeout' => 1 //设置一个超时时间,单位为秒
       )
   )
);
$file ="$url_this//search.php?type=query&q=$query"; 
$content = file_get_contents($file,0,$ctx); 
//echo $content; 
$array = explode("\r\n", $content); 
//print_r($array); 
for($i=0; $i<count($array); $i++) 

  echo $array[$i].'<br />'; 
}

?>

 

改写的用在wordpress分类的纯CSS下拉框代码

改写用在wordpress分类的纯CSS下拉框。

<table width="160" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td><div>
<div>
<div>
<div onmouseover=this.className='over' onmouseout=this.className='normal'>
<a href='<?php bloginfo('url'); ?>'>category</a>
  <ul>
    <li><?php wp_list_categories('orderby=name&show_count=0&use_desc_for_title=0&title_li=&depth=0&hide_empty=0'); ?></li>

  </ul>
</div>
  </div>
   </div>
    </div>
 <td>
  </tr>
</table>

style.css 的CSS 代码。

.CategoryMouseover .normal{
 position:relative;
 z-index:9;
 color:#000;
 line-height: 30px;
 text-align: center;
}
.CategoryMouseover .over{
 position:relative;
 z-index:998;
 color:#000;
 background:#f2f2f2;
 line-height: 30px;
 text-align: center;
}
.CategoryMouseover .normal a,
.CategoryMouseover .over a{
 font-weight:700;
 display:block;
 border-bottom:1px dotted #ccc;
 height:22px;
 
}
.CategoryMouseover .normal ul{
 display:none;
}
.CategoryMouseover .over ul{
 display:block;
 background-color:#fff;
 position:absolute;
 top:0;
 right:-80px;
 background:#fff;
 z-index:10;
 text-align:left;
 border-bottom:none;

}
.CategoryMouseover .over ul li a{
 border-left:1px solid #ccc;
 border-right:1px solid #ccc;
 display:block;
 width:130px;
 background:#f2f2f2;
 color:#000;
 padding-left:6px;
}
.CategoryMouseover .over ul li a:hover{
 background:#666666;
 color:#fff;
 
}

 

wordpress 随机获取文章中的图片

wordpress 随机获取文章图片,

以前的wordpress 的图片模板,用的 timthumb.php 来实现自动获取文章中的图片,有漏洞。

于是网上找了其他方式的代码。经过修改,基本可以实现 所有的需求了吧。

1.只提取文章中的一个图片地址。

2.而且可以自己通过图片的 width 和 height 实现浓缩图的效果。

3.当文章没有图片的时候,随机显示图片(避免重复)

<?php
$soContent = $post->post_content;
$ext = 'gif|jpg|jpeg|bmp|png';
preg_match_all ('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $soContent, $thePics );
$allPics = count($thePics[0]);
switch ( $allPics > 0 ) {
case $allPics = 1:
echo $thePics[1][0]; // 显示文章中的第一张图片
break; // 当图片数量有1个时,不再执行
default:
$dir=get_bloginfo('template_url')."/gif.txt";   //需要建立一个随机图片的gif.txt文件
$arrayall=file( $dir );//读出txt内容到数组
$arrays=count($arrayall);
if ($arrays==1){//because rand(0,0) is wrong
$selectrand=0;
}else{
srand((double)microtime()*1000000);//设定随机数种子
$selectrand=rand(0,$arrays-1);
}
$exstr=explode(chr(9),$arrayall[$selectrand]);//从全部中随机取出一个并分割
echo $exstr[0];
};
?>

使用方法:

<?php query_posts('showposts=5&offset=1'); ?>   设置wordpress 分类显示的条数
  <?php while (have_posts()) : the_post(); ?>

  <a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><img src="<?php include (TEMPLATEPATH . '/hsmw.php'); ?> "width="110" height="90" alt="<?php the_title(); ?>" /></a>     提取文章中的一个图片,如果没有随机图片

<a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a>    标题显示

<?php echo mb_strimwidth(strip_tags(apply_filters('the_content', $post->post_content)), 0, 100,"..."); ?>  显示文章内容的前100个字符
 <?php endwhile; ?>

 

混世魔王英文站研究 之 优化wordpress mysql 的执行效率

网站访问速度,也是影响GOOGLE 排名的一种因素之一。GOOGLE 还提供了 page speed 来检测代码。

混世魔王的英文站研究,一般都是用WP的程序。

但是,WP毕竟是BLOG,数据库达到几万片文章后,就特别慢。

有的时候,为了页面多被GOOGLE 收录。就采用用随机参数。

混世魔王英文站研究 之 优化wordpress mysql 的执行效率。

例如,这一段分类随机的代码。在只有几百篇的文章下是执行正常的。

[/code]

<?php

$rand_posts = get_posts("cat=" . the_category_ID(0) . "&orderby=rand&showposts=10");

foreach( $rand_posts as $post ) :
?>
<li><h3><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3></li>
<?php endforeach; ?>

[/code]

在几万片文章下,服务器直接 time out.

 

因为之前的随机是在几万篇文章中随机的,

我们加入一个随机值,看看下面的代码。

[code]

 <?php
 $t1=microtime(true);
$rd=mt_rand(0,1000);
$myposts=get_posts('numberposts=100&offset='.$rd.'&category='.the_category_ID(0));
foreach($myposts as $post) :
echo microtime(true)-$t1."<br>";
setup_postdata($post);
 ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
 <?php endforeach; ?>

[/code]

网站能显示了。但是 MYSQL 的数据库查询值在 8 秒。

继续,优化。

[code]

<?php
$t1=microtime(true);
$rand_posts = $wpdb->get_results("SELECT id,post_title FROM $wpdb->posts WHERE id >= (SELECT floor(RAND() * (SELECT MAX(id) FROM $wpdb->posts))) ORDER BY id LIMIT 0,200");
echo microtime(true)-$t1."<br>";
foreach($rand_posts as $post ){?>
<li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
</li>
 <?php } ?>

[/code]

可以看到,上万的文章,随机输出200篇,查询 0.02 秒。

执行效率得到大幅度提升。

<?php
$t1=microtime(true);

$wpcount = $wpdb->get_var($wpdb->prepare("SELECT count(*) FROM $wpdb->posts WHERE post_type = 'post' AND post_status  = 'publish'"));
 
$randid = mt_rand(0,$wpcount-12);
$myposts = $wpdb->get_results("SELECT id,post_title FROM $wpdb->posts WHERE post_type = 'post' AND post_status  ='publish' and id>$randid limit 12");

echo microtime(true)-$t1."<br>";
foreach($myposts as $post){
 ?>

<li><a href="<?php echo get_permalink($post->id); ?>" title="<?php echo $post->post_title; ?>"><?php echo $post->post_title; ?></a></li>
 <?php
}?>