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 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程