WordPress常用路径函数

WordPress 开发来源:网络62.4K阅读模式

WordPress常用路径函数

  1. <?php
  2.     // 不允许直接访问此文件。  
  3.     if ( ! defined( 'ABSPATH' ) ) {
  4.           exit( '拒绝直接访问此脚本.' );
  5.     }
  6.     /** 
  7.      * 定义框架常用的几个路径,你也可以添加自己的路径在此 
  8.      *  
  9.      */
  10.     //定义框架文件夹,默认为主题目录下的redux  
  11.     /** 以下为常用路径函数总结,可参考添加自己用到的变量。*/
  12.     //主题常用路径总结  
  13.     $op_home_url = home_url();  //输出: http://www.xxxx.com  
  14.     $op_images_url = home_url('/images/');  //输出: http://www.xxxx.com/images/  
  15.     $op_site_url = site_url(); //如果WordPress安装在子目录下,返回http://www.xx.com/wordpress,相当于后台->设置->常规中的“WordPress 地址(URL)”。  
  16.     $op_admin_url = admin_url(); //输出:http://www.solagirl.net/wp-admin/  
  17.     $op_content_url = content_url(); //输出:http://www.solagirl.net/wp-content 如果在wp-config.php中改变了wp-content目录的位置,则该函数会返回正确地址  
  18.     $op_includes_url = includes_url( '/js/'); //输出:http://www.solagirl.net/wp-includes/js/  
  19.     $op_wp_upload_dir = wp_upload_dir();
  20.     /** 可用参数 
  21.     * 示例:$upload_dir = wp_upload_dir();   
  22.     * echo $upload_dir['baseurl'];  //输出:http://www.solagirl.net/wp-content/uploads 
  23.     * 'path' - 上传目录的服务器绝对路径,通常以反斜杠(/)开头 
  24.     * 'url' - 上传目录的完整URL 
  25.     * 'subdir' - 子目录名称,通常是以年/月形式组织的目录地址,例如/2012/07 
  26.     * 'basedir' - 上传目录的服务器绝对路径,不包含子目录 
  27.     * 'baseurl' - 上传目录的完整URL,不包含子目录 
  28.     * 'error' - 报错信息. 
  29.     */
  30.     $op_get_theme_root_uri = get_theme_root_uri(); //获取存放主题的目录URI 输出:http://www.solagirl.net/wp-content/themes  
  31.     $op_get_theme_root = get_theme_root(); //获取存放主题的目录的服务器绝对路径 输出:<tt>/home/user/public_html/wp-content/themes</tt>  
  32.     $op_get_theme_roots = get_theme_roots(); //获取主题目录的目录名称,如果你的主题目录是/wp-content/themes,则输出:/themes  
  33.     $op_get_stylesheet_directory = get_stylesheet_directory();
  34.     /** //获取当前启用的主题目录的服务器绝对路径, 
  35.     * /home/user/public_html/wp-content/themes/twentyeleven 
  36.     * 可以用来include文件,例如 
  37.     * <?php include( get_stylesheet_directory() . '/includes/myfile.php'); ?> 
  38.     */
  39.     $op_get_stylesheet_directory_uri = get_stylesheet_directory_uri();
  40.     /** //获取当前启用的主题目录的URI  
  41.     * 输出:http://www.solagirl.net/wp-content/themes/twentyeleven 
  42.     * 可以使用在需要主题目录URI的场合,例如图片 
  43.     * <img src="<?php echo get_stylesheet_directory_uri() ?>/images/aternus.png" alt="" title="" width="" height="" /> 
  44.     */
  45.     $op_GET_TEMPLATE_DIRECTORY_URI = GET_TEMPLATE_DIRECTORY_URI(); //如果当前启用的主题是一个child theme,该函数返回parent theme的主题目录URI,用法与get_stylesheet_directory_uri()类似。  
  46.     $op_GET_TEMPLATE_DIRECTORY = GET_TEMPLATE_DIRECTORY(); //如果当前启用的主题是一个child theme,该函数返回parent theme的主题目录的服务器绝对路径,用法与get_stylesheet_directory()类似。  
  47.     $op_get_stylesheet = get_stylesheet();//获取当前启用主题的主题目录名称,与get_template()的区别是,如果用了child theme,则返回child theme的目录名称。  
  48.     $op_get_template = get_template();//获取当前启用主题的主题目录名称,例如现在启用的主题为twentyeleven,则输出:twentyeleven  
  49.     //插件常用路径总结  
  50.     $op_plugins_url() = plugins_url(); //输出:http://www.solagirl.net/wp-content/plugins  
  51.     $op_plugins_url('',__FILE__) = plugins_url(); //输出:http://www.solagirl.net/wp-content/plugins/myplugin  
  52.     $op_plugins_url('js/myscript.js',__FILE__); = plugins_url(); //输出:http://www.solagirl.net/wp-content/plugins/myplugin/js/myscript.js  
  53.     $op_plugin_dir_url__FILE__ ) = plugin_dir_url( __FILE__ ); //输出:(注意结尾有反斜杠):http://www.solagirl.net/wp-content/plugins/myplugin/  
  54.     $op_plugin_dir_path__FILE__ ) = plugin_dir_path( __FILE__ );
  55.     /**  
  56.     * 输出:/home/user/public_html/wp-content/plugins/myplugin/ 
  57.     * 可以用来include文件,例如 
  58.     * <?php 
  59.         define( 'MYPLUGINNAME_PATH', plugin_dir_path(__FILE__) ); 
  60.         require MYPLUGINNAME_PATH . 'includes/class-metabox.php'; 
  61.         require MYPLUGINNAME_PATH . 'includes/class-widget.php'; 
  62.       ?> 
  63.     */
  64.     $op_plugin_basename(__FILE__) = plugin_basename(__FILE__);
  65.     /**  
  66.     * 返回调用该函数的插件文件名称(包含插件路径) 
  67.       例如在插件myplugin下的myplugin.php文件中调用该函数,结果如下 
  68.       echo plugin_basename(__FILE__);  
  69.       //输出:myplugin/myplugin.php 
  70.       如果在myplugin/include/test.php文件中调用(test.php通过include引用到myplugin.php中),结果如下 
  71.       echo plugin_basename(__FILE__); 
  72.       //输出:myplugin/include/test.php 
  73.     */
  74.     $op_redux_path = get_template_directory() . '/redux';
  75.     $op_redux_path = get_template_directory() . '/redux';
  76.     $op_redux_path = get_template_directory() . '/redux';
  77.     //定义框架文件夹,默认为主题目录下的redux  
  78.     $op_plugins_path = get_template_directory() . '/redux/plugins';
  79.     //定义框架文件夹,默认为主题目录下的redux  
  80.     $op_themes_path = get_template_directory() . '/redux/themes';
  81.     //定义框架文件夹,默认为主题目录下的redux  
  82.     $op_uploads_path = get_template_directory() . '/redux/uploads';
  83.     //定义框架文件夹,默认为主题目录下的redux  
  84.     $op_languages_path = get_template_directory() . '/redux/languages';
  85.     //定义框架文件夹,默认为主题目录下的redux  
  86.     $op_wp_content_path = get_template_directory() . '/redux';
  87.     //定义框架文件夹,默认为主题目录下的redux  
  88.     $op_wp_includes = get_template_directory() . '/redux';

本站文章大部分始于原创,用于个人学习记录,可能对您有所帮助,仅供参考!

weinxin
我的微信
版权声明
本站原创文章转载请注明文章出处及链接,谢谢合作!
 
知更鸟
评论  6  访客  6
    • boke112导航
      boke112导航 4

      这种教程很实用,要不然对这些知识都是懵懵懂懂的

      • 网站建设
        网站建设 4

        不错,简单易懂

        • 励志语录
          励志语录 6

          看不懂 :lol:

          • 我爱动感单车网
            我爱动感单车网 7

            进入2018年以来,基本没有关注过自己的博客,对WordPress已经变得陌生了。

            • 米扑博客
              米扑博客 2

              非常实用,干货呀

              • 广州网站建设
                广州网站建设 4

                好用,赞

              匿名

              发表评论

              匿名网友
              :?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

              拖动滑块以完成验证