一、站点路径相关函数
函数名 | 说明 | 示例 |
---|---|---|
home_url() | 返回站点路径,相当于后台设置->常规中的”站点地址(URL)”。 | $url = home_url(); echo $url; $url = home_url('/images/'); |
site_url() | WordPress实际安装地址 | $url = site_url(); echo $url; //输出:http://www.objui.com |
admin_url() | 返回后台地址,传递参数后也可返回后台menu的地址 | $url = admin_url(); echo $url; //输出:http://www.objui.com/wp-admin/ |
content_url() | 返回实际的wp-content目录,如果是默认安装,且装在根目录下 | $url = content_url(); echo $url; //输出:http://www.objui.com/wp-content |
includes_url() | 返回当前WordPress站点存放核心文件的目录wp-includes的地址,可以带一个$path作为参数。 | $url = includes_url( '/js/'); echo $url; //输出:http://www.objui.com/wp-includes/js/ |
wp_upload_dir() | 返回WordPress上传目录的地址,是一个数组,包含一系列与上传地址相关的信息。
| $upload_dir = wp_upload_dir(); echo $upload_dir['baseurl']; //输出:http://www.objui.com/wp-content/uploads |
二、主题路径相关函数
名称 | 说明 | 示例 |
---|---|---|
get_theme_root_uri() | 获取存放主题的目录URI | echo get_theme_root_uri(); //输出:http://www.objui.com/wp-content/themes |
get_theme_root() | 获取存放主题的目录的服务器绝对路径 | echo get_theme_root(); //输出:/home/user/public_html/wp-content/themes |
get_theme_roots() | 获取主题目录的目录名称 | echo get_theme_roots(); //输出:/themes |
get_stylesheet_directory() | 获取当前启用的主题目录的服务器绝对路径 | /home/user/public_html/wp-content/themes/twentyeleven |
get_stylesheet_directory_uri() | 获取当前启用的主题目录的URI | echo get_stylesheet_directory_uri(); //输出:http://www.objui.com/wp-content/themes/twentyeleven |
get_template_directory_uri() | 如果当前启用的主题是一个child theme,该函数返回parent theme的主题目录URI | 与get_stylesheet_directory_uri()类似 |
get_template_directory() | 如果当前启用的主题是一个child theme,该函数返回parent theme的主题目录的服务器绝对路径 | 与get_stylesheet_directory()类似 |
get_template() | 获取当前启用主题的主题目录名称 | echo get_stylesheet(); //输出:twentyeleven |
get_stylesheet() | 获取当前启用主题的主题目录名称,与get_template()的区别是,如果用了child theme,则返回child theme的目录名称 |
三、插件路径相关函数
名称 | 说明 | 示例 |
---|---|---|
plugins_url() | 获取当前插件的目录的URI | echo plugins_url(); //输出:http://www.objui.com/wp-content/plugins
echo plugins_url('',__FILE__); //输出:http://www.objui.com/wp- content/plugins/myplugin
echo plugins_url('js/myscript.js',__FILE__); //输出:http://www.objui.com/wp-content/plugins/myplugin/js/myscript.js |
plugin_dir_url() | 返回当前插件的目录URI | echo plugin_dir_url(__FILE__ ); //输出:http://www.objui.com/wp-content/plugins/myplugin/ |
plugin_dir_path() | 返回当前插件目录的服务器绝对路径 | echo plugin_dir_path(__FILE__ ); //输出:/home/user/public_html/wp-content/plugins/myplugin/ |
plugin_basename() | 返回调用该函数的插件文件名称(包含插件路径) | echo plugin_basename(__FILE__); //输出:myplugin/myplugin.php |
四、路径相关常量
名称 | 说明 | 示例 |
---|---|---|
WP_CONTENT_DIR | wp-content目录的服务器绝对路径 | /home/user/public_html/wp-content |
WP_CONTENT_URL | wp-content目录的URI地址 | http://www.objui.com/wp-content |
WP_PLUGIN_DIR | 插件目录的服务器绝对路径 | /home/user/public_html/wp-content/plugins |
WP_PLUGIN_URL | 插件目录的URI地址 | http://www.objui.com/wp-content/plugins |
TEMPLATEPATH | 当前启用主题目录的服务器绝对路径 | /home/user/public_html/wp-content/themes/twentyeleven |
STYLESHEETPATH | 当前启用主题目录的服务器绝对路径 | 与TEMPLATEPATH的区别在于如果使用child theme,该常量指向child theme目录。 |