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代码

<?php bloginfo(‘name’); ?> 网站标题
<?php wp_title(); ?> 日志或页面标题
<?php bloginfo(’stylesheet_url’); ?> WordPress主题样式表文件style.css的相对地址
<?php bloginfo(‘pingback_url’); ?> WordPress博客的Pingback地址
<?php bloginfo(‘template_url’); ?> WordPress主题文件的相对地址
<?php bloginfo(‘version’); ?> 博客的Wordpress版本
<?php bloginfo(‘atom_url’); ?> WordPress博客的Atom地址
<?php bloginfo(‘rss2_url’); ?> WordPress博客的RSS2地址
<?php bloginfo(‘url’); ?> WordPress博客的绝对地址
<?php bloginfo(‘name’); ?> WordPress博客的名称
<?php bloginfo(‘html_type’); ?> 网站的HTML版本
<?php bloginfo(‘charset’); ?> 网站的字符编码格式

WordPress 主体模板 PHP代码

<?php the_content(); ?> 日志内容
<?php if(have_posts()) : ?> 确认是否有日志
<?php while(have_posts()) : the_post(); ?> 如果有,则显示全部日志
<?php endwhile; ?> 结束PHP函数”while”
<?php endif; ?> 结束PHP函数”if”
<?php get_header(); ?> header.php文件的内容
<?php get_sidebar(); ?> sidebar.php文件的内容
<?php get_footer(); ?> footer.php文件的内容
<?php the_time(‘m-d-y’) ?> 显示格式为”02-19-08″的日期
<?php comments_popup_link(); ?> 显示一篇日志的留言链接
<?php the_title(); ?> 显示一篇日志或页面的标题
<?php the_permalink() ?> 显示一篇日志或页面的永久链接/URL地址
<?php the_category(‘, ‘) ?> 显示一篇日志或页面的所属分类
<?php the_author(); ?> 显示一篇日志或页面的作者
<?php the_ID(); ?> 显示一篇日志或页面的ID
<?php edit_post_link(); ?> 显示一篇日志或页面的编辑链接
<?php get_links_list(); ?> 显示Blogroll中的链接
<?php comments_template(); ?> comments.php文件的内容
<?php wp_list_pages(); ?> 显示一份博客的页面列表
<?php wp_list_cats(); ?> 显示一份博客的分类列表
<?php next_post_link(‘ %link ‘) ?> 下一篇日志的URL地址
<?php previous_post_link(‘%link’) ?> 上一篇日志的URL地址
<?php get_calendar(); ?> 调用日历
<?php wp_get_archives() ?> 显示一份博客的日期存档列表
<?php posts_nav_link(); ?> 显示较新日志链接(上一页)和较旧日志链接(下一页)
<?php bloginfo(‘description’); ?> 显示博客的描述信息

其它的一些Wordpress模板代码

/%postname%/ 显示博客的自定义永久链接
<?php the_search_query(); ?> 搜索表单的值
<?php _e(‘Message’); ?> 打印输出信息
<?php wp_register(); ?> 显示注册链接
<?php wp_loginout(); ?> 显示登入/登出链接
<!–next page–> 在日志或页面中插入分页
<!–more–> 截断日志
<?php wp_meta(); ?> 显示管理员的相关控制信息
<?php timer_stop(1); ?> 显示载入页面的时间
<?php echo get_num_queries(); ?> 显示载入页面查询

看到这里的时侯,有人露出不屑的神色。太菜了?看样子,不露出点真功夫还不能服众啊!接着,再来:

指定example.php中的内容只在首页显示

<?php if ( is_home() ) { include (‘example.php’); } ?>

为不同分类指定不同的样式表

<?php if ( is_category(’15′) ) {<link rel="stylesheet" href="<?php bloginfo(‘template_url’); ?>/cat-15.CSS"

type="text/css" media="screen" />;

<?php } else { ?>

<link rel="stylesheet" href="<?php bloginfo(‘stylesheet_url’); ?>"

type="text/css" media="screen" />

<?php } ?>

为不同的分类指定不同的图像

<?php if (is_category(’7′) ):<img src=’<?php bloginfo(‘template_url’);?>/images/cat7.jpg’ alt=” />

<?php } elseif (is_category(’8′) ):

<img src=’<?php bloginfo(‘template_url’);?>/images/cat8.jpg’ alt=” />

<?php endif; ?>

样式化单篇日志

<div id="post-<?php the_ID();?>">This snippet will assign the post ID to the DIV. For example, if the ID for the post is 8, that line will echo as

<div id=”post-8”></div>. Now you can style that individual post in the CSS as #post-8. Place this code

within the loop.

上一页和下一页链接

<?php next_posts_link(‘Next Entries »’) ?><?php previous_post_link(‘« Older Entries’); ?>

动态页面链接

<ul><li<?php if(is_home()) { ?> class="current_page_item"<?php } ?>><a href="

<?php bloginfo(‘home’); ?>">home</a></li>

<?php wp_list_pages(‘sort_column=menu_order&depth=1&title_li=’); ?>

</ul>

This snippet will rst echo the text “home” with a link to the home page. Next, it will echo the WordPress

pages links in a list, in order dened by your settings, excluding the child pages, and excluding a title

header for the list. If one of the pages in the list is active, the link for that page will be assigned the class

“current_page_item”, which can now be styled in your CSS. Place this code in the template les.

动态页面标题

<?phpif (is_home()) { echo bloginfo(‘name’); } elseif (is_404()) { echo ‘WPCandy » 404′; } elseif(is_search()) { echo ‘WPCandy » Search Results’; } else { echo ‘WPCandy » ‘; wp_title(”); }

?>

分类日志

<?php query_posts(‘cat=2&showposts=5′); ?>

CSS样式表头部声明

/*

Theme Name:  WPChineseoa

Description: Description goes here

Theme URI: http:www.2leee.com

Version: 2.0

Author: TonyDeng

Author URI: http:www.2leee.com

Template: Dene a parent template (optional)

*/

日志循环

The Loop<?php if(have_posts()) : ?><?php while(have_posts()) : the_post(); ?>

// this is the inside of the loop

<?php endwhile; ?>

<?php else : ?>

<?php endif; ?>

标签云(Tag cloud)

<?php wp_tag_cloud(‘smallest=1&largest=9&’); ?>

页面模板头部声明

<?php/*

Template Name: Gallery

*/

?>

为每个分类指定不同的模板

<?php$post = $wp_query- >post;if ( in_category(’3′) ) {

include(TEMPLATEPATH . ‘/cat3.php’);

} elseif ( in_category(’4′) ) {

include(TEMPLATEPATH . ‘/cat4.php’);

} else {

include(TEMPLATEPATH . ‘/cat.php’);

} ? >

本文作者墨绿来自广州SEO博客

One Comment, Comment or Ping

  1. 毫无疑问,我是来顶博主的..

Reply to “WordPress模版调用的PHP代码”