获取附件地址 [英] Get attachment URL

查看:26
本文介绍了获取附件地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从 WordPress 获取上传的音频文件,但遇到了一些问题.我需要获取不包括域名的补丁,例如 wp-content/uploads/2014/09/file.mp3.

I am attempting to grab the uploaded audio file from WordPress but having some issues. I need to grab the patch excluding the domain name so something like wp-content/uploads/2014/09/file.mp3.

我尝试使用 get_attached_media(),当我将 var_dump() 存储在一个带有 键的数组中时,我可以准确地看到我需要什么指导.我尝试了几种不同的方法,但我无法访问它.这就是结果.

I tried to use get_attached_media() and I can see exactly what I need when I var_dump() it is stored in an array with the key of guid. I have tried several different ways but I cannot access it. This is the result.

object(WP_Post)#2059 (24) {
 ["ID"]=>
 int(4312)
 ["post_author"]=>
 string(1) "3"
 ["post_date"]=>
 string(19) "2014-10-06 15:33:16"
 ["post_parent"]=>
 int(4298)
 ["guid"]=>
 string(73) "/wp-content/uploads/2014/09/file.mp3"

 }
}

我删除了大部分代码以保持简短.如何访问 guid 键?我将结果存储在 $a 并尝试了 $a->guid $a['guid']$a->post->guid 和其他人,但没有运气.

I removed most of the code to keep it brief. How can I access the guid key? I store the results in $a and tried $a->guid $a['guid']and $a->post->guid and others but no luck.

任何帮助将不胜感激.

推荐答案

如果您查看 的文档get_attached_media 函数,您将看到它返回一个 WP_Post 对象数组.

If you look at the documentation of the get_attached_media function you will see that it returns an array of WP_Post objects.

该数组由附件 id 索引,因此您无法通过执行 $a[0] 来访问第一个元素.我建议您在使用之前重新索引数组,如下所示:

The array is indexed by attachment id so you can't access the first element simply by doing $a[0]. I would recommend that you reindex the array before using it, like this:

$a = get_attached_media(...);
$a = array_values($a);
echo 'GUID: ' . $a[0]->guid;

您也可以像这样遍历所有附件

You could also iterate over all the attachments like this

foreach ($a as $attachment) {
    echo "GUID: {$attachment->guid}\n";    
}

这篇关于获取附件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆