文章目录
本文最后更新于 2022-10-29,已超过 365 天未更新,内容可能已失效。

与WordPress打交道,经常遇到的一个问题就是获取路径,包括URL路径和服务器路径,在主题或插件中引用js或css文件需要URL地址,而include一些文件时则需要服务器路径。在WordPress中,不能认定wp-content目录一定位于/wp-content下,也不能认为admin的地址一定是/wp-admin,为了避免错误,了解WordPress中与获取路径相关的函数很重要。

站点路径相关函数

返回站点路径,相当于后台设置->常规中的”站点地址(URL)“。

home_url()

$url = home_url();
echo $url;

site_url()

如果WordPress安装在域名根目录下,则该函数与home_url()相同。

如果WordPress安装在子目录下,例如https://zpthems.com/wordpress,则site_url()返回WordPress实际安装地址,相当于后台->设置->常规中的“WordPress 地址(URL)”。

$url = site_url();
echo $url;

admin_url()

返回后台地址,传递参数后也可返回后台menu的地址

$url = admin_url();
echo $url;

//输出:https://zpro.zptheme.com/wp-admin/

content_url()

返回实际的wp-content目录,如果是默认安装,且装在根目录下,则如下所示

$url = content_url();
echo $url;

//输出:https://zpro.zptheme.com/wp-content

includes_url()

返回当前WordPress站点存放核心文件的目录wp-includes的地址,可以带一个$path作为参数。

$url = includes_url( '/js/');
echo $url;

//输出:https://zpro.zptheme.com/wp-includes/js/

wp_upload_dir()

返回WordPress上传目录的地址,是一个数组,包含一系列与上传地址相关的信息。

<?php 
$upload_dir = wp_upload_dir(); 
?>
//提供如下信息给你
* 'path' - 上传目录的服务器绝对路径,通常以反斜杠(/)开头
* 'url' - 上传目录的完整URL
* 'subdir' - 子目录名称,通常是以年/月形式组织的目录地址,例如/2012/07
* 'basedir' - 上传目录的服务器绝对路径,不包含子目录
* 'baseurl' - 上传目录的完整URL,不包含子目录
* 'error' - 报错信息.

主题路径相关函数

get_theme_root_uri()

//获取存放主题的目录URI
echo get_theme_root_uri();

//输出:https://zpro.zptheme.com/wp-content/themes

get_theme_root()

//获取存放主题的目录的服务器绝对路径

echo get_theme_root();

//输出:/home/user/public_html/wp-content/themes

get_theme_roots()

//获取主题目录的目录名称,如果你的主题目录是/wp-content/themes,则

echo get_theme_roots();

//输出:/themes

get_stylesheet_directory()

//获取当前启用的主题目录的服务器绝对路径,例如

/home/user/public_html/wp-content/themes/twentyeleven

//可以用来include文件,例如

include( get_stylesheet_directory() . '/includes/myfile.php');

get_stylesheet_directory_uri()

//获取当前启用的主题目录的URI,例如

echo get_stylesheet_directory_uri();

//输出:https://zpro.zptheme.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()

//获取当前启用主题的主题目录名称,例如现在启用的主题为twentyeleven,则

echo get_stylesheet();

//输出:twentyeleven

get_stylesheet()

获取当前启用主题的主题目录名称,与get_template()的区别是,如果用了child theme,则返回child theme的目录名称。

路径相关常量

WordPress中还有一组用define定义的常量代表路径。

WP_CONTENT_DIR

//wp-content目录的服务器绝对路径,例如

/home/user/public_html/wp-content

WP_CONTENT_URL

//wp-content目录的URI地址,例如

https://zpro.zptheme.com/wp-content

WP_PLUGIN_DIR

插件目录的服务器绝对路径,例如

/home/user/public_html/wp-content/plugins

WP_PLUGIN_URL

//插件目录的URI地址,例如

https://zpro.zptheme.com/wp-content/plugins

TEMPLATEPATH

当前启用主题目录的服务器绝对路径,相当于get_template_directory()例如

/home/user/public_html/wp-content/themes/twentyeleven

STYLESHEETPATH

当前启用主题目录的服务器绝对路径,相当于get_stylesheet_directory(),与TEMPLATEPATH的区别在于如果使用child theme,该常量指向child theme目录。

CC 许可协议
署名-非商业性使用-相同方式共享 4.0 国际 了解协议
文章链接:https://zptheme.com/5527.html
本文作者:子佩·来源:子佩工作室

除非注明,子佩工作室 文章均为原创,转载请以链接形式标明本文地址。

相关推荐

登录后即可发表评论

社交账号登录

全部评论 (0)