为了 WordPress 更快的运行直接写了绝对地址,其实是不好的,如果修改了 WordPress 程序的地址,编写的这个插件或者是主题就只有你自己用,别人无法使用,为了避免错误,了解 WordPress 中与获取路径相关的函数很重要。
本文主要总结了:站点路径相关函数
/主题路径相关函数
/插件路径相关函数
和路径相关常量
,以下均假设 WordPress 站点安装在 http://www.uedsc.com
下。
返回站点路径,相当于后台设置->常规中的”站点地址(URL)”。
$url = home_url();echo $url;//输出: http://www.uedsc.com $url = home_url('/images/');echo $url;//输出:http://www.uedsc.com/images/
如果 WordPress 安装在域名根目录下,则该函数与 home_url() 相同。
如果 WordPress 安装在子目录下,例如 http://www.uedsc.com/,则 site_url() 返回 WordPress 实际安装地址,相当于后台->设置->常规中的 “WordPress 地址(URL)”。
$url = site_url();echo $url;//假设WordPress安装在http://www.uedsc.com下//输出:http://www.uedsc.com
返回后台地址,传递参数后也可返回后台menu的地址
$url = admin_url();echo $url;//输出:http://www.uedsc.com/wp-admin/
返回实际的 wp-content 目录,如果是默认安装,且装在根目录下,则如下所示
$url = content_url();echo $url;//输出:http://www.uedsc.com/wp-content
如果在 wp-config.php 中改变了 wp-content 目录的位置,则该函数会返回正确地址,例如 wp-config.php 中如下定义
define('WP_CONTENT_DIR','/home/user/public_html/cdn');define('WP_CONTENT_URL','http://sola-cdn.me');
则 content_url() 的返回值为
http://sola-cdn.me
返回当前 WordPress 站点存放核心文件的目录 wp-includes 的地址,可以带一个 $path
作为参数。
$url = includes_url( '/js/');echo $url;//输出:http://www.uedsc.com/wp-includes/js/
返回 WordPress 上传目录的地址,是一个数组,包含一系列与上传地址相关的信息。
<?php $upload_dir = wp_upload_dir(); ?>
提供如下信息给你
例如
$upload_dir = wp_upload_dir();echo $upload_dir['baseurl'];//输出:http://www.uedsc.com/wp-content/uploads
获取存放主题的目录 URI
echo get_theme_root_uri();//输出:http://www.uedsc.com/wp-content/themes
获取存放主题的目录的服务器绝对路径
echo get_theme_root();//输出:<tt>/home/user/public_html/wp-content/themes</tt>
获取主题目录的目录名称,如果你的主题目录是 /wp-content/themes ,则
echo get_theme_roots();//输出:/themes
获取当前启用的主题目录的服务器绝对路径,例如
/home/user/public_html/wp-content/themes/twentyeleven//可以用来 include 文件,例如<?phpinclude( get_stylesheet_directory() . ‘/includes/myfile.php’); ?>
获取当前启用的主题目录的 URI,例如
echo get_stylesheet_directory_uri();//输出:http://www.uedsc.com/wp-content/themes/twentyeleven
可以使用在需要主题目录URI的场合,例如图片
<img src="<?php echo get_stylesheet_directory_uri() ?>/images/aternus.png" alt="" title="" width="" height="" />
如果当前启用的主题是一个 child theme,该函数返回 parent theme 的主题目录URI,用法与 get_stylesheet_directory_uri()
类似。
如果当前启用的主题是一个child theme,该函数返回 parent theme 的主题目录的服务器绝对路径,用法与 get_stylesheet_directory()
类似。
获取当前启用主题的主题目录名称,例如现在启用的主题为 twentyeleven ,则
echo get_stylesheet();//输出:twentyeleven
获取当前启用主题的主题目录名称,与 get_template() 的区别是,如果用了 child theme,则返回 child theme 的目录名称。
获取当前插件的目录的 URI,例如一个插件位于 /wp-content/plugins/myplugin 下,该目录下放有插件的主文件名为 myplugin.php,在 myplugin.php 中执行下面的代码,结果如下
echo plugins_url();//输出:http://www.uedsc.com/wp-content/plugins echo plugins_url('',__FILE__);//输出:http://www.uedsc.com/wp-content/plugins/myplugin echo plugins_url('js/myscript.js',__FILE__);//输出:http://www.uedsc.com/wp-content/plugins/myplugin/js/myscript.js
返回当前插件的目录URI,例如
echo plugin_dir_url(__FILE__ );//输出:http://www.uedsc.com/wp-content/plugins/myplugin///注意结尾有反斜杠。
返回当前插件目录的服务器绝对路径,例如
echo plugin_dir_path(__FILE__ );//输出:/home/user/public_html/wp-content/plugins/myplugin/
可以用来引用文件,例如
<?phpdefine('MYPLUGINNAME_PATH', plugin_dir_path(__FILE__) );require MYPLUGINNAME_PATH . 'includes/class-metabox.php';require MYPLUGINNAME_PATH . 'includes/class-widget.php';?>
返回调用该函数的插件文件名称(包含插件路径)
例如在插件 myplugin 下的 myplugin.php 文件中调用该函数,结果如下
echo plugin_basename(__FILE__);//输出:myplugin/myplugin.php
如果在 myplugin/include/test.php 文件中调用(test.php通过include引用到myplugin.php中),结果如下
echo plugin_basename(__FILE__);//输出:myplugin/include/test.php
WordPress中还有一组用 define
定义的常量代表路径。
wp-content 目录的服务器绝对路径,例如
/home/user/public_html/wp-content
wp-content 目录的 URI 地址,例如
http://www.uedsc.com/wp-content
插件目录的服务器绝对路径,例如
/home/user/public_html/wp-content/plugins
插件目录的URI地址,例如
http://www.uedsc.com/wp-content/plugins
当前启用主题目录的服务器绝对路径,相当于 get_template_directory()
例如
/home/user/public_html/wp-content/themes/twentyeleven
当前启用主题目录的服务器绝对路径,相当于 get_stylesheet_directory()
,与 TEMPLATEPATH 的区别在于如果使用 child theme,该常量指向 child theme 目录。
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。
本文系转载,前往查看
如有侵权,请联系 cloudcommunity@tencent.com 删除。