douxidang9092 2018-08-16 19:52
浏览 56
已采纳

“wp_get_attachment_url”在functions.php中不起作用

This works just fine in any other post:

$vid_att = get_attached_media('video',$post_id); // Check if a video is attached
// print_r($vid_att);
if ($vid_att) {
$vid_att_url = wp_get_attachment_url($vid_att->ID);
echo $vid_att_url;
}

HOWEVER

if I use this within my functions.php file it will return the full array (print_r($vid_att);) but will not return the ID when requested!

If I insert the attachment ID manually it works:

$vid_att_url = wp_get_attachment_url('138');

What can it be? I've tried converting ID to Integer and other stuff but cannot get it to work. Also ne errors given.

The array it returns looks like this:

Array ( [138] => WP_Post Object ( [ID] => 138 [post_author] => 1 [post_date] => 2018-08-14 16:21:42 [post_date_gmt] => 2018-08-14 16:21:42 [post_content] => [post_title] => SampleVideo_1280x720_1mb [post_excerpt] => [post_status] => inherit [comment_status] => open [ping_status] => closed [post_password] => [post_name] => samplevideo_1280x720_1mb [to_ping] => [pinged] => [post_modified] => 2018-08-14 16:21:42 [post_modified_gmt] => 2018-08-14 16:21:42 [post_content_filtered] => [post_parent] => 113 [guid] => http://flockstock/wp-content/uploads/edd/2018/08/SampleVideo_1280x720_1mb.mp4 [menu_order] => 0 [post_type] => attachment [post_mime_type] => video/mp4 [comment_count] => 0 [filter] => raw ) )
  • 写回答

3条回答 默认 最新

  • duankeng2026 2018-08-17 13:44
    关注

    Conclusion:

    $vid_att = get_attached_media('video',$post_id); // Check if a video is attached
    
        if ($vid_att) {
    
                $vid_att_arr = array_values($vid_att);
                $vid_att_ID = $vid_att_arr[0]->ID;
                $vid_att_url = wp_get_attachment_url($vid_att_ID);
                // echo $vid_att_url;
            }else{
                echo 'No Video uploaded!';
            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名