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

“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条)

相关推荐 更多相似问题