简体   繁体   English

你如何添加一个lib命名空间到WordPress的根圣人?

[英]How do you add a lib namespace to Wordpress roots sage?

Following the documentation written here , I added a file to lib directory containing: 根据此处编写的文档,我向lib目录添加了一个文件,其中包含:

<?php

namespace Theme\URIs;

/**
 * Returns theme images directory uri
 */
function get_images_directory_uri() {
    return get_template_directory_uri() . '/assets/images';
}

Which I then tried calling from a template file with: 然后我尝试使用以下模板文件从模板文件中调用:

<?php print Theme\URIs\get_images_directory_uri(); ?>

Calling this however, returned a Fatal error: Call to undefined function Theme\\URIs\\get_images_directory_uri() . 但是,调用此方法将返回Fatal error: Call to undefined function Theme\\URIs\\get_images_directory_uri()

The solution is to add the file to the $sage_includes array in functions.php , so it looks like this: 解决方案是将文件添加到functions.php$sage_includes数组中,因此如下所示:

$sage_includes = [
  'lib/assets.php',  // Scripts and stylesheets
  'lib/extras.php',  // Custom functions
  'lib/setup.php',   // Theme setup
  'lib/titles.php',  // Page titles
  'lib/wrapper.php',  // Theme wrapper class
  'lib/uris.php' // Project URIs
];

This imports the file to be used in the theme. 这将导入要在主题中使用的文件。

The present documentation does not explain this. 本文档没有对此进行解释。 (I assumed it simply imported all files in the libs directory...) (我假设它只是将所有文件导入了libs目录中...)

Not the question (I can't comment yet) but unless you've changed the directory structure your function should probably return: 这不是问题(我不能发表评论),但是除非您更改了目录结构,否则函数应该返回:

get_template_directory_uri() . '/dist/images';

As the images in /assets/images get optimised and then copied to /dist/images . 随着/assets/images得到优化,然后复制到/dist/images

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM