243
文章
15
说说
244
评论
248529
访问

个人博客网站:prettywordpress.com(吃饭用大碗的程序猿)

最新评论
qq_avatar
1年前,”四五设计网”在《如何使用百度云CDN优化wordpress速度》
说:来学习一下,应该用得上
qq_avatar
2年前,” ”在《友情链接》
说:像不能用了更新地址为 https://aba.pet/wp-content/uploads/2022/05/favicon-1.gif
qq_avatar
2年前,”Alex”在《终于找到拖慢网站的罪魁祸首》
说:优化这么快,你有想过我们的感觉?
qq_avatar
3年前,”ヘル”在《码农=新生代农民工》
说:打卡@[huaixiao]
qq_avatar
3年前,”肋巴骨”在《Win10系统桌面一直闪屏自动刷新怎么办》
说:不错啊,这是谁写的教程,真棒
A+

分享wordpress常用的快捷地址

标签: 最后编辑:2020年5月6日

wordpress的路径一般不能写绝对路径,特别是编写程序的时候。如果别人用了你写的插件或者别人更改了文件路径,那你的网站就会报错!!!

下面假装你的wordpress安装在http://www.abc.com根目录下。

一、站点路径相关函数

1.home_url()

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

$url = home_url();
echo $url;
//输出: http://www.abc.com

$url = home_url('/images/');
echo $url;
//输出:http://www.abc.com/images/

2.site_url()

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

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

$url = site_url();
echo $url;
//假设WordPress安装在http://www.abc.com下
//输出:http://www.abc.com

3.admin_url()

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

$url = admin_url();
echo $url;
//输出:http://www.abc.com/wp-admin/

4.content_url()

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

$url = content_url();
echo $url;
//输出:http://www.abc.com/wp-content
如果在wp-config.php中改变了wp-content目录的位置,则该函数会返回正确地址
则content_url()的返回值为http://sola-cdn.me

5.includes_url()

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

$url = includes_url( '/js/');
echo $url;
//输出:http://www.abc.com/wp-includes/js/

6.wp_upload_dir()

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

<?php $upload_dir = wp_upload_dir(); ?>
提供如下信息给你

‘path’ – 上传目录的服务器绝对路径,通常以反斜杠(/)开头
‘url’ – 上传目录的完整URL
‘subdir’ – 子目录名称,通常是以年/月形式组织的目录地址,例如/2012/07
‘basedir’ – 上传目录的服务器绝对路径,不包含子目录
‘baseurl’ – 上传目录的完整URL,不包含子目录
‘error’ – 报错信息.
例如

$upload_dir = wp_upload_dir();
echo $upload_dir['baseurl'];
//输出:https://www.abc.com/wp-content/uploads
主题路径相关函数

7.get_theme_root_uri()

获取存放主题的目录URI

echo get_theme_root_uri();
//输出:http://www.abc.com/wp-content/themes

8.get_theme_root()

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

echo get_theme_root();
//输出:<tt>/home/user/public_html/wp-content/themes</tt>

9.get_theme_roots()

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

echo get_theme_roots();
//输出:/themes

10.get_stylesheet_directory()

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

/home/user/public_html/wp-content/themes/twentyeleven
可以用来include文件,例如

<?phpinclude( get_stylesheet_directory() . ‘/includes/myfile.php’); ?>

11.get_stylesheet_directory_uri()

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

echo get_stylesheet_directory_uri();
//输出:http://www.abc.com/wp-content/themes/twentyeleven
可以使用在需要主题目录URI的场合,例如图片

<img src="<?php echo get_stylesheet_directory_uri() ?>/images/aternus.png" alt="" title="" width="" height="" />
get_template_directory_uri()

如果当前启用的主题是一个child theme,该函数返回parent theme的主题目录URI,用法与get_stylesheet_directory_uri()类似。

12.get_template_directory()

如果当前启用的主题是一个child theme,该函数返回parent theme的主题目录的服务器绝对路径,用法与get_stylesheet_directory()类似。

13.get_template()

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

echo get_stylesheet();
//输出:twentyeleven

14.get_stylesheet()

获取当前启用主题的主题目录名称,与get_template()的区别是,如果用了child theme,则返回child theme的目录名称。
插件路径相关函数

15.plugins_url()

获取当前插件的目录的URI,例如一个插件位于/wp-content/plugins/myplugin下,该目录下放有插件的主文件名为myplugin.php,在myplugin.php中执行下面的代码,结果如下

echo plugins_url();
//输出:http://www.abc.com/wp-content/plugins

echo plugins_url('',__FILE__);
//输出:http://www.abc.com/wp-content/plugins/myplugin

echo plugins_url('js/myscript.js',__FILE__);
//输出:http://www.abc.com/wp-content/plugins/myplugin/js/myscript.js

16.plugin_dir_url()

返回当前插件的目录URI,例如

echo plugin_dir_url(__FILE__ );
//输出:http://www.abc.com/wp-content/plugins/myplugin/
注意结尾有反斜杠。

17.plugin_dir_path()

返回当前插件目录的服务器绝对路径,例如

echo plugin_dir_path(__FILE__ );
//输出:/home/user/public_html/wp-content/plugins/myplugin/
可以用来引用文件,例如

<?php
define('MYPLUGINNAME_PATH', plugin_dir_path(__FILE__) );
require MYPLUGINNAME_PATH . 'includes/class-metabox.php';
require MYPLUGINNAME_PATH . 'includes/class-widget.php';
?>

18.plugin_basename()

返回调用该函数的插件文件名称(包含插件路径)

例如在插件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_DIR

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

/home/user/public_html/wp-content

二、WP_CONTENT_URL

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

http://www.abc.com/wp-content

三、WP_PLUGIN_DIR

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

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

四、WP_PLUGIN_URL

插件目录的URI地址,例如

http://www.abc.com/wp-content/plugins

知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议

发表一下你的评论呗

回复评论代表你同意网站的 隐私政策

... 友情提示 请保留版权标识
复制成功!
目录