获取分类id

  1. foreach((get_the_category()) as $category) {
  2. if($category->cat_ID == "1"){
  3. echo $category->cat_name;
  4. }elseif($category->category_parent=="1"){
  5. echo $category->cat_name . '教程 ';
  6. }
  7. else{
  8. echo $category->cat_name . '面试题 ';
  9. }
  10. }

获取热评文章

  1. $postslist = get_posts('numberposts=10&order=DESC&orderby=comment_count');
  2.  foreach( $postslist as $post ) : ?>
  3.     <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>(<?php echo $post->comment_count; ?>)
  4. </li>       
  5. <?php endforeach; ?>

获取最新文章

方法一

  1. <?php $posts = get_posts("numberposts=20&order=DESC&orderby=id" ); ?>
  2. <?php if( $posts ) : ?>
  3. <ul><?php foreach( $posts as $post ) : setup_postdata( $post ); ?>
  4. <li>
  5. <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>">
  6. <?php
  7. if (mb_strlen($post->post_title) > 14) {
  8. echo mb_substr($post->post_title, 0, 14,'utf-8') . '...';
  9. } else
  10. {echo the_title();}
  11. ?>
  12. </a>
  13. </li>
  14. <?php endforeach; ?>
  15. </ul>
  16. <?php endif; ?>

方法二

  1. <?php get_archives('postbypost', 10); ?> (显示10篇最新更新文章)

方法三
显示你博客中最新的20篇文章,其中format=custom这里主要用来自定义这份文章列表的显示样式

  1. <?php wp_get_archives('type=postbypost&limit=20&format=custom'); ?>

解决wordpress 3.1升级后首页打不开的问题

wordpress更新到3.1出现首页开不开重定向循环的情况:google提示网页生成了过多的重定向。清除此网站的 Cookie 或允许第三方 Cookie 可能会解决该问题。如果不能解决,则可能是服务器配置的问题,而不是您的计算机有问题。

解决办法如下
直接把这句代码加到主题的functions.php模板文件中

  1. remove_filter('template_redirect', 'redirect_canonical');

关于wordpress 3.1升级至首页重定向循环原因参考:
wordpress 3.1引入Canonical URL Redirection技术

wordpress升级备忘录

下面是对wordpress做的修改记录,升级的时候,不能覆盖下列文件
1. classes.php
2. wp-includes/category-template.php

  1. /**
  2. 根据id获取其顶级分类的id
  3. **/
  4. function get_category_top_id($id) {
  5. $parent = &get_category( $id );
  6. if ( is_wp_error( $parent ) )
  7. return $id;
  8. if ( $parent->parent && ( $parent->parent != $parent->term_id )) {
  9. $id = get_category_top_id( $parent->parent);
  10. }
  11. return $id;
  12. }
  13.  
  14. /*
  15. 根据分类的id,得到分类名称
  16. */
  17.  
  18. function get_category_title($prefix = '', $cat,$display=True) {
  19. if ( !empty($cat) && !(strtoupper($cat) == 'ALL') ) {
  20. $my_cat_name = apply_filters('single_cat_title', get_the_category_by_ID($cat));
  21. if ( !empty($my_cat_name) ) {
  22. if ( $display )
  23. echo $prefix.strip_tags($my_cat_name);
  24. else
  25. return strip_tags($my_cat_name);
  26. }
  27. } else if ( is_tag() ) {
  28. return single_tag_title($prefix, $display);
  29. }
  30. }

wordpress调用分类下的文章

  1. <?php $posts = get_posts("category=1&numberposts=10"); ?>
  2. <?php if( $posts ) : ?>
  3. <ul><?php foreach( $posts as $post ) : setup_postdata( $post ); ?>
  4. <li>
  5. <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>
  6. </li>
  7. <?php endforeach; ?>
  8. </ul>
  9. <?php endif; ?>

wordpress在IIS下无rewrite利用cos-html-cache实现静态页面

cos-html-cache是个很不错的,可以生成静态html文件的插件,可是如作者所说“cos-html-cache插件是需要urlrewrite支持的,不幸的是这个条件已经将国内的70%的虚拟主机挡在门外了”,我也是因为urlrewrite都原因,一直徘徊在实现html静态页面都大门之外,看着网站的速度越来越慢,可是又改变不了这种现状,已经让我失眠几个晚上。

前段时间想自己写个程序,不用urlrewrite来生成html。今晚想正式开始做,于是就想到借鉴一下cos-html-cache,无意中看到了“图解cos-html-cache原理”这篇文章,就仔细都读了一下,果然受到启发,由此我又想到了以前看到的一篇文章“IIS下不用Rewrite实现wordpress伪静态”,说实话,第一次看到这篇文章都时候挺不屑的,因为我现在不要华丽的URL,不要什么所谓的SEO,我要的是实实在在的访问速度,没有了性能,我没心情搞那些花哨的东西。

首先cos-html-cache的原理就是,当访问者请求一个网址的时候,如/html/2009/110.html,服务器首先会去相关的文件夹(/html/2009/)下寻找文件(110.html)是否存在,如果存在则直接将该文件发送给浏览者,否则发送一个404文件未找到的错误给浏览者,所以我们只要设置好IIS的404错误页面,当访问出现404错误的时候,自动访问首页(index.php)即可,剩下都任务交给cos-html-cache来处理,它会自动为你生成html文件。

第一步,如“IIS下不用Rewrite实现wordpress伪静态”所说,在网站根目录新建一个404.php文件,内容如下:
<?php
$qs = $_SERVER['QUERY_STRING'];
$_SERVER['REQUEST_URI'] = substr($qs, strpos($qs, ‘:80′)+3);
$_SERVER['PATH_INFO'] = $_SERVER['REQUEST_URI'];
include(‘index.php’);
?>
第二步,在IIS的网站属性-自定义错误里面,将404错误的地址改成URL形式的,地址为/404.php

第三步,将永久链接改成自定义的一个访问路径,如/html/%post_id%.html

第四步,安装上cos-html-cache,享受html带给你的速度吧(如果没有正确生成html,请参考cos-html-cache的说明文件)

希望所有用IIS作php服务器并且无法用urlrewrite的武林同道,wordpress生成html成功!