前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >WordPress主题的基本模板及常用函数

WordPress主题的基本模板及常用函数

作者头像
切图仔
发布2022-09-14 15:18:21
8580
发布2022-09-14 15:18:21
举报
文章被收录于专栏:生如夏花绚烂
常用模板文件
  • style.css : css文件一般包括主题声明和通用css样式代码
  • index.php:主页模板
  • header.php:header模板,网页头部
  • sidebar.php:侧边栏模板
  • footer.php:网页底部模板
  • archive.php:分类模板,显示分类下的文章目录
  • single.php:内容页(post)模板 例如,single-book.php 是作为 post type 为 book 的 posts 的模板。
  • page.php:内容也(page)模板 例如,page-about.php 是作为 slug 为 about 的 Page 的模板。
  • comments.php:留言回复模板
  • searchform.php:搜索表单模板
  • search.php:搜索界面模板
  • 404.php:错误页面模板
  • author.php:作者文章目录页面
  • functions.php:模板函数,存放主题用到的函数模块
  • attachment.php:附件模板页面
判断标签
  1. is_home():是否为首页
  2. is_front_page():是否为指定的首页,后台-设置-阅读指定的首页
  3. is_single():是否为内容页面(post)
  4. is_page():是否为内容页面(page)
  5. is_attachment():是否为附件页面
  6. is_singular():可以简单理解为 is_single()||is_page()||is_attachment() 的综合
  7. is_category():是否为category页面
  8. is_tag():是否为Tag存档页
  9. is_date():是否为指定日期存档页
  10. is_year():是否为指定年份存档页
  11. is_month():是否为指定月份存档页
  12. is_day():是否为指定日存档页
  13. is_time():是否为指定时间存档页
  14. is_archive():是否为存档页
  15. is_search():是否为搜索结构页
  16. is_author():是否为作者存档页
  17. is_404():是否为错误页
  18. is_paged() : 主页/Category/Archive页是否以多页显示
  19. is_user_logged_in() :用户是否登录
常用函数

get_header():调header模板

get_sidebar():调用sidebar模板

get_footer():调用footer模板

get_categories() :获取所有分类列表(只显示有内容的分类列表)

get_category_link($category->term_id) :获取category链接

bloginfo('html_type'):网页html类型

bloginfo('charset'):网页编码

bloginfo('name'):网站名称

bloginfo('url'):网站url

bloginfo('description'):网站描述

bloginfo('stylesheet_url'):css文件路径(style.css)

bloginfo('template_url'):模板文件目录

wp_head():头部挂钩,通过该函数让其他插件或功能函数在网站头部输出css,js等

wp_footer():同上类似

wp_nav_menu():调用导航菜单,一般需要在functions.php添加注册菜单函数 register_nav_menus()一起使用

wp_list_bookmarks() 友情链接函数

the_title():内容页标题

the_permalink():内容页url

the_category():特定内容页所属category

the_author():作者(只显示作者名字,没有链接)

the_author_posts_link():作者(显示作者,并且包含链接到作者文章目录的链接)

the_time('Y-m-d'):显示时间格式化

get_post_meta():获取保存在post_meta这个表的数据,比如输出某个自定义字段的内容

the_ID():特定内容页ID

the_tags('关键字','',''):显示文章的关键tag

the_excerpt():post/page页的摘要,输入文章发布页面中的摘要面板的内容

the_content('more'):显示内容(post/page)全文

wp_list_pages():显示page列表,常用于显示单篇文章的分页,配合 <!–next page-> 来使用

edit_post_link() : 如果用户已登录并具有权限,显示编辑链接

posts_nav_link(); : 显示上一页/下一页的链接,通常用在索引页、分类页和文章存档页

previous_post_link('%link', '上篇', TRUE) : 下一篇文章链接,通常用在单篇文章 single.php 中

php next_post_link('%link', '下篇', TRUE) : 上一篇文章链接,通常用在单篇文章 single.php 中

php comments_popup_link('暂无评论', '评论数 1', '评论数 %') : 正文中的留言链接。如果使用 comments_popup_script(),则留言会在新窗口中打开,反之,则在当前窗口打开

php comments_template( '', true ); :显示评论模块

php include(TEMPLATEPATH . '/xxx/xxxx.php'); : 嵌入其他文件,可为定制的模板或其他类型php文件,很常用

php echo get_avatar( get_the_author_email(), '48' ) :根据作者邮箱输出作者的头像

php wp_list_categories() : 显示Categories列表

php get_calendar() : 日历

php wp_get_archives() : 显示内容存档

php _e('Message') : 输出相应信息

php wp_register() : 显示注册链接

php wp_loginout() : 显示登录/注销链接

php timer_stop(1) : 网页加载时间(秒)

php echo get_num_queries() : 网页加载查询量

single_cat_title():当前分类页标题

single_post_title():当前文章标题

the_post_thumbnail():显示文章缩略图(首先要在functions.php文件加入 缩略图代码add_theme_support('post_thumbnails');)

只显示文章缩略图链接

代码语言:javascript
复制
$large_image_url = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'large');
echo $large_image_url[0];

文章没有缩略图时显示占位图片

代码语言:javascript
复制
$image_url = wp_get_attachment_image_src(get_post_thumbnail_id($post->id));
if(!$image_url){
    //封面图占位
    echo "<img src='http://www.zihanzy.com/uploads/images/article_img/202007/25/article_img_1595654106_DhHuMFcqhU.jpg'>";
}else{
    the_post_thumbnail();
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-09-17 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 常用模板文件
  • 判断标签
  • 常用函数
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档