@52fhy
2015-12-05T13:43:04.000000Z
字数 6815
阅读 485
WordPress
常用方法:
get_header()就相当于将header.php中的代码拷贝到当前的php文件。
get_footer()
get_sidebar()
get_template_part( 'includes/nav' ) 获取主题下includes/nav.php
get_template_part( 'content','2' )获取主题下content-2.php
is_home():当前页面为主页时返回true
is_category():当前页面为分类页时返回true
is_single():当前页面为单文章页时返回true
is_page():当前页面为单页面时返回true
更详细的内容参阅WordPress文档:条件标签
bloginfo('name') 输出的是博客名
bloginfo('stylesheet_url') 输出的是当前主题css文件style.css
bloginfo('template_url') 获取当前主题根目录的URL
<?php bloginfo('template_url'); ?>/abc.css
bloginfo('pingback_url')
get_option('home');博客首页网址,get_option()返回键值对应的配置值,取至wp_options表
ot_get_option('favicon')
wp_head(): 有些插件需要在网页头部执行一些类如添加一些js或css的动作,要让这些插件能够正常的工作,也让你的主题有更好的兼容性,你应该添加wp_head()函数。打开header.php,在</head>前面添加。
wp_footer()
wp_list_pages() 用于列出博客页面,即导航菜单
header.php
页面内容:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>
<?php if ( is_home() ) {
bloginfo('name'); echo " - "; bloginfo('description');
} elseif ( is_category() ) {
single_cat_title(); echo " - "; bloginfo('name');
} elseif (is_single() || is_page() ) {
single_post_title();
} elseif (is_search() ) {
echo "搜索结果"; echo " - "; bloginfo('name');
} elseif (is_404() ) {
echo '页面未找到!';
} else {
wp_title('',true);
} ?>
</title>
<!-- Stylesheets -->
<link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" />
<link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" />
<link rel="alternate" type="application/rss+xml" title="RSS 2.0 - 所有文章" href="<?php bloginfo('rss2_url'); ?>" />
<link rel="alternate" type="application/rss+xml" title="RSS 2.0 - 所有评论" href="<?php bloginfo('comments_rss2_url'); ?>" />
<!--
<?php wp_head(); ?>
-->
<?php flush(); ?>
</head>
<body>
<div id="wrapper" class="container_12 clearfix">
<!-- Text Logo 博客名称-->
<h1 id="logo" class="grid_4"><a href="<?php echo get_option('home')?>"><?php bloginfo('name')?></a><h1>
<!-- Navigation Menu 导航菜单-->
<ul id="navigation" class="grid_8">
<!-- MENU -->
<?php wp_list_pages('depth=1&title_li=0&sort_column=menu_order'); ?>
<!-- HOME -->
<li <?php if (is_home()) { echo 'class="current"';} ?>><a title="<?php bloginfo('name'); ?>" href="<?php echo get_option('home'); ?>/">主页</a></li>
</ul>
<div class="hr grid_12 clearfix"> </div>
<!-- Caption Line 描述-->
<h2 class="grid_12 caption clearfix"><?php bloginfo('description')?></h2>
<div class="hr grid_12 clearfix"> </div>
footer.php
页面内容:
<!-- Footer -->
<p class="grid_12 footer clearfix">
<span class="float">版权所有 © 2015 <?php bloginfo('name'); ?>
| Powered By <a rel="external" title="WordPress主页" class="link" href="http://wordpress.org/">WordPress</a>
| Design By QwibbleDesigns |
Code By <a href="http://www.ludou.org/">Ludou</a>
</span>
<a class="float right" href="#">top</a> </p>
</div>
<!--end wrapper-->
<!--
<?php wp_footer(); ?>
-->
</body>
</html>
sidebar.php
页面内容:
<!-- Column 2 / Sidebar -->
<div class="grid_4">
<?php if ( !function_exists('dynamic_sidebar')
|| !dynamic_sidebar('First_sidebar') ) : ?>
<h4>分类目录</h4>
<ul>
<?php wp_list_categories('depth=1&title_li=&orderby=id&show_count=0&hide_empty=1&child_of=0'); ?>
</ul>
<?php endif; ?>
<?php if ( !function_exists('dynamic_sidebar')
|| !dynamic_sidebar('Second_sidebar') ) : ?>
<h4>最新文章</h4>
<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>
<?php endif; ?>
<?php if ( !function_exists('dynamic_sidebar')
|| !dynamic_sidebar('Third_sidebar') ) : ?>
<h4>标签云</h4>
<p><?php wp_tag_cloud('smallest=8&largest=22'); ?></p>
<?php endif; ?>
<?php if ( !function_exists('dynamic_sidebar')
|| !dynamic_sidebar('Fourth_sidebar') ) : ?>
<h4>文章存档</h4>
<ul>
<?php wp_get_archives('limit=10'); ?>
</ul>
<?php endif; ?>
</div>
<div class="hr grid_12 clearfix"> </div>
注释:
wp_list_categories('depth=1&title_li=&orderby=id&show_count=0&hide_empty=1&child_of=0')分类目录
get_posts('numberposts=6&orderby=post_date')最新文章
wp_tag_cloud('smallest=8&largest=22')标签云
wp_get_archives('limit=10')文章存档
index.php
注释:
<?php the_permalink(); ?> 输出文章的URL链接(参考文档)
<?php the_title(); ?> 输出文章的标题(参考文档)
<?php the_tags('标签:', ', ', ''); ?>
<?php the_time('Y年n月j日') ?>
<?php comments_popup_link('0 条评论', '1 条评论', '% 条评论', '', '评论已关闭'); ?>
<?php edit_post_link('编辑', ' • ', ''); ?>
<?php the_excerpt(); ?>首页显示的就是摘要,如果不填就输出全文
<?php the_content('阅读全文...'); ?>用于输出全文,除非你在文章中使用了<!-- more -->
<?php next_posts_link(); ?>下一页
<?php previous_posts_link(); ?>上一页
<?php previous_post_link('【上篇】%link') ?><br/><?php next_post_link('【下篇】%link') ?>
文章循环:
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
文章html骨架
<?php endwhile; ?>
<?php else : ?>
输出找不到文章提示
<?php endif; ?>
参考文档: The Loop
single.php
<?php the_content(); ?> 文章内容
<!-- Column 1 /Content -->
<?php if (have_posts()) : the_post(); update_post_caches($posts); ?>
<div class="grid_8">
<!-- Blog Post -->
<div class="post">
<!-- Post Title -->
<h3 class="title"><a href="<?php the_permalink()?>"><?php the_title()?></a></h3>
<!-- Post Title -->
<p class="sub">
<?php the_tags('标签:', ', ', ''); ?>•
<?php the_time('Y年n月j日') ?>•
<?php comments_popup_link('0 条评论', '1 条评论', '% 条评论', '', '评论已关闭'); ?>
<?php edit_post_link('编辑', ' • ', ''); ?>
</p>
<div class="hr dotted clearfix"> </div>
<!-- Post Title -->
<!--
<img class="thumb" src="<?php bloginfo('template_url')?>/images/610x150.gif" alt=""/>
-->
<!-- Post Content -->
<?php the_content(); ?>
<!-- Post Links -->
<p class="clearfix"> <a href="<?php echo get_option('home')?>" class="button float" ><< Back to Blog</a> <a href="#commentform" class="button float right" >Discuss this post</a> </p>
</div>
<div class="hr clearfix"> </div>
<!-- Comment's List -->
<h3>Comments</h3>
<div class="hr dotted clearfix"> </div>
<ol class="commentlist">
<li class="comment">
<div class="gravatar"> <img alt="" src='<?php bloginfo('template_url')?>/images/gravatar.png' height='48' width='48' /> <a class="comment-reply-link" href=''>Reply</a> </div>
<div class="comment_content">
<div class="clearfix"> <cite class="author_name"><a href="">Joe Bloggs</a></cite>
<div class="comment-meta commentmetadata">January 6, 2010 at 6:26 am</div>
</div>
<div class="comment_text">
<p>Donec leo. Aliquam risus elit, luctus vel, interdum vitae, malesuada eget, elit. Nulla vitae ipsum. Donec ligula ante, bibendum sit amet, elementum quis, viverra eu, ante. Fusce tincidunt. Mauris pellentesque, arcu eget feugiat accumsan, ipsum mi molestie orci, ut pulvinar sapien lorem nec dui.</p>
</div>
</div>
</li>
</ol>
<div class="hr clearfix"> </div>
<!-- Comment Form -->
<?php comments_template(); ?>
<!-- end Comment -->
</div>
<?php else : ?>
<div class="errorbox">
没有文章!
</div>
<?php endif; ?>
<?php get_sidebar(); ?>
comment.php
<?php comments_template(); ?>获取comments.php文件内容
wp_list_comments()列出评论
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 退出登录的链接