简体   繁体   English

全局Wordpress IMG目录功能

[英]Global Wordpress IMG directory function

I am using Bones theme to develop wordpress themes. 我正在使用Bones主题来开发wordpress主题。 Images are stored in 'theme/lib/images/site/' . 图像存储在'theme / lib / images / site /'中 To load an image in a template I a currently using: 要将图像加载到当前使用的模板中:

<?php get_template_directory_uri().'/library/images/'; ?>

I would like to add this directory as a global variable in functions.php so I can use a shortcode to load images, for example. 我想将此目录添加为functions.php中的全局变量,以便例如可以使用简码加载图像。

<?php echo imgDir(); ?>

My first thought would be to use something like 我的第一个想法是使用类似

function imgDir() {
     $path_to_image = get_template_directory_uri() . '/library/images/';
}

But does not seem to be working, am I missing something simple here? 但是似乎没有用,我在这里错过了一些简单的东西吗? Shouldn't be to hard to add a short code like this. 应该不难添加这样的简短代码。

You need to return your value: 您需要返回您的值:

function imgDir() {
    return get_template_directory_uri() . '/library/images/';
}

The proper way to add a shortcode hook according to the Wordpress Documentation is this. 根据Wordpress文档添加短代码挂钩的正确方法是:

Inside your functions.php 在您的functions.php中

function imgDir( $atts ){
    return get_template_directory_uri() . '/library/images/';
}
add_shortcode( 'imgdir', 'imgDir' );

So the shortcode [imgdir] will return the desired path. 因此,简码[imgdir]将返回所需的路径。

To call a shortcode inside your PHP-File just use the do_shortcode() function like this - 要在您的PHP文件中调用简码,只需使用do_shortcode()函数-

echo do_shortcode('[imgdir]'); ( Documentation ) 文件

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

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