一、header.php文件中用到的函数
(1)同一个首部文件,但当加载不同页面时需要的判断
is_home():当前页面为主页时返回true
is_category():当前页面为分类页时返回true
is_single():当前页面为单文章页时返回true
is_page():当前页面为单页面时返回true
(2)当引用style.css样式文件时
<link rel="stylesheet" href="<?php bloginfo('stylesheet_url');?>" type="text/css" media="screen" />
//如果名字不是style.css 或者在文件夹下 则需要用到 template_url()
//如引用test.css文件
<link rel="stylesheet" href=<?php bloginfo('template_url'); ?>/test.css" type="text/css" media="screen" />
bloginfo('template_url'); 用于输出主题目录的URL。
bloginfo('stylesheet_url');输出的是你的主题style.css文件绝对网址
(3)当作者文章被引用链接时,可获得通知
<link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" />
(4)当输出博客信息时
<?php echo get_option('home'); ?> 输出你的博客首页网址
<?php bloginfo('name'); ?> 输出你的博客名称
<?php bloginfo('description'); ?> 输出博客描述
(5)分类和页面如何做导航栏
<ul>
<li<?php if (is_home()) { echo ' class="current-cat"';} ?>><a title="Home" href="/">博客主页</a></li>
<?php
$currentcategory = '';
6>// 以下这行代码用于在导航栏添加分类列表,
7>// 如果你不想添加分类到导航中,
8>// 请删除 6 - 14 行代码
if (is_category() || is_single()) {
$catsy = get_the_category();
$myCat = $catsy[0]->cat_ID;
$currentcategory = '¤t_category='.$myCat;
}
14> wp_list_categories('depth=1&title_li=&show_count=0&hide_empty=0&child_of=0'.$currentcategory);
16>// 以下这行代码用于在导航栏添加页面列表
// 如果你不想添加页面到导航中,
// 请删除16 - 19行代码
19> wp_list_pages('depth=1&title_li=&sort_column=menu_order');
?>
</ul>
二、siderbar.php page.php single.php等 用到的函数
<?php get_header(); ?> 从当前主题文件夹中包含header.php文件
is_home(),is_single(),is_category()等几个条件判断标签
<?php bloginfo('stylesheet_url'); ?>输出主题文件夹中style.css文件的路径
<?php bloginfo('pingback_url'); ?> 输出博客pingback网址
<?php bloginfo('template_url'); ?> 输出博客主题目录URL
<?php echo get_option('home'); ?> 输出你的博客首页网址
<?php bloginfo('name'); ?> 输出你的博客名称
<?php bloginfo('description'); ?> 输出博客描述
<?php wp_head(); ?> 用于包含WordPress程序输出头部信息
<?php wp_list_categories(); ?> 用于列出博客分类页
<?php wp_list_pages(); ?> 用于列出博客页面
<?php the_permalink(); ?> 输出文章的URL链接(参考文档)
<?php the_title(); ?> 输出文章的标题
(1)获取最新文章
<ul>
<?php
$posts = get_posts('numberposts=6&orderby=post_date');
foreach ($posts as $post) {
setup_postdata($post);
echo '<li><a href ="'.get_permalink().'">' .get_the_title().'</a></li>';
}
$post = $posts[0];
?>
</ul>
(2)获取单页面
<?php if (have_posts()) : the_post(); update_post_caches($posts); ?>
<h2 class="grid_12 caption clearfix"><?php the_title(); ?></h2>
<div class="grid_8">
//获取页面内容
<?php the_content(); ?>
//获取评论
<?php comments_template(); ?>
</div>
<?php else : ?>
<div class="grid_8">
没有找到你想要的页面!
</div>
<?php endif; ?>
(3)循环获取文章列表
<?php if(have_posts()) :
while(have_posts()):
the_posts();
?>
<div class="post">
<!-- 文章标题-->
<h3 class="title"><a href="<?php the_permalink();?>" rel="bookmark"><?php tht_title();?></a></h3>
<!-- 文章数据 -->
<p class="sub"><?php the_tags('标签1',',','');?> • <?php the_time('Y年n月j日') ?>
• <a href="#"><?php comments_popop_link('0条评论','1条评论','%条评论','评论已关闭');?><?php edit_post_lini('编辑','~','');?></a></p>
<div class="hr dotted clearfix"> </div>
<!-- 文章图片 -->
<!-- 同样的道理,图片也换成绝对的地址 函数先获取主题根目录-->
<img class="thumb" alt="" src="<?php bloginfo('template_url');?>/images/610x150.gif" />
<!-- 文章内容 -->
<?php the_excerpt();?>
<!-- 阅读更多按钮-->
<p class="clearfix"><a href="<?php the_petmalink();?>" class="button right"> <?php the_content('阅读全文。。。');?></a></p>
</div>
<div class="hr clearfix"> </div>
<?php endwhile;?>
<!-- 文章分页 -->
<p class="clearfix"><?php preveious_posts_link('<<查看新文章',0);?>
<span class="float right"><?php next_post_link('查看旧文章>>',0);?></span>
</p>
<?php else: ?>
<h3 class="title"> <a href="#" rel="bookmark">未找到</a></h3>
<p>没有找到任何文章</p>
<?php endif; ?>
三、comments.php
get_avatar($comment, 48) 获取评论者的gravatar头像,尺寸为48 * 48
comment_reply_link() 回复留言的链接
get_comment_author_link 用于获取评论者博客地址
get_comment_time 获取评论发布时间
edit_comment_link 管理员修改评论的链接
comment_text() 输出评论内容
is_user_logged_in 判断用户是否登录
wp_login_url 博客登录地址
get_comment_author_link 用于获取评论者博客地址
$comment_author 读取cookie,如果该用户之前已经发表过评论则自动帮助用户填写用户名
$comment_author_email 读取cookie,如果该用户之前已经发表过评论则自动帮助用户填写Email
$comment_author_url 读取cookie,如果该用户之前已经发表过评论则自动帮助用户填写博客地址
do_action('comment_form', $post->ID); 该函数为某些插件预留
wp_logout_url 退出登录的链接
(1)发表评论
<!--发表评论 -->
<?php
if(!comments_open()) :
elseif(get_option('comment_registration') && !is_user_logged_in()) :
?>
<p>你必须<a href="<?php echo wp_login_url(get_permalink());?>">登陆</a>才能发表评论</p>
<?php else : ?>
<form id="comment_form" action="<?php echo get_option('siteurl');?>/wp-comments-post.php" method="post">
<h3>Add a comment</h3>
<div class="hr dotted clearfix"> </div>
<ul>
//用户信息列表
<?php else: ?>
<li class="clearfix">您已登陆<a href="<?php echo get_option('siteurl');?>/wp-admin/profile.php"><?php echo $user_identify;?></a> <a href="<?php echo wp_logout_url(get_permalink());?>" title="退出登录">退出</a></li>
<li class="clearfix">
<label for="message">评论内容</label>
<textarea id="message" name="message" rows="3" cols="40"></textarea>
</li>
<li class="clearfix">
<a href="javascript:void(0);" onClick="Javascript:document.forms['commentform'].submit()" class="button medium black right">发表评论</a> </li>
</ul>
<?php comment_id_fields();?>
<?php do_action('comment_form',$post->ID);?>
</form>
<?php endif;?>