网站模板制作-6-wordpress主题中常用的函数有哪些

统计了历年的wordpress 的官方主题,一共统计了 13 个,这 13个 主题中使用的函数,总结了一下。这些函数对制作模板来说,很重要。

🎨 主题相关函数

https://www.wpzhan.com/static/html/wordpress-theme-functions.html

函数参数返回值功能
get_header( $name )$name (string) – 模板部分名称void加载头部模板
get_footer( $name )$name (string) – 模板部分名称void加载底部模板
get_sidebar( $name )$name (string) – 模板部分名称void加载侧边栏模板
get_template_part( $slug, $name )$slug (string) – 模板段, $name (string) – 特定名称void加载指定的模板部分
get_template_directory()string获取当前主题目录路径
get_template_directory_uri()string获取当前主题目录URL
get_stylesheet_directory()string获取子主题目录路径
get_stylesheet_directory_uri()string获取子主题目录URL
get_stylesheet_uri()string获取主样式表URL
get_theme_mod( $name, $default )$name (string) – 主题修改项, $default (mixed) – 默认值mixed获取主题修改值
set_theme_mod( $name, $value )$name (string) – 主题修改项, $value (mixed) – 设置值bool设置主题修改值
add_theme_support( $feature )$feature (string) – 功能名称bool添加主题功能支持
current_theme_supports( $feature )$feature (string) – 功能名称bool检查当前主题是否支持某功能
get_theme_support( $feature )$feature (string) – 功能名称mixed|false获取主题支持的功能配置
register_nav_menu( $location, $description )$location (string) – 菜单位置, $description (string) – 描述void注册一个导航菜单
register_nav_menus( $locations )$locations (array) – 菜单位置数组void注册多个导航菜单
has_nav_menu( $location )$location (string) – 菜单位置bool检查是否有指定位置的菜单
wp_nav_menu( $args )$args (array) – 菜单参数string|void显示导航菜单
register_sidebar( $args )$args (array) – 侧边栏参数string注册侧边栏
dynamic_sidebar( $index )$index (string|int) – 侧边栏IDbool显示动态侧边栏
is_active_sidebar( $index )$index (string|int) – 侧边栏IDbool检查侧边栏是否激活
add_image_size( $name, $width, $height, $crop )$name (string) – 尺寸名称, $width (int) – 宽度, $height (int) – 高度, $crop (bool|array) – 裁剪方式void添加新的图片尺寸
set_post_thumbnail_size( $width, $height, $crop )$width (int) – 宽度, $height (int) – 高度, $crop (bool|array) – 裁剪方式void设置特色图片尺寸
body_class( $class )$class (string|array) – 额外的类名void输出body类名
post_class( $class, $post_id )$class (string|array) – 额外的类名, $post_id (int) – 文章IDvoid输出文章类名
wp_body_open()void触发body打开钩子
wp_footer()void触发底部钩子
wp_head()void触发头部钩子

📝 文章/内容相关函数

函数参数返回值功能
the_title( $before, $after, $echo )$before (string) – 前缀, $after (string) – 后缀, $echo (bool) – 是否直接输出string|void显示文章标题
get_the_title( $post )$post (int|WP_Post) – 文章ID或对象string获取文章标题
the_content( $more_link_text, $strip_teaser )$more_link_text (string) – 更多链接文本, $strip_teaser (bool) – 是否剥离摘要void显示文章内容
get_the_content( $more_link_text, $strip_teaser, $post )$more_link_text (string) – 更多链接文本, $strip_teaser (bool) – 是否剥离摘要, $post (int|WP_Post) – 文章string获取文章内容
the_excerpt()void显示文章摘要
get_the_excerpt( $post )$post (int|WP_Post) – 文章string获取文章摘要
has_excerpt( $post )$post (int|WP_Post) – 文章bool检查是否有自定义摘要
the_permalink()void显示文章永久链接
get_permalink( $post, $leavename )$post (int|WP_Post) – 文章, $leavename (bool) – 是否保留名称string|false获取文章永久链接
the_post_thumbnail( $size, $attr )$size (string|array) – 图片尺寸, $attr (array) – 图片属性void显示文章特色图片
get_the_post_thumbnail( $post, $size, $attr )$post (int|WP_Post) – 文章, $size – 尺寸, $attr – 属性string获取文章特色图片
has_post_thumbnail( $post )$post (int|WP_Post) – 文章bool检查是否有特色图片
get_post_thumbnail_id( $post )$post (int|WP_Post) – 文章int获取特色图片ID
the_date( $format, $before, $after, $echo )$format (string) – 日期格式, $before (string) – 前缀, $after (string) – 后缀, $echo (bool) – 是否输出string|void显示文章发布日期
get_the_date( $format, $post )$format (string) – 日期格式, $post (int|WP_Post) – 文章string获取文章发布日期
the_time( $format )$format (string) – 时间格式void显示文章发布时间
get_the_time( $format, $post )$format (string) – 时间格式, $post – 文章string获取文章发布时间
the_modified_date( $format, $before, $after, $echo )同上string|void显示文章修改日期
get_the_modified_date( $format, $post )$format – 日期格式, $post – 文章string获取文章修改日期
the_author()void显示文章作者
get_the_author( $post )$post (int|WP_Post) – 文章string获取文章作者名
the_author_meta( $field, $user_id )$field (string) – 字段名, $user_id (int) – 用户IDvoid显示作者元数据
get_the_author_meta( $field, $user_id )$field – 字段名, $user_id – 用户IDstring获取作者元数据
the_category( $separator, $parents, $post_id )$separator – 分隔符, $parents – 父级显示方式, $post_id – 文章IDvoid显示文章分类
get_the_category_list( $separator, $parents, $post_id )同上string获取文章分类列表
in_category( $category, $post )$category (mixed) – 分类, $post – 文章bool检查文章是否在指定分类中
the_tags( $before, $sep, $after )$before – 前缀, $sep – 分隔符, $after – 后缀void显示文章标签
get_the_tag_list( $before, $sep, $after, $post_id )同上string|false获取文章标签列表
has_tag( $tag, $post )$tag (mixed) – 标签, $post – 文章bool检查文章是否有指定标签
the_terms( $post_id, $taxonomy, $before, $sep, $after )$post_id – 文章ID, $taxonomy – 分类法, $before – 前缀, $sep – 分隔符, $after – 后缀void显示文章术语
has_term( $term, $taxonomy, $post )$term – 术语, $taxonomy – 分类法, $post – 文章bool检查是否有指定术语
wp_link_pages( $args )$args (array) – 参数数组void显示文章分页链接

🔄 循环相关函数

函数参数返回值功能
have_posts()bool检查是否有文章
the_post()void设置当前文章
rewind_posts()void重置循环到开始
wp_reset_query()void重置主查询
wp_reset_postdata()void重置文章数据
in_the_loop()bool检查是否在主循环内
get_queried_object()WP_Term|WP_Post|WP_User|null获取当前查询对象
get_queried_object_id()int获取当前查询对象ID
get_query_var( $var, $default )$var – 变量名, $default – 默认值mixed获取查询变量
set_query_var( $var, $value )$var – 变量名, $value – 变量值void设置查询变量

💬 评论相关函数

函数参数返回值功能
comments_template( $file, $separate_comments )$file – 模板文件, $separate_comments – 是否分离评论void加载评论模板
comments_number( $zero, $one, $more )$zero – 0条评论文本, $one – 1条评论文本, $more – 多条评论文本void显示评论数量
get_comments_number( $post_id )$post_id (int) – 文章IDint获取评论数量
comment_form( $args, $post_id )$args – 参数数组, $post_id – 文章IDvoid显示评论表单
wp_list_comments( $args, $comments )$args – 参数数组, $comments – 评论数组void显示评论列表
comments_open( $post_id )$post_id (int) – 文章IDbool检查评论是否开放
pings_open( $post_id )$post_id (int) – 文章IDbool检查Pingback是否开放
get_comment_pages_count( $comments, $per_page, $threaded )$comments – 评论数组, $per_page – 每页数量, $threaded – 是否嵌套int获取评论分页数
paginate_comments_links( $args )$args – 参数数组string|void显示评论分页链接
previous_comments_link( $label )$label – 链接文本void显示上一页评论链接
next_comments_link( $label )$label – 链接文本void显示下一页评论链接
get_comment_author()string获取当前评论作者
get_comment_date( $format, $comment )$format – 日期格式, $comment – 评论对象string获取评论日期
get_comment_time( $format, $comment )$format – 时间格式, $comment – 评论对象string获取评论时间
comment_reply_link( $args )$args – 参数数组string|void显示评论回复链接
edit_comment_link( $text, $before, $after )$text – 链接文本, $before – 前缀, $after – 后缀void显示编辑评论链接

👤 用户相关函数

函数参数返回值功能
is_user_logged_in()bool检查用户是否登录
wp_get_current_user()WP_User获取当前用户对象
get_current_user_id()int获取当前用户ID
current_user_can( $capability, $args )$capability – 权限, $args – 附加参数bool检查用户权限
wp_login_form( $args )$args (array) – 登录表单参数void显示登录表单
wp_loginout( $redirect )$redirect – 重定向URLvoid显示登录/退出链接
wp_register( $before, $after )$before – 前缀, $after – 后缀void显示注册链接
wp_logout_url( $redirect )$redirect – 重定向URLstring获取退出登录URL
wp_lostpassword_url( $redirect )$redirect – 重定向URLstring获取找回密码URL
get_userdata( $user_id )$user_id (int) – 用户IDWP_User|false获取用户数据
get_user_meta( $user_id, $key, $single )$user_id – 用户ID, $key – 元数据键, $single – 是否返回单个值mixed获取用户元数据
get_avatar( $id_or_email, $size, $default, $alt, $args )$id_or_email – 用户ID或邮箱, $size – 头像尺寸string|false获取头像HTML
wp_signon( $credentials, $secure_cookie )$credentials – 登录凭证, $secure_cookie – 安全CookieWP_User|WP_Error用户登录

🎯 条件标签函数

函数参数返回值功能
is_home()bool判断是否为博客首页
is_front_page()bool判断是否为网站首页
is_single( $post )$post (mixed) – 文章ID、slug或对象bool判断是否为单篇文章页
is_page( $page )$page (mixed) – 页面ID、slug或标题bool判断是否为页面
is_singular( $post_types )$post_types – 文章类型bool判断是否为单一文章类型页
is_archive()bool判断是否为归档页
is_category( $category )$category – 分类ID、slug或名称bool判断是否为分类页
is_tag( $tag )$tag – 标签ID、slug或名称bool判断是否为标签页
is_tax( $taxonomy, $term )$taxonomy – 分类法, $term – 术语bool判断是否为自定义分类页
is_author( $author )$author – 作者ID、邮箱或昵称bool判断是否为作者页
is_date()bool判断是否为日期归档页
is_year()bool判断是否为年度归档页
is_month()bool判断是否为月度归档页
is_day()bool判断是否为日度归档页
is_time()bool判断是否为时间归档页
is_new_day()bool判断是否为新的一天
is_search()bool判断是否为搜索结果页
is_404()bool判断是否为404页
is_paged()bool判断是否为分页页
is_preview()bool判断是否为预览模式
is_sticky( $post_id )$post_id (int) – 文章IDbool判断文章是否置顶
is_attachment( $attachment )$attachment – 附件ID或对象bool判断是否为附件页
is_feed()bool判断是否为Feed
is_admin()bool判断是否为后台页面
is_multi_author()bool判断是否为多作者站点
is_multisite()bool判断是否为多站点网络
is_main_site()bool判断是否为主站点
is_super_admin( $user_id )$user_id (int) – 用户IDbool判断是否为超级管理员
is_child_theme()bool判断是否使用子主题
is_page_template( $template )$template – 模板文件名bool判断是否使用特定页面模板
is_post_type_archive( $post_types )$post_types – 文章类型bool判断是否为文章类型归档页
is_post_type_hierarchical( $post_type )$post_type – 文章类型bool判断文章类型是否为层级结构
taxonomy_exists( $taxonomy )$taxonomy – 分类法名称bool检查分类法是否存在
is_rtl()bool判断是否为从右到左语言

⚡ 钩子/动作/过滤器函数

函数参数返回值功能
add_action( $hook_name, $callback, $priority, $accepted_args )$hook_name – 钩子名, $callback – 回调函数, $priority – 优先级, $accepted_args – 接受参数数量true添加动作钩子
remove_action( $hook_name, $callback, $priority )$hook_name – 钩子名, $callback – 回调函数, $priority – 优先级bool移除动作钩子
do_action( $hook_name, ...$arg )$hook_name – 钩子名, ...$arg – 传递给钩子的参数void执行动作钩子
add_filter( $hook_name, $callback, $priority, $accepted_args )$hook_name – 过滤器名, $callback – 回调函数, $priority – 优先级, $accepted_args – 接受参数数量true添加过滤器钩子
remove_filter( $hook_name, $callback, $priority )$hook_name – 过滤器名, $callback – 回调函数, $priority – 优先级bool移除过滤器钩子
apply_filters( $hook_name, $value, ...$args )$hook_name – 过滤器名, $value – 要过滤的值, ...$args – 附加参数mixed应用过滤器钩子
add_shortcode( $tag, $callback )$tag – 短代码名称, $callback – 回调函数void添加短代码
remove_shortcode( $tag )$tag – 短代码名称void移除短代码
do_shortcode( $content, $ignore_html )$content – 要解析的内容, $ignore_html – 是否忽略HTMLstring解析短代码
shortcode_parse_atts( $text )$text – 短代码属性字符串array解析短代码属性

🔧 选项/设置函数

函数参数返回值功能
get_option( $option, $default )$option – 选项名, $default – 默认值mixed获取选项值
update_option( $option, $value, $autoload )$option – 选项名, $value – 选项值, $autoload – 是否自动加载bool更新选项
add_option( $option, $value, $deprecated, $autoload )$option – 选项名, $value – 选项值, $deprecated – 已废弃, $autoload – 是否自动加载bool添加选项
delete_option( $option )$option – 选项名bool删除选项
get_transient( $transient )$transient – 临时数据名mixed|false获取临时数据
set_transient( $transient, $value, $expiration )$transient – 临时数据名, $value – 值, $expiration – 过期时间bool设置临时数据
delete_transient( $transient )$transient – 临时数据名bool删除临时数据
register_setting( $option_group, $option_name, $args )$option_group – 选项组, $option_name – 选项名, $args – 参数void注册设置

🌐 URL/路径相关函数

函数参数返回值功能
home_url( $path, $scheme )$path – 路径, $scheme – URL方案string获取网站URL
site_url( $path, $scheme )$path – 路径, $scheme – URL方案string获取站点URL
admin_url( $path, $scheme )$path – 路径, $scheme – URL方案string获取后台URL
content_url( $path )$path – 路径string获取内容目录URL
includes_url( $path )$path – 路径string获取包含目录URL
plugins_url( $path, $plugin )$path – 路径, $plugin – 插件文件string获取插件目录URL
wp_upload_dir()array获取上传目录信息
wp_get_attachment_url( $attachment_id )$attachment_id (int) – 附件IDstring|false获取附件URL
wp_get_attachment_image_url( $attachment_id, $size )$attachment_id – 附件ID, $size – 图片尺寸string|false获取附件图片URL
wp_get_attachment_image( $attachment_id, $size, $icon, $attr )$attachment_id – 附件ID, $size – 尺寸, $icon – 是否使用图标, $attr – 属性string获取附件图片HTML
wp_get_attachment_metadata( $attachment_id )$attachment_id (int) – 附件IDarray|false获取附件元数据
wp_attachment_is_image( $attachment_id )$attachment_id (int) – 附件IDbool检查附件是否为图片

🔒 安全/清理函数

函数参数返回值功能
esc_html( $text )$text (string) – 要转义的文本string转义HTML内容
esc_attr( $text )$text (string) – 要转义的文本string转义HTML属性
esc_url( $url, $protocols, $_context )$url – URL, $protocols – 允许的协议, $_context – 上下文string转义URL
esc_js( $text )$text (string) – 要转义的文本string转义JavaScript
esc_textarea( $text )$text (string) – 要转义的文本string转义文本域内容
esc_url_raw( $url, $protocols )$url – URL, $protocols – 允许的协议string转义原始URL(用于数据库存储)
wp_kses( $string, $allowed_html, $allowed_protocols )$string – 内容, $allowed_html – 允许的HTML, $allowed_protocols – 允许的协议string过滤HTML内容
wp_kses_post( $data )$data (string) – 内容string过滤文章内容的HTML
wp_strip_all_tags( $string, $remove_breaks )$string – 字符串, $remove_breaks – 是否移除换行string移除所有HTML标签
sanitize_text_field( $str )$str (string) – 要清理的字符串string清理文本字段
sanitize_email( $email )$email (string) – 邮箱地址string清理邮箱地址
sanitize_file_name( $filename )$filename (string) – 文件名string清理文件名
sanitize_key( $key )$key (string) – 键名string清理键名
sanitize_title( $title, $fallback_title, $context )$title – 标题, $fallback_title – 备用标题, $context – 上下文string清理为URL友好的标题
sanitize_hex_color( $color )$color (string) – 颜色值string|null清理十六进制颜色
sanitize_hex_color_no_hash( $color )$color (string) – 无#的颜色值string|null清理无哈希的十六进制颜色

🌍 国际化/翻译函数

函数参数返回值功能
__( $text, $domain )$text – 要翻译的文本, $domain – 文本域string获取翻译文本
_e( $text, $domain )$text – 要翻译的文本, $domain – 文本域void输出翻译文本
_x( $text, $context, $domain )$text – 文本, $context – 上下文, $domain – 文本域string获取带上下文的翻译
_ex( $text, $context, $domain )$text – 文本, $context – 上下文, $domain – 文本域void输出带上下文的翻译
_n( $single, $plural, $number, $domain )$single – 单数, $plural – 复数, $number – 数量, $domain – 文本域string获取单复数翻译
_nx( $single, $plural, $number, $context, $domain )$single – 单数, $plural – 复数, $number – 数量, $context – 上下文, $domain – 文本域string获取带上下文的单复数翻译
esc_html__( $text, $domain )$text – 文本, $domain – 文本域string获取并转义HTML翻译
esc_html_e( $text, $domain )$text – 文本, $domain – 文本域void输出并转义HTML翻译
esc_attr__( $text, $domain )$text – 文本, $domain – 文本域string获取并转义属性翻译
esc_attr_e( $text, $domain )$text – 文本, $domain – 文本域void输出并转义属性翻译
esc_html_x( $text, $context, $domain )$text – 文本, $context – 上下文, $domain – 文本域string获取带上下文的转义HTML翻译
esc_attr_x( $text, $context, $domain )$text – 文本, $context – 上下文, $domain – 文本域string获取带上下文的转义属性翻译
load_theme_textdomain( $domain, $path )$domain – 文本域, $path – 路径bool加载主题翻译文件
load_child_theme_textdomain( $domain, $path )$domain – 文本域, $path – 路径bool加载子主题翻译文件
get_locale()string获取当前语言环境

📱 媒体/附件函数

函数参数返回值功能
wp_get_archives( $args )$args (array) – 归档参数void显示文章归档
wp_get_archives$args – 参数数组string|void获取文章归档
wp_get_attachment_image( $attachment_id, $size, $icon, $attr )$attachment_id – 附件ID, $size – 尺寸, $icon – 是否使用图标, $attr – 属性string获取附件图片
wp_get_attachment_image_src( $attachment_id, $size, $icon )$attachment_id – 附件ID, $size – 尺寸, $icon – 是否使用图标array|false获取附件图片信息
wp_get_attachment_metadata( $attachment_id )$attachment_id (int) – 附件IDarray|false获取附件元数据
wp_get_attachment_url( $attachment_id )$attachment_id (int) – 附件IDstring|false获取附件URL
wp_attachment_is_image( $attachment_id )$attachment_id (int) – 附件IDbool检查是否为图片附件
the_header_image_tag()void显示头部图片标签
get_header_image()string|false获取头部图片URL
has_header_image()bool检查是否有头部图片
get_custom_header()object|false获取自定义头部对象
get_background_image()string获取背景图片URL
get_background_color()string获取背景颜色

📦 文章类型/分类法函数

函数参数返回值功能
get_post_type( $post )$post (int|WP_Post) – 文章string|false获取文章类型
post_type_supports( $post_type, $feature )$post_type – 文章类型, $feature – 功能bool检查文章类型是否支持某功能
get_post_types( $args, $output, $operator )$args – 参数, $output – 输出类型, $operator – 运算符array获取文章类型列表
register_post_type( $post_type, $args )$post_type – 文章类型名, $args – 参数WP_Post_Type|WP_Error注册文章类型
get_taxonomies( $args, $output, $operator )$args – 参数, $output – 输出类型, $operator – 运算符array获取分类法列表
get_taxonomy( $taxonomy )$taxonomy (string) – 分类法名称WP_Taxonomy|false获取分类法对象
register_taxonomy( $taxonomy, $object_type, $args )$taxonomy – 分类法名, $object_type – 对象类型, $args – 参数WP_Taxonomy|WP_Error注册分类法
is_object_in_taxonomy( $object_type, $taxonomy )$object_type – 对象类型, $taxonomy – 分类法bool检查对象类型是否在分类法中
wp_get_post_parent_id( $post_id )$post_id (int) – 文章IDint获取文章父级ID

📦 其他实用函数

函数参数返回值功能
wp_parse_args( $args, $defaults )$args – 参数, $defaults – 默认值array解析合并参数
wp_remote_get( $url, $args )$url – 请求URL, $args – 请求参数array|WP_Error发送GET请求
wp_remote_post( $url, $args )$url – 请求URL, $args – 请求参数array|WP_Error发送POST请求
wp_mail( $to, $subject, $message, $headers, $attachments )$to – 收件人, $subject – 主题, $message – 内容, $headers – 头部, $attachments – 附件bool发送邮件
wp_die( $message, $title, $args )$message – 消息, $title – 标题, $args – 参数void终止并显示错误
wp_redirect( $location, $status, $x_redirect_by )$location – 重定向地址, $status – HTTP状态码, $x_redirect_by – 代理bool重定向
wp_safe_redirect( $location, $status, $x_redirect_by )同上bool安全重定向
get_bloginfo( $show, $filter )$show – 显示项, $filter – 是否过滤string获取博客信息
bloginfo( $show )$show (string) – 显示项void显示博客信息
wp_get_theme( $stylesheet, $theme_root )$stylesheet – 主题样式, $theme_root – 主题根目录WP_Theme获取主题对象
wp_is_json_request()bool检查是否为JSON请求
wp_rand( $min, $max )$min – 最小值, $max – 最大值int生成随机数
wp_unique_id( $prefix )$prefix (string) – 前缀string生成唯一ID
wpautop( $pee, $br )$pee – 文本, $br – 是否保留换行string自动添加段落标签
absint( $maybeint )$maybeint (mixed) – 可能为整数的值int转换为非负整数
number_format_i18n( $number, $decimals )$number – 数字, $decimals – 小数位数string国际化数字格式化
date_i18n( $format, $timestamp, $gmt )$format – 格式, $timestamp – 时间戳, $gmt – 是否GMTstring国际化日期格式化
mysql2date( $format, $date, $translate )$format – 格式, $date – MySQL日期, $translate – 是否翻译string转换MySQL日期
wp_date( $format, $timestamp, $timezone )$format – 格式, $timestamp – 时间戳, $timezone – 时区string|false本地化日期格式化
trailingslashit( $string )$string (string) – 字符串string添加尾部斜杠
untrailingslashit( $string )$string (string) – 字符串string移除尾部斜杠

📊 总结

分类函数数量主要用途
🎨 主题相关22主题结构、菜单、侧边栏
📝 文章/内容28文章显示、内容处理
🔄 循环相关8文章循环控制
💬 评论相关14评论系统功能
👤 用户相关11用户认证、资料
🎯 条件标签31页面条件判断
⚡ 钩子/动作9钩子系统
🔧 选项/设置8数据存储
🌐 URL/路径10链接处理
🔒 安全/清理13数据安全过滤
🌍 国际化13多语言支持
📱 媒体/附件9图片、媒体处理
📦 文章类型8CPT、分类法
📦 其他实用15通用功能

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注