wp_upload_dir如何只获取目录名称。


18

我想这对某些人来说是一件轻而易举的事-但我很难仅获取上载的目录名-而不是完整路径-我已经走到了这一步:

$uploads = wp_upload_dir();
$upload_path = $uploads['baseurl']; // now how to get just the directory name?

有人有想法么?感谢您分享您的体验。。。


5
basename($uploads['baseurl'])
onetrickpony 2012年

Answers:


26

这是您从函数中得到的结果:

Array
(
    [path] => C:\development\xampp\htdocs\example.com/content/uploads/2012/04
    [url] => http://example.com/content/uploads/2012/04
    [subdir] => /2012/04
    [basedir] => C:\~\example.com/content/uploads
    [baseurl] => http://example.com/content/uploads
    [error] => 
)

因此,您可以使用(如@OneTrickPony所指出的)获取文件夹/目录名称

echo wp_basename( $uploads['baseurl'] );

如果您正在运行多站点并且定义了常量UPLOADS,则可以从UPLOADS或访问它BLOGUPLOADDIR

编辑

对于多站点,您将获得以下内容:

Array
(
    [path] => /var/www/example.com/public_html/wp-content/uploads/sites/2/2016/12,
    [url] => http://example.com/wp-content/uploads/sites/2/2016/12,
    [subdir] => /2016/12,
    [basedir] => /var/www/example.com/public_html/wp-content/uploads/sites/2,
    [baseurl] => http://example.com/wp-content/uploads/sites/2,
    [error] => ,
)

后面的“ 2” sites是博客的ID


2
-多亏你们两个 基本名称,经过一番研究wp_basename我也认为。
orionrush 2012年

wp_basename是“ i18n的basename()友好版本” codex.wordpress.org/Function_Reference/wp_basename
Chaoley
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.