欢迎您光临10元资源网,本站秉承服务宗旨 履行“站长”责任,销售只是起点 服务永无止境!

wordpress 获取网址的函数简单汇总

wordpress 获取网址的函数简单汇总

wordpress中的路径也不是很负责,有人为了让wordpress运行速度更快,就直接写了绝对地址,其实这样是很不好的,有可能别人修改了wordpress程序的地址,那么这样你编写的这个插件或者是主题就只有你自己用,别人无法使用,这样做得不偿失,为了避免错误,了解 WordPress 中与获取路径相关的函数很重要。

以下均假设 WordPress 站点安装在 https://www.lizi.tw 下。

站点路径相关函数

home_url()

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

  1. $url = home_url();
  2. echo $url;
  3. //输出: https://www.lizi.tw
  4.  
  5. $url = home_url(‘/images/’);
  6. echo $url;
  7. //输出:https://www.lizi.tw/images/

site_url()

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

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

  1. $url = site_url();
  2. echo $url;
  3. //假设 WordPress 安装在 https://www.lizi.tw 下
  4. //输出:https://www.lizi.tw

admin_url()

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

  1. $url = admin_url();
  2. echo $url;
  3. //输出:https://www.lizi.tw/wp-admin/

content_url()

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

  1. $url = content_url();
  2. echo $url;
  3. //输出:https://www.lizi.tw/wp-content

如果在wp-config.php中改变了wp-content目录的位置,则该函数会返回正确地址,例如wp-config.php中如下定义

  1. define(‘WP_CONTENT_DIR’,‘/home/user/public_html/cdn’);
  2. define(‘WP_CONTENT_URL’,‘http://sola-cdn.me’);

则 content_url()的返回值为

  1. http://sola-cdn.me

includes_url()

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

  1. $url = includes_url( ‘/js/’);
  2. echo $url;
  3. //输出:https://www.lizi.tw/wp-includes/js/

wp_upload_dir()

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

  1. <?php $upload_dir = wp_upload_dir(); ?>

提供如下信息给你

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

例如

  1. $upload_dir = wp_upload_dir();
  2. echo $upload_dir[‘baseurl’];
  3. //输出:https://www.lizi.tw/wp-content/uploads

主题路径相关函数

get_theme_root_uri()

获取存放主题的目录 URI

  1. echo get_theme_root_uri();
  2. //输出:https://www.lizi.tw/wp-content/themes

get_theme_root()

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

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

get_theme_roots()

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

  1. echo get_theme_roots();
  2. //输出:/themes

get_stylesheet_directory()

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

  1. /home/user/public_html/wpcontent/themes/twentyeleven

可以用来 include 文件,例如

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

get_stylesheet_directory_uri()

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

  1. echo get_stylesheet_directory_uri();
  2. //输出:https://www.lizi.tw/wp-content/themes/twentyeleven

可以使用在需要主题目录 URI 的场合,例如图片

  1. <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()类似。

get_template_directory()

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

get_template()

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

  1. echo get_stylesheet();
  2. //输出:twentyeleven

get_stylesheet()

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

plugins_url()

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

  1. echo plugins_url();
  2. //输出:https://www.lizi.tw/wp-content/plugins
  3.  
  4. echo plugins_url(,__FILE__);
  5. //输出:https://www.lizi.tw/wp-content/plugins/myplugin
  6.  
  7. echo plugins_url(‘js/myscript.js’,__FILE__);
  8. //输出:https://www.lizi.tw/wp-content/plugins/myplugin/js/myscript.js

plugin_dir_url()

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

  1. echo plugin_dir_url(__FILE__ );
  2. //输出:https://www.lizi.tw/wp-content/plugins/myplugin/

注意结尾有反斜杠。

plugin_dir_path()

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

  1. echo plugin_dir_path(__FILE__ );
  2. //输出:/home/user/public_html/wp-content/plugins/myplugin/

可以用来引用文件,例如

  1. <?php
  2. define(‘MYPLUGINNAME_PATH’, plugin_dir_path(__FILE__) );
  3. require MYPLUGINNAME_PATH . ‘includes/class-metabox.php’;
  4. require MYPLUGINNAME_PATH . ‘includes/class-widget.php’;
  5. ?>

plugin_basename()

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

例如在插件myplugin下的myplugin.php文件中调用该函数,结果如下

  1. echo plugin_basename(__FILE__);
  2. //输出:myplugin/myplugin.php

如果在myplugin/include/test.php文件中调用(test.php通过include引用到myplugin.php中),结果如下

  1. echo plugin_basename(__FILE__);
  2. //输出:myplugin/include/test.php

路径相关常量

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

WP_CONTENT_DIR

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

  1. /home/user/public_html/wpcontent

WP_CONTENT_URL

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

  1. https://www.lizi.tw/wp-content

WP_PLUGIN_DIR

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

  1. /home/user/public_html/wpcontent/plugins

WP_PLUGIN_URL

插件目录的 URI 地址,例如

  1. https://www.lizi.tw/wp-content/plugins

TEMPLATEPATH

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

  1. /home/user/public_html/wpcontent/themes/twentyeleven

STYLESHEETPATH

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


大资源网 » wordpress 获取网址的函数简单汇总

发表评论

售后服务:

  • 售后服务范围 1、商业模板使用范围内问题免费咨询
    2、源码安装、模板安装(一般 ¥200-1000)服务答疑仅限SVIP用户
    3、单价超过1000元的模板免费一次安装,需提供服务器信息。
    付费增值服务 1、提供dedecms模板、WordPress主题、discuz模板优化等服务请详询在线客服
    2、承接 WordPress、DedeCMS、Discuz 等系统建站、仿站、开发、定制等服务
    3、服务器环境配置(一般 ¥50-300)
    4、网站中毒处理(需额外付费,500元/次/质保三个月)
    售后服务时间 周一至周日(法定节假日除外) 9:00-23:00
    免责声明 本站所提供的模板(主题/插件)等资源仅供学习交流,若使用商业用途,请购买正版授权,否则产生的一切后果将由下载用户自行承担,有部分资源为网上收集或仿制而来,若模板侵犯了您的合法权益,请来信通知我们(Email: 263795763@qq.com),我们会及时删除,给您带来的不便,我们深表歉意!

Hi, 如果你对这款模板有疑问,可以跟我联系哦!

联系作者
';