[关闭]
@52fhy 2015-12-05T13:43:04.000000Z 字数 6815 阅读 485

WordPress开发

WordPress


常用方法:

  1. get_header()就相当于将header.php中的代码拷贝到当前的php文件。
  2. get_footer()
  3. get_sidebar()
  4. get_template_part( 'includes/nav' ) 获取主题下includes/nav.php
  5. get_template_part( 'content','2' )获取主题下content-2.php
  6. is_home():当前页面为主页时返回true
  7. is_category():当前页面为分类页时返回true
  8. is_single():当前页面为单文章页时返回true
  9. is_page():当前页面为单页面时返回true

更详细的内容参阅WordPress文档:条件标签

  1. bloginfo('name') 输出的是博客名
  2. bloginfo('stylesheet_url') 输出的是当前主题css文件style.css
  3. bloginfo('template_url') 获取当前主题根目录的URL
  4. <?php bloginfo('template_url'); ?>/abc.css
  5. bloginfo('pingback_url')
  6. get_option('home');博客首页网址,get_option()返回键值对应的配置值,取至wp_options
  7. ot_get_option('favicon')
  8. wp_head(): 有些插件需要在网页头部执行一些类如添加一些jscss的动作,要让这些插件能够正常的工作,也让你的主题有更好的兼容性,你应该添加wp_head()函数。打开header.php,在</head>前面添加。
  9. wp_footer()
  10. wp_list_pages() 用于列出博客页面,即导航菜单

header.php
页面内容:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>
  6. <?php if ( is_home() ) {
  7. bloginfo('name'); echo " - "; bloginfo('description');
  8. } elseif ( is_category() ) {
  9. single_cat_title(); echo " - "; bloginfo('name');
  10. } elseif (is_single() || is_page() ) {
  11. single_post_title();
  12. } elseif (is_search() ) {
  13. echo "搜索结果"; echo " - "; bloginfo('name');
  14. } elseif (is_404() ) {
  15. echo '页面未找到!';
  16. } else {
  17. wp_title('',true);
  18. } ?>
  19. </title>
  20. <!-- Stylesheets -->
  21. <link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" />
  22. <link rel="pingback" href="<?php bloginfo('pingback_url'); ?>" />
  23. <link rel="alternate" type="application/rss+xml" title="RSS 2.0 - 所有文章" href="<?php bloginfo('rss2_url'); ?>" />
  24. <link rel="alternate" type="application/rss+xml" title="RSS 2.0 - 所有评论" href="<?php bloginfo('comments_rss2_url'); ?>" />
  25. <!--
  26. <?php wp_head(); ?>
  27. -->
  28. <?php flush(); ?>
  29. </head>
  30. <body>
  31. <div id="wrapper" class="container_12 clearfix">
  32. <!-- Text Logo 博客名称-->
  33. <h1 id="logo" class="grid_4"><a href="<?php echo get_option('home')?>"><?php bloginfo('name')?></a><h1>
  34. <!-- Navigation Menu 导航菜单-->
  35. <ul id="navigation" class="grid_8">
  36. <!-- MENU -->
  37. <?php wp_list_pages('depth=1&title_li=0&sort_column=menu_order'); ?>
  38. <!-- HOME -->
  39. <li <?php if (is_home()) { echo 'class="current"';} ?>><a title="<?php bloginfo('name'); ?>" href="<?php echo get_option('home'); ?>/">主页</a></li>
  40. </ul>
  41. <div class="hr grid_12 clearfix">&nbsp;</div>
  42. <!-- Caption Line 描述-->
  43. <h2 class="grid_12 caption clearfix"><?php bloginfo('description')?></h2>
  44. <div class="hr grid_12 clearfix">&nbsp;</div>

footer.php
页面内容:

  1. <!-- Footer -->
  2. <p class="grid_12 footer clearfix">
  3. <span class="float">版权所有 &copy; 2015 <?php bloginfo('name'); ?>
  4. &nbsp;&nbsp;|&nbsp;&nbsp;Powered By <a rel="external" title="WordPress主页" class="link" href="http://wordpress.org/">WordPress</a>
  5. &nbsp;&nbsp;|&nbsp;&nbsp;Design By QwibbleDesigns&nbsp;&nbsp;|&nbsp;&nbsp;
  6. Code By <a href="http://www.ludou.org/">Ludou</a>
  7. </span>
  8. <a class="float right" href="#">top</a> </p>
  9. </div>
  10. <!--end wrapper-->
  11. <!--
  12. <?php wp_footer(); ?>
  13. -->
  14. </body>
  15. </html>

sidebar.php
页面内容:

  1. <!-- Column 2 / Sidebar -->
  2. <div class="grid_4">
  3. <?php if ( !function_exists('dynamic_sidebar')
  4. || !dynamic_sidebar('First_sidebar') ) : ?>
  5. <h4>分类目录</h4>
  6. <ul>
  7. <?php wp_list_categories('depth=1&title_li=&orderby=id&show_count=0&hide_empty=1&child_of=0'); ?>
  8. </ul>
  9. <?php endif; ?>
  10. <?php if ( !function_exists('dynamic_sidebar')
  11. || !dynamic_sidebar('Second_sidebar') ) : ?>
  12. <h4>最新文章</h4>
  13. <ul>
  14. <?php
  15. $posts = get_posts('numberposts=6&orderby=post_date');
  16. foreach($posts as $post) {
  17. setup_postdata($post);
  18. echo '<li><a href="' . get_permalink() . '">' . get_the_title() . '</a></li>';
  19. }
  20. $post = $posts[0];
  21. ?>
  22. </ul>
  23. <?php endif; ?>
  24. <?php if ( !function_exists('dynamic_sidebar')
  25. || !dynamic_sidebar('Third_sidebar') ) : ?>
  26. <h4>标签云</h4>
  27. <p><?php wp_tag_cloud('smallest=8&largest=22'); ?></p>
  28. <?php endif; ?>
  29. <?php if ( !function_exists('dynamic_sidebar')
  30. || !dynamic_sidebar('Fourth_sidebar') ) : ?>
  31. <h4>文章存档</h4>
  32. <ul>
  33. <?php wp_get_archives('limit=10'); ?>
  34. </ul>
  35. <?php endif; ?>
  36. </div>
  37. <div class="hr grid_12 clearfix">&nbsp;</div>

注释:

  1. wp_list_categories('depth=1&title_li=&orderby=id&show_count=0&hide_empty=1&child_of=0')分类目录
  2. get_posts('numberposts=6&orderby=post_date')最新文章
  3. wp_tag_cloud('smallest=8&largest=22')标签云
  4. wp_get_archives('limit=10')文章存档

index.php
注释:

  1. <?php the_permalink(); ?> 输出文章的URL链接(参考文档)
  2. <?php the_title(); ?> 输出文章的标题(参考文档)
  3. <?php the_tags('标签:', ', ', ''); ?>
  4. <?php the_time('Y年n月j日') ?>
  5. <?php comments_popup_link('0 条评论', '1 条评论', '% 条评论', '', '评论已关闭'); ?>
  6. <?php edit_post_link('编辑', ' &bull; ', ''); ?>
  7. <?php the_excerpt(); ?>首页显示的就是摘要,如果不填就输出全文
  8. <?php the_content('阅读全文...'); ?>用于输出全文,除非你在文章中使用了<!-- more -->
  9. <?php next_posts_link(); ?>下一页
  10. <?php previous_posts_link(); ?>上一页
  11. <?php previous_post_link('【上篇】%link') ?><br/><?php next_post_link('【下篇】%link') ?>

文章循环:

  1. <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
  2. 文章html骨架
  3. <?php endwhile; ?>
  4. <?php else : ?>
  5. 输出找不到文章提示
  6. <?php endif; ?>

参考文档: The Loop

single.php

  1. <?php the_content(); ?> 文章内容
  1. <!-- Column 1 /Content -->
  2. <?php if (have_posts()) : the_post(); update_post_caches($posts); ?>
  3. <div class="grid_8">
  4. <!-- Blog Post -->
  5. <div class="post">
  6. <!-- Post Title -->
  7. <h3 class="title"><a href="<?php the_permalink()?>"><?php the_title()?></a></h3>
  8. <!-- Post Title -->
  9. <p class="sub">
  10. <?php the_tags('标签:', ', ', ''); ?>&bull;
  11. <?php the_time('Y年n月j日') ?>&bull;
  12. <?php comments_popup_link('0 条评论', '1 条评论', '% 条评论', '', '评论已关闭'); ?>
  13. <?php edit_post_link('编辑', ' &bull; ', ''); ?>
  14. </p>
  15. <div class="hr dotted clearfix">&nbsp;</div>
  16. <!-- Post Title -->
  17. <!--
  18. <img class="thumb" src="<?php bloginfo('template_url')?>/images/610x150.gif" alt=""/>
  19. -->
  20. <!-- Post Content -->
  21. <?php the_content(); ?>
  22. <!-- Post Links -->
  23. <p class="clearfix"> <a href="<?php echo get_option('home')?>" class="button float" >&lt;&lt; Back to Blog</a> <a href="#commentform" class="button float right" >Discuss this post</a> </p>
  24. </div>
  25. <div class="hr clearfix">&nbsp;</div>
  26. <!-- Comment's List -->
  27. <h3>Comments</h3>
  28. <div class="hr dotted clearfix">&nbsp;</div>
  29. <ol class="commentlist">
  30. <li class="comment">
  31. <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>
  32. <div class="comment_content">
  33. <div class="clearfix"> <cite class="author_name"><a href="">Joe Bloggs</a></cite>
  34. <div class="comment-meta commentmetadata">January 6, 2010 at 6:26 am</div>
  35. </div>
  36. <div class="comment_text">
  37. <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>
  38. </div>
  39. </div>
  40. </li>
  41. </ol>
  42. <div class="hr clearfix">&nbsp;</div>
  43. <!-- Comment Form -->
  44. <?php comments_template(); ?>
  45. <!-- end Comment -->
  46. </div>
  47. <?php else : ?>
  48. <div class="errorbox">
  49. 没有文章!
  50. </div>
  51. <?php endif; ?>
  52. <?php get_sidebar(); ?>

comment.php

  1. <?php comments_template(); ?>获取comments.php文件内容
  2. wp_list_comments()列出评论
  3. get_avatar($comment, 48) 获取评论者的gravatar头像,尺寸为48 * 48
  4. comment_reply_link() 回复留言的链接
  5. get_comment_author_link 用于获取评论者博客地址
  6. get_comment_time 获取评论发布时间
  7. edit_comment_link 管理员修改评论的链接
  8. comment_text() 输出评论内容
  9. is_user_logged_in 判断用户是否登录
  10. wp_login_url 博客登录地址
  11. get_comment_author_link 用于获取评论者博客地址
  12. $comment_author 读取cookie,如果该用户之前已经发表过评论则自动帮助用户填写用户名
  13. $comment_author_email 读取cookie,如果该用户之前已经发表过评论则自动帮助用户填写Email
  14. $comment_author_url 读取cookie,如果该用户之前已经发表过评论则自动帮助用户填写博客地址
  15. do_action(‘comment_form’, $post->ID); 该函数为某些插件预留
  16. wp_logout_url 退出登录的链接
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注