程序随笔 – C/C++ & PHP

2010年03月14日

WordPress运行原理学习笔记之一:主题机制初探

类归于: wordpress — admin @ 10:02 下午

一、主题包各文件说明

      header.php:这个文件中包含博客的标题(title),描述(description),tag,以及css等信息。
      sidebar.php:这个文件控制你侧边栏中的显示方式。
      footer.php:控制页面底部的显示,包括ICP,版权声明等信息。
      archive.php:被系统调用用于显示用户提交分类,按日期归档,tag等查询后显示的页面。下面代码显示了archive.php内部是如何辨别不同的查询请求的:
<?php if (is_category()) { ?>
<div class=”pagetitle”>‘<?php single_cat_title(); ?>’ 分类下的文章</div>
<!–显示下面是哪个分类下的文章列表,‘ ’ 分别是左右单引号,下同–>
<?php }    elseif( is_tag() ) { ?>
<div class=”pagetitle”>‘<?php single_tag_title(); ?>’ 标签下的文章</div>
…………
<?php } elseif (isset($_GET['paged']) && !empty($_GET['paged'])) { ?><div class=”pagetitle”>日志归档</div>
<?php } ?>

      search.php:用于显示用户提交搜索后显示的页面。
     searchform.php:搜索栏表单,被header.php调用。
     single.php:用于全文显示post页面。
     page.php:用于全文显示page页面。
     function.php:存放自定义的函数,被其他文件包含和调用。
     comments.php:留意评论模板,被single.php,page.php等需要评论功能的网页调用。
     style.css:整个网站布局和显示模式。
     screenshot.jpg:用于在后台选择主题页面里显示这个主题的截图,wordpress会自动在主题目录下寻找screenshot.*文件。

二、从源码看主题的调用过程

     当浏览器向apache发送请求要得到博客主页的HTML代码时,./index.php被调用。./index.php中的require(’./wp-blog-header.php’);调用./wp-blog-header.php。
而./wp-blog-header.php做了一些链接数据库包含头文件等处理,其中有两件事情与主题调用有关:
     1、有require_once( dirname(__FILE__) ./wp-includes/functions.php’);
而/wp-includes/functions.php中包含进了./wp-includes/theme.php,这里定义了get_home_template()。
     function get_home_template() {//返回所要加载的主题
          $template = ”;
          if ( file_exists(TEMPLATEPATH . “/home.php”) )
               $template = TEMPLATEPATH . “/home.php”;
          elseif ( file_exists(TEMPLATEPATH . “/index.php”) )
               $template = TEMPLATEPATH . “/index.php”;
          return apply_filters(’home_template’, $template);

     其中apply_filters()的作用是:将$template hook到tag=’home_template’的filters上并返回$template,也就是主题包的路径。
     2、有require_once(ABSPATH . WPINC . ‘/template-loader.php’);
调用了./wp-includes/template-loader.php(ABSPATH代表./, WPINC代表/wp-includes)。
     而./wp-includes/template-loader.php中有:
     } else if ( is_home() && $template = get_home_template() ) {
     include($template);
     return;

     get_home_template()将 主题包路径返回给$template,这里的include($template);语句就把./wp-content/themes/主题名/index.php(比如阿德日志现在所用的主题路径就是./wp-content/ade/index.php)包含进去了。于是开始执到./wp-content/themes/ade/index.php里面的代码了,接下来就是该主题内部的事情了。

三、主题被调用后,首页的加载过程

     各个页面的加载过程大致相同,下面以首页为例说明。显示首页的时候,Wordpress只调用index.php,在index.php定义了要加载那些模板以及其加载顺序。一般情况是:header.php->页面主要内容(index,page,signle等页面都有其自己的定义)->sidebar.php->footer.php

     其实对于一个能够提供Theme的程序而言,在程序的构架上必须要实现数据和表现的分离。通常我们所说的MVC(Model、View、Controller)架构就是这个意思。

     在WordPress中,是这样来实现数据和实现的分离的。
     1. XHTML:用Div等用来表现数据,CSS来描述这些数据的表现形式,用这种方式来实现数据和表现的分离;
     2. Template的方式:页面各个部分单独保存为一个一个.php文件(如上面的sidebar.php,footer.php等),页面加载时候调用所需代码。

     比如<?php comments_template(); ?>就返回了留言框的所需代码。

     在比如index中有:
     <?php get_footer(); ?>
     <?php get_header(); ?>
     “get_header”和”get_footer”分别用来得到该页的Header和Footer,其实就是两个DIV块儿。index的主体同样是一个大的Div(<div id=”content”>),在该Theme的CSS中即可以看到对这个DIV的表现定义,包括字体、背景颜色、边距等等。这里唯一的 id=content来告诉浏览器,这个标签内的内容显示在css中定义的content部分里面。

WordPress运行原理学习笔记之二:插件机制初探

类归于: wordpress — admin @ 7:30 下午

      Wordpress的插件机制使得开发者可以方便地向系统添加自己需要的功能,而这是使得Wordpress得以在全世界流行的重要原因。一个 WordPress插件是一个程序,或者是用PHP脚本语言写出的一个或一些函数的集合,用来往WordPress Webblog 里增加一些特定的特征和服务,它们可以通过WordPress的 Plugin Application Program Interface (API) 提供的接入点和函数无缝的集成到Webblog里.

      插件机制的实现主要依靠wp-includes目录下的plugin.php文件,该文件中包含了与插件机制相关的几个函数。在 wordpress内核运行时设立了一些标记(tag),当遇到这些标记时,wordpess会自动调用挂载到 (hook to)这个标记上的所有函数,该功能是通过数组来实现的,其过程可以直观的表示为下图:

插件hook原理

      用户可以通过plugin API方便的将自定义的功能添加到系统相应的位置。需要指出的是:wordpress定义了两种类型的插件API,行为(actions)和过滤器 (filters):

Actions: Actions are the hooks that the WordPress core launches at specific points during execution, or when specific events occur. Your plugin can specify that one or more of its PHP functions are executed at these points, using the Action API.

      Actions 是由WordPress内核在执行过程中的特定的点或者特定的事件发生时调用的. 使用这些Action API,你的plugin可以指定在这些点去执行一个或者多个PHP函数.

Filters: Filters are the hooks that WordPress launches to modify text of various types before adding it to the database or sending it to the browser screen. Your plugin can specify that one or more of its PHP functions is executed to modify specific types of text at these times, using the Filter API.

      Filters是一种hooks,由WordPress调用,针对于各种形式的文本,让它们在进入数据或发送到浏览器之前得到调整. 使用Filter API,你的插件可以指定在某个时间执行一个或者多个PHP函数来修改特定类型的文本.

一、一个最简单的插件的开发过程

      下面以阿德弄的一个a_simple_song插件(其实跟自带的那个插件是一样的,只是修改了歌词而已)为例简述整个过程三个步骤:编写插件要执行的代码,添加add_action(),进入后台激活它。
      该插件的功能是:在WordPress后台左上角随机显示《一首简单的歌》这首歌的歌词。
      1.在wordpress目录/wp-content/plugins/下新建一个文件:a_simple_song。
      2.用文本编辑工具打开它,输入<?php    ?>,接下来的工作就是在这里面填写相应PHP代码。
      3.首先按一定的格式编写本插件的一些信息,WordPress读取它用于在后台显示,方便用户查看:

      /*
     Plugin Name: 一首简单的歌
     Plugin URI: http://blog.shidelai.cn/#
     Description: 这个插件会在后台随机显示《一首简单的歌》的歌词。
     Author: 施德来
     Version: 1.0
     Author URI: http://blog.shidelai.cn
     */

      4.编写要执行的操作:
     $lyrics = “一首简单的歌    //定义了一个字符串数组用于存储歌词
     这世界很复杂
     ………
     你是我最珍贵的财富”;
     $lyrics = explode(”\n”, $lyrics);
     //explode()是PHP内置函数,功能就是分割字符串成字符数组
     $chosen = wptexturize( $lyrics[ mt_rand(0, count($lyrics) - 1) ] );
     /*
     给chosen赋一条随机的歌词
     wptexturize()在wp-includes\functions-formatting.php定义,功能:把某些特殊符号给自动转换化成标准码格式。
     mt_rand()函数返回它2个参数之间的随机值
     count()返回数组大小
     */
     function a_simple_song() {
          global $chosen;
          echo “<p id=’dolly’>$chosen</p>”;
     }
     //dolly样式表由后面定义。
     //显示歌词
      5.添加add_action(),将a_simple_song函数hook到main_foot tag的队列中
      add_action(’admin_footer’, ‘a_simple_song’);
      //admin_footer.php被调用时执行这个函数
      add_action(’admin_head’, ‘dolly_css’);
     //admin_head.php被调用时执行dolly_css()
     具体经过请参考下面的从源码解读a_simple_song插件的调用过程。

     这样,保存为.php文件放到plugins文件夹下面,再到后台激活就可以了。效果如图

二、WordPress对所有可用的插件的读取

      在文件/wordpress/wp-admin/plugins.php中,函数
      <?php function get_plugins() ?>
      用来从文件系统得到所有的插件。原理很简单,就是读取’wp-content/plugins’目录下的所有PHP文件。这个函数允许一级的子文件夹,也就是说在’wp-content/plugins’下面的PHP文件,以及所有在此目录下的一级子文件夹内部的PHP文件被列作插件的候选,用下面的函数去进一步提取插件信息。这样的好处是方便用户利用文件夹来对插件进行管理和组织。

foreach($plugins as $plugin_file => $plugin_data) {
     ….
     $plugin_data['Title']       = wp_kses($plugin_data['Title'], $plugins_allowedtags);
     $plugin_data['Version']     = wp_kses($plugin_data['Version'], $plugins_allowedtags);
     $plugin_data['Description'] = wp_kses($plugin_data['Description'], $plugins_allowedtags);
     $plugin_data['Author']      = wp_kses($plugin_data['Author'], $plugins_allowedtags);
     $author = ( empty($plugin_data['Author']) ) ? ” :  ‘ <cite>’ . sprintf( __(’By %s’), $plugin_data['Author'] ) . ‘.</cite>’;
     ….
}

        用于从插件的头部注释中获取插件的版本、名称、作者,等信息。

三、Active & Deactive Plugin(插件的激活与注销)

       Active(Deactive)插件的操作都在Plugins.php中,比如我要ple_Deactive “a_simsong”这个插件,最后的URL其实是这个样子:

http://localhost/wordpress/wp-admin/plugins.php?action=deactivate&plugin=a_simple_song.php&_wpnonce=242c8cc3e5

      其中,”Action”表示动作,值为”active”或者”deactivate”,而”Plugin”表示动作的对象插件,此处为”a_simple_song.php”。得到动作指令后,首先从数据库中取出当前已经激活的插件。

$active = get_option(’active_plugins’);

      然后根据动作,重新生成已激活插件数组,和theme一样存入数据库,并重新加载此页。加载的时候就需要考虑这些已经激活的插件是怎么工作的了。

四、插件的加载过程

      WordPress中的每页都会包含”wp-config.php”文件,而在”wp-config.php”的最后有这样一句:

<?php require_once(ABSPATH.’wp-settings.php’); ?>

      在”wp-settings.php”文件中,可以找到以下与插件相关的代码片断:

if ( get_option(’active_plugins’) ) {
     $current_plugins = get_option(’active_plugins’);
     if ( is_array($current_plugins) ) {
          foreach ($current_plugins as $plugin) {
               if (” != $plugin && file_exists(ABSPATH . PLUGINDIR . ‘/’ . $plugin))
                    include_once(ABSPATH . PLUGINDIR . ‘/’ . $plugin);
               }
          }
}

      可见,这段代码会取出系统中所有Actived的插件,并Include进来。所以在每页加载的时候,都会首先Include这些插件代码。这也就是为什么在主题模板里面可以直接调用插件的函数。

五、WordPress通过插件机制注册系统事件

      WordPress中的很多功能也都是通过这种插件结构来实现的,WordPress默认注册的系统事件保存在default-filter.php中。比如:
<?php add_filter(’the_content’, ‘convert_smilies’); ?>
用来将正文(content)中的笑脸符号转换为图像。
add_filter(’comment_text’, ‘wptexturize’);
用来将从数据库提取的正文中的特殊符号转化成标准格式的。

六、从源码解读a_simple_song插件的调用过程

      在每一个wordpress/wp-admin/下面的文件(也就是后台显示需要的文件)的前面都有
<?php require_once(’admin-header.php’); ?>

而在”admin-head.php”中将会执行扩展点”admin_head”的所有扩展:

<?php  do_action(’admin_head’, ”); ?>

这样,就会执行所有hook到admin_head上的函数
Admin Page的Footer部分同样如此,
<?php do_action(’admin_footer’, ”); ?>

      这样,就会执行所有挂接到admin_footer的函数

a_simple_song里面有这么2行代码:

add_action(’admin_footer’, ‘a_simple_song’);
add_action(’admin_head’, ‘dolly_css’);

      将控制形式和内容2个函数挂到不同的tag上,控制文字显示位置和样式的dolly_css是先被调用,然后admin_foot.php被调用一次,a_simple_song()也就被调用一次,来显示歌词。

WordPress插件机制实现原理

类归于: wordpress — admin @ 6:17 下午

      一直对软件的组件、插件架构非常感兴趣,我认为现在开发任何应用程序,一定要有一个架构良好的插件机制,这样可以吸引其他人来开发插件,极大的扩充系统的功能。Eclipse就是一个非常棒的例子,几乎你想什么功能,都可以从社区中找到相应的第三方插件。Firefox也同样如此,它的插件机制非常灵活(尤其是相对IE而言),因此Firefox社区中可以找到各种各样的插件。

1)WordPress读取所有可用的插件
  在文件“/wp-admin/includes/plugin.php”中,函数 get_plugins() 用来从文件系统得到所有的插件。原理很简单,就是读取“wp-content/plugins”目录下的所有PHP文件。这个函数允许一级的子文件夹,也就是说在’wp-content/plugins’下面的PHP文件,以及所以在此目录下的一级子文件夹内部的PHP文件被列作插件的候选,用下面的函数去进一步提取插件信息。这样的好处是方便用户利用文件夹来对插件进行管理和组织。而函数 get_plugin_data() 则用来得到插件的描述(Plugin Descriptor),主要包括插件的版本、名称、作者,等信息,而这些其实是以注释的方式存在的。用WordPress中自带的Hello插件来举例:

PHP代码
<?php   
/*  
Plugin Name: Hello Dolly  
Plugin URI: http://wordpress.org/#  
Description: This is not just a plugin, it symbolizes the hope and enthusiasm of an entire generation summed up in two words sung most famously by Louis Armstrong: Hello, Dolly. When activated you will randomly see a lyric from <cite>Hello, Dolly</cite> in the upper right of your admin screen on every page.  
Author: Matt Mullenweg  
Version: 1.5  
Author URI: http://ma.tt/  
*/  
?>  
这样,在get_plugin_data函数中,就可以来得到插件的详细信息。

PHP代码
<?php    
function get_plugin_data( $plugin_file ) {   
    $plugin_data = implode( ”, file( $plugin_file ));   
    preg_match( ‘|Plugin Name:(.*)$|mi’, $plugin_data, $plugin_name );   
    preg_match( ‘|Plugin URI:(.*)$|mi’, $plugin_data, $plugin_uri );   
    preg_match( ‘|Description:(.*)$|mi’, $plugin_data, $description );   
    preg_match( ‘|Author:(.*)$|mi’, $plugin_data, $author_name );   
    preg_match( ‘|Author URI:(.*)$|mi’, $plugin_data, $author_uri );   
    
    if ( preg_match( “|Version:(.*)|i”, $plugin_data, $version ))   
        $version = trim( $version[1] );   
    else  
        $version = ”;   
    
    $description = wptexturize( trim( $description[1] ));   
    
    $name = $plugin_name[1];   
    $name = trim( $name );   
    $plugin = $name;   
    if (” != trim($plugin_uri[1]) && ” != $name ) {   
        $plugin = ‘<a href=”‘ . trim( $plugin_uri[1] ) . ‘” title=”‘.__( ‘Visit plugin homepage’ ).‘”>’.$plugin.‘</a>’;   
    }   
    
    if (” == $author_uri[1] ) {   
        $author = trim( $author_name[1] );   
    } else {   
        $author = ‘<a href=”‘ . trim( $author_uri[1] ) . ‘” title=”‘.__( ‘Visit author homepage’ ).‘”>’ . trim( $author_name[1] ) . ‘</a>’;   
    }   
    
    return array(‘Name’ => $name, ‘Title’ => $plugin, ‘Description’ => $description, ‘Author’ => $author, ‘Version’ => $version);   
}   
?>
2)启用 & 禁用插件
  启用(禁用)插件的操作都在Plugins.php中,比如我要Deactive “Hello”这个插件,最后的URL其实是这个样子:

http://localhost/blog/wp-admin/plugins.php?action=deactivate&plugin=hello.php

  其中,“Action”表示动作,值为“active”或者“deactivate”,而“Plugin”表示动作的对象插件,此处为“hello.php”。得到动作指令后,首先从数据库中取出当前已经激活的插件。

PHP代码
<?php $current = get_settings(‘active_plugins’); ?>  
然后根据动作,重新生成已激活插件数组,存入数据库,并重新加载此页。加载的时候就需要考虑这些已经激活的插件是怎么工作的了。
BTW:附上数据库的Options表中0插件和只有1个插件的值:
没有插件:

a:1:{i:0;s:0:””;}

只有Hello插件:

a:2:{i:0;s:0:””;i:1;s:9:”hello.php”;}

3)如何加载启用的插件到系统中
WordPress中的每页都会包含“wp-config.php”文件,而“wp-config.php”中也会自动加载“wp-settings.php”文件。在“wp-settings.php”文件中,可以找到以下与插件相关的代码片断:

PHP代码
<?php    
if ( get_option(‘active_plugins’) ) {   
    $current_plugins = get_option(‘active_plugins’);   
    if ( is_array($current_plugins) ) {   
        foreach ($current_plugins as $plugin) {   
            if ( ” != $plugin && 0 == validate_file($plugin) && file_exists(WP_PLUGIN_DIR . ‘/’ . $plugin) )   
                include_once(WP_PLUGIN_DIR . ‘/’ . $plugin);   
        }   
    }   
}   
?>
可见,这段代码会取出系统中所有启用的插件,并包含进来。所以在每页加载的时候,都会首先包含这些插件代码。那么,这些插件自己在加载的时候都做了什么呢?

4)插件的加载
  插件的加载其实最重要的一个部分就是插件的事件注册机制,WordPress插件中的事件注册其实和Eclipse中的扩展点(Extension-Point)机制非常相像,而这种类似“插销”、“插销座”的软件插拔方式也成为了最近软件组件架构方面应用最多的实践。
  事件注册过程中比较重要的几个函数分别是:do_action、add_action、add_filter。WordPress中默认定义了很多扩展点(也可以叫做“钩子”),或者说注册了很多系统事件(WP中的正规叫法应该是“Action Tag”),比如“admin_head”表示Admin页面的Head输出事件,“publish_post”表示发布一篇帖子的事件等等。而插件要做的就是扩展这些扩展点,或者说挂接这些钩子,从而实现系统的扩展功能。add_action就是通常插件扩展某个扩展点用到的函数,而do_action 是扩展点本身开始执行的函数。
  刚才说过WordPress中的每一页执行前都会Include所有Active的插件代码,而这些代码通常都会用“add_action”来将自己的函数注册到系统的扩展点中。这样,在扩展点执行的时候,就会找到系统中所有已经挂接到这个扩展点上的插件的函数来执行之,从而扩充系统的功能。
  WordPress中的很多功能也都是通过这种插件结构来实现的,默认注册了很多系统事件,都在 “default-filter.php”中。比如:

PHP代码
<?php add_action(‘publish_post’, ‘generic_ping’); ?>  
这个是用来在发布每篇帖子的时候发送XML-RPC Ping的。再比如:

PHP代码
<?php add_filter(‘the_content’, ‘convert_smilies’); ?>  
用来将正文(content)中的笑脸符号转换为图像。
  还是举“Hello”插件来说。Hello插件会随机的在Admin Page的右上角显示一段话,它的工作原理是这样的:
  在每一个Admin page的前面都有

PHP代码
<?php require_once(‘admin-header.php’); ?>  
而在“admin-head.php”中将会执行扩展点“admin-head”的所有扩展:

PHP代码
<?php do_action(‘admin_head’, ”); ?>  
这样,就会执行所有挂接到admin_head的函数,Admin Page 的 Footer 部分也是类似。

5)如果插件中涉及UI
  其实是一样的。以WordPress FeedBurner Plugin中添加菜单为例。
  如果想添加一个菜单,就需要注册“admin_menu”这个Action Tag(系统事件)即可:

PHP代码
<?php add_action(‘admin_menu’, ‘ol_add_feedburner_options_page’); ?>  
插件中的这个函数为:

PHP代码
<?php   
function ol_add_feedburner_options_page() {   
    if (function_exists(‘add_options_page’)) {   
        add_options_page(‘FeedBurner’, ‘FeedBurner’, 8, basename(__FILE__), ‘ol_feedburner_options_subpanel’);   
    }   
}   
?>
“add_options_page”这个函数就会在系统的“Options”菜单中添加“FeedBurner”这样一个子菜单。

6)其它
  还有一些简单的插件就是只提供一些API函数。比如Most_Commented Plugin,它提供一个API “mdv_most_commented”:通过数据库查询得到评论最多的文章,并加以显示。因为这个插件已经被Include过,所以可以用这个API 来进行显示。

2010年03月13日

wordpress二次开发常用函数

类归于: wordpress — admin @ 10:46 上午

      这里只列出最常用的几个。先是重要的bloginfo(),不直接echo数值的函数为get_bloginfo();恩,不少函数都是按照这个规则写的,PS:并不是全部!echo出函数值的函数为xxx()的话,那么不echo出结果值的的函数为get_xxx()。
      该函数范围的是WordPress的配置参数,主要参数和值举例如下,常用的黑体标出:
     admin_email = admin@example
     atom_url = http://example/home/feed/atom
     charset = UTF-8
     comments_atom_url = http://example/home/comments/feed/atom
     comments_rss2_url = http://example/home/comments/feed
     description = Just another WordPress blog
     home = http://example/home这个是网站首页地址
     html_type = text/html
     language = en-US
     name = Testpilot网站名称
     pingback_url = http://example/home/wp/xmlrpc.php
     rdf_url = http://example/home/feed/rdf
     rss2_url = http://example/home/feed
     rss_url = http://example/home/feed/rss
     siteurl = http://example/home也是网站地址
     stylesheet_directory = http://example/home/wp/wp-content/themes/largo
     stylesheet_url = http://example/home/wp/wp-content/themes/largo/style.css主题文件夹下的style.css地址
     template_directory = http://example/home/wp/wp-content/themes/largo主题包地址
     template_url = http://example/home/wp/wp-content/themes/largo
     text_direction = ltr
     url = http://example/home
     version = 2.7
     wpurl = http://example/home/wp
     
     举例,bloginfo(“wpurl “)会输出“http://example/home/wp”。

      下边是主题中的几个常用函数。

<?php get_header();//调用header.php ?>
<?php get_sidebar();//调用sidebar.php ?>
<?php get_footer();//调用footer.php ?>
//下边就比较多了,先来分类的。
<?php wp_list_categories( $args ); ?>
<?php $args = array(
  'show_option_all'    => ,
  'orderby'            => 'name',
  'order'              => 'ASC',
  'show_last_update'   => 0,
  'style'              => 'list',
  'show_count'         => 0,
  'hide_empty'         => 1,
  'use_desc_for_title' => 1,
  'child_of'           => 0,
  'feed'               => ,
  'feed_type'          => ,
  'feed_image'         => ,
  'exclude'            => ,
  'exclude_tree'       => ,
  'include'            => ,
  'current_category'   => 0,
  'hierarchical'       => true,
  'title_li'           => __( 'Categories' ),
  'number'             => NULL,
  'echo'               => 1,
  'depth'              => 0
);
?>
//我估计这个是参数最复杂的一个函数了,改函数的用途自然是输出分类,对了,WordPress最多支持4级分类。
//另外经常用到的就是get_the_category(),例:
<?php
foreach((get_the_category()) as $category) {
    echo $category->cat_name . ' ';
}
?>
可以在循环读取分类后,输出每个分类的名称,该项全部变量为:
cat_ID
    the category id (also stored as 'term_id') 分类ID
cat_name
    the category name (also stored as 'name') 分类名称
category_nicename
    a slug generated from the category name (also stored as 'slug') slug名称
category_description
    the category description (also stored as 'description') 分类描述
category_parent
    the category id of the current category's parent. '0' for no parents. (also stored as 'parent') 父分类id
category_count
    the number of uses of this category (also stored as 'count') 分类包含的日志数。
//接下去是判断分类的,通常用在sidebar和single里用来判断当前内容的分类信息。
<?php in_category( $category, $_post ) ?>
//例子:
<?php
if (in_category('pachoderms')){
    //分类pachoderms
}elseif(in_category(array('Tropical Birds','small-mammals'))){
    //分类Tropical Birds和small-mammals
}elseif(in_category(array('1','5'))){
    //分类1,5
}else{
    //& c.
}
?>
//page页的,Displays a list of pages in a select (i.e dropdown) box with no submit button. 输入全部page的下拉选择框。
<?php wp_dropdown_pages( $args ); ?>
//$args默认设置,恩,只介绍常用的。
<?php $args = array(
    'depth'            => 0,
    'child_of'         => 0,//父类page的ID
    'selected'         => 0,
    'echo'             => 1,
    'name'             => 'page_id',//page_id
    'show_option_none' => //是否列出空项
    'exclude'          => //不包含的page_id
    'exclude_tree'     => //不包含某个page_id为顶点的page树
);
?>
//Displays a list of WordPress Pages as links.直接输出page页,当然是带链接的。
<?php wp_list_pages( $args ); ?>
<?php $args = array(
    'depth'        => 0,
    'show_date'    => ,
    'date_format'  => get_option('date_format'),
    'child_of'     => 0,
    'exclude'      => ,
    'title_li'     => __('Pages'),//列表前的标题,不要就留空。
    'echo'         => 1,
    'authors'      => ,//作者
    'sort_column'  => 'menu_order, post_title',
    'link_before'  => ,//内容输出前的内容
    'link_after'   => ,//内容输出后的内容,这2个通常用来做custom化。
    'exclude_tree' =>
);
?>
//友情链接部分
<?php wp_list_bookmarks( $args ); ?>
<?php $args = array(
    'orderby'          => 'name',
    'order'            => 'ASC',
    'limit'            => -1,
    'category'         => ,
    'category_name'    => ,
    'hide_invisible'   => 1,
    'show_updated'     => 0,
    'echo'             => 1,
    'categorize'       => 1,
    'title_li'         => __('Bookmarks'),
    'title_before'     => '<h2>',
    'title_after'      => '</h2>',
    'category_orderby' => 'name',
    'category_order'   => 'ASC',
    'class'            => 'linkcat',
    'category_before'  => '<li id=\"%id\" class=\"%class\">',
    'category_after'   => '</li>'
);
//相对来说比较复杂的函数,不过基本不传任何参数,使用默认的就行。
//不怎么用的的wp_list_comments()回复列表函数,参数也不介绍了~
<?php wp_list_comments();?>

接着嘛,是万用数据库查询函数,记住不是query_posts()而是WP_Query()哦!这个函数很强大,具体参照《WordPress-WP_Query》,里边也有讲为何不建议使用query_posts()的原因。

//最后是设计到具体内容后的函数部分,这部分通常是在循环体内,比如:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
//code here~
<?php endif; ?>
也有带循环的:
<?php while (have_posts()) : the_post(); ?>
//code here~
<?php endwhile; ?>
the_ID()
the_title() 标题,常用
the_title_attribute() (Version 2.3)
single_post_title()
the_title_rss()
the_content() 内容,常用
the_content_rss()
the_excerpt() 日志中有more标签时输出more前的内容,参数是查看全部内容的链接名,重要
the_excerpt_rss()
next_post_link() 后一个日志的链接,重要
previous_post_link() 前一个日志的链接,重要
next_posts_link() 后xx个日志,基本上用不到这个,而用WP-PageNavi插件实现
previous_posts_link() 同上
posts_nav_link()
sticky_class() (Version 2.7)
the_meta()
the_tags() (Version 2.3) 日志tag,重要
the_time() 时间
the_date() 日期
the_permalink() 固定链接
wp_loginout() 登出
wp_register() 注册用户

WordPress 所驱动