一、 WordPress 模板基本文件

  • style.css 样式表文件
  • index.php 主页文件
  • single.php 日志单页文件
  • page.php 页面文件
  • archvie.php 分类和日期存档页文件
  • searchform.php 搜索表单文件
  • search.php 搜索页面文件
  • comments.php 留言区域文件 (包括留言列表和留言框)
  • 404.php 404 错误页面
  • header.php 网页头部文件
  • sidebar.php 网页侧边栏文件
  • footer.php 网页底部文件

二、 WordPress Header 头部 PHP 代码

  • 注: 也就是位于<head> 和</head> 之间的 PHP 代码
  1. <?php bloginfo('name'); ?>
  • WordPress 主题样式表文件 style.css 的相对地址
  1. <?php bloginfo('stylesheet_url'); ?>
  1. <?php bloginfo('pingback_url'); ?>
  1. <?php bloginfo('template_url'); ?>
  1. <?php bloginfo('version'); ?>
  1. <?php bloginfo('atom_url'); ?>
  1. <?php bloginfo('rss2_url'); ?>
  1. <?php bloginfo('url'); ?>
  1. <?php bloginfo('name'); ?>
  1. <?php bloginfo('html_type'); ?>
  1. <?php bloginfo('charset'); ?>

三、 WordPress 主体模板 PHP 代码

  1. <?php if(have_posts()) : ?>
  1. <?php while(have_posts()) : the_post(); ?>
  1. <?php the_time('m-d-y') ?>
  1. <?php comments_popup_link(); ?>
  1. <?php the_category(', ') ?>
  1. <?php edit_post_link(); ?>
  1. <?php get_links_list(); ?>
  1. <?php comments_template(); ?>
  1. <?php wp_list_pages(); ?>
  1. <?php next_post_link('%link') ?>
  1. <?php previous_post_link('%link') ?>
  1. <?php wp_get_archives() ?>
  • 显示较新日志链接 (上一页) 和较旧日志链接 (下一页)
  1. <?php posts_nav_link(); ?>
  1. <?php bloginfo('description'); ?>

四、其它的一些 WordPress 模板代码

  1. <?php the_search_query(); ?>
  1. <?php echo get_num_queries(); ?>

五、 WordPress 调用的常用方法

  • 1 、 WordPress 最新文章的调用可以使用一行很简单的模板标签 wp_get_archvies 来实现. 代码如下:
  1. <?php get_archives('postbypost', 10); ?>
  1. <?php wp_get_archives('type=postbypost&limit=20&format=custom'); ?>
  • 后面这个代码显示你博客中最新的 20 篇文章,其中 format=custom 这里主要用来自定义这份文章列表的显示样式。具体的参数和使用方法你可 以参考官方的使用说明- wp_get_archvies 。 (fromat=custom 也可以不要,默认以 UL 列表显示文章标题。)

补充: 通过 WP 的 query_posts() 函数也能调用最新文章列表, 虽然代码会比较多一点,但可以更好的控制 Loop 的显示,比如你可以设置是否显示摘要。具体的使用方法也可以查看官方的说明。

  1. <?php
  2. $rand_posts = get_posts(『numberposts=10&orderby=rand』);
  3. foreach( $rand_postsas$post ) :
  4. ?>
  5. <!–下面是你想自定义的 Loop–>
  6. <li><a href="<?php%20the_permalink();%20?>"><?php the_title(); ?></a></li>
  7. <?php endforeach; ?>
  • 3. WordPress 调用最新留言
    下面是我之前在一个 WordPress 主题中代到的最新留言代码,具体也记不得是哪个主题了。该代码直接调用数据库显示一份最新留言。其中 LIMIT10 限制留言显示数量。绿色部份则是每条留言的输出样式。
  1. <?php
  2. global$wpdb;
  3. $sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID,
  4. comment_post_ID, comment_author, comment_date_gmt, comment_approved,
  5. comment_type,comment_author_url,
  6. SUBSTRING(comment_content,1,30) AS com_excerpt
  7. FROM $wpdb->comments
  8. LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID =
  9. $wpdb->posts.ID)
  10. WHERE comment_approved = '1' AND comment_type = " AND
  11. post_password = "
  12. ORDER BY comment_date_gmt DESC
  13. LIMIT 10";
  14. $comments = $wpdb->get_results($sql);
  15. $output = $pre_HTML; foreach ($commentsas$comment) {
  16. $output .= "n<li>".strip_tags($comment->comment_author)
  17. .":" . 」 <a href="". get_permalink($comment->ID) .
  18. "#comment-". $comment->comment_ID . "" title="on" .
  19. $comment->post_title . "">" . strip_tags($comment->com_excerpt)
  20. ."</a></li>";
  21. } $output .= $post_HTML;
  22. echo$output;?>
  • 4.WordPress 调用相关文章
    在文章页显示相关文章
  1. <?php
  2. $tags = wp_get_post_tags($post->ID);
  3. if ($tags) {
  4. $first_tag = $tags[0]->term_id;
  5. $args=array(
  6. 'tag__in' => array($first_tag),
  7. 'post__not_in'=> array($post->ID),
  8. 'showposts'=>10,
  9. 'caller_get_posts'=>1
  10. );
  11. $my_query = new WP_Query($args);
  12. if( $my_query->have_posts() ) {
  13. while ($my_query->have_posts()) : $my_query->the_post(); ?>
  14. <li><a href="<?php%20the_permalink()%20?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title();?> <?php comments_number(' ','(1)','(%)'); ?></a></li>
  15. <?php
  16. endwhile;
  17. }
  18. }
  19. wp_reset_query();
  20. ?>
  1. <?php $posts = get_posts( "category=4&numberposts=10" ); ?>
  2. <?php if( $posts ) : ?>
  3. <ul><?php foreach( $postsas$post ) : setup_postdata( $post ); ?>
  4. <li>
  5. <a href="<?php%20the_permalink()%20?>" rel=」bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>
  6. </li>
  7. <?php endforeach; ?>
  8. </ul>
  9. <?php endif; ?>
  1. <?php
  2. global$wpdb;
  3. $sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID,
  4. comment_post_ID, comment_author, comment_date_gmt, comment_approved,
  5. comment_type,comment_author_url,
  6. SUBSTRING(comment_content,1,14) AS com_excerpt
  7. FROM $wpdb->comments
  8. LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID =
  9. $wpdb->posts.ID)
  10. WHERE comment_approved = '1' AND comment_type = " AND
  11. post_password = "
  12. ORDER BY comment_date_gmt DESC
  13. LIMIT 10";
  14. $comments = $wpdb->get_results($sql);
  15. $output = $pre_HTML;
  16. foreach ($commentsas$comment) {
  17. $output .= "
    <li>".strip_tags($comment->comment_author)
  18. .":" . " <a href="" . get_permalink($comment->ID) .
  19. "#comment-" . $comment->comment_ID . "" title="on " .
  20. $comment->post_title . "">" . strip_tags($comment->com_excerpt)
  21. ."</a></li>";
  22. }
  23. $output .= $post_HTML;
  24. echo$output;?>
  • 7.WordPress 调用含 gravatar 头像的评论输出
  1. <?php
  2. global$wpdb;
  3. $sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID, comment_post_ID, comment_author, comment_date_gmt, comment_approved,comment_author_email, comment_type,comment_author_url, SUBSTRING(comment_content,1,10) AS com_excerpt FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) WHERE comment_approved = '1' AND comment_type = " AND comment_author != '笔者' AND post_password = 」 ORDER BY comment_date_gmt DESC LIMIT 10";
  4. $comments = $wpdb->get_results($sql);
  5. $output = $pre_HTML;
  6. foreach ($commentsas$comment) {
  7. $output .= "
    <li>".get_avatar(get_comment_author_email('comment_author_email'), 18). " <a href="" . get_permalink($comment->ID) . "#comment-" . $comment->comment_ID . "" title="" . $comment->post_title . " 上的评论">". strip_tags($comment->comment_author) .": ". strip_tags($comment->com_excerpt) ."</a></li>";
  8. }
  9. $output .= $post_HTML;
  10. $output = convert_smilies($output);
  11. echo$output;
  12. ?>

上面代码把 comment_author 的值改成你的 ID,18 是头像大小,10 是评论数量。

  1. <?php $count_posts = wp_count_posts(); echo$published_posts = $count_posts->publish;?>
  1. <?php $count_posts = wp_count_posts(); echo$draft_posts = $count_posts->draft; ?>
  1. <?php echo$wpdb->get_var("SELECT COUNT(*) FROM $wpdb->comments");?>
  1. <?php echofloor((time()-strtotime("2008-8-18"))/86400); ?>
  1. <?php echo$count_tags = wp_count_terms('post_tag'); ?>
  1. <?php $count_pages = wp_count_posts('page'); echo$page_posts = $count_pages->publish; ?>
  1. <?php echo$count_categories = wp_count_terms('category'); ?>
  1. <?php $link = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->links WHERE link_visible = 'Y'"); echo$link; ?>
  1. <?php $users = $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->users"); echo$users; ?>
  1. <?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; ?>
  1. is_author('Elite Hacker')
  • 上面是通过不同的判断实现以年、月、日、时间等方式来显示归档
  • 判断是否翻页,比如你当前的 blog 是 http://domain.com 显示 http://domain.com?paged=2 的时候,这个判断将返 回真,通过这个函数可以配合 is_home 来控制某些只能在首页显示的界面,例如:

//这里写你想显示的内容,包括函数

或者:

  1. <?php if(is_home() && !is_paged() ):?>

//这里写你想显示的内容,包括函数

显示 Blogroll 中的链接;

  1. <!–smilies–>
  2. <?php
  3. function<s/ol/li .= "
    - pan> wp_smilies() {
  4. global$wpsmiliestrans;
  5. if ( !get_option('use_smilies') or (emptyempty($wpsmiliestrans))) return;
  6. $smilies = array_unique($wpsmiliestrans);
  7. $link=";
  8. foreach ($smiliesas$key => $smile) {
  9. $file = get_bloginfo('wpurl').'/wp-includes/images/smilies/'.$smile;
  10. $value = " ".$key." ";
  11. $img = "<img src="{$file}" alt="{$smile}" />";
  12. $imglink = htmlspecialchars($img);
  13. $link .= "<a href="#commentform" title="{$smile}" onclick="document.getElementById('comment').value += '{$value}'">{$img}</a>&nbsp;";
  14. }
  15. echo '<div>'.$link.'</div>';
  16. }
  17. ?>
  18. <?php wp_smilies();?>
  19. <!–smilies—>
  • 将以上代码复制到 comments.php 中合适的位置:

留着自己学习之用!