这在其他任何帖子中都可以正常使用:
$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;
}
但是
如果我在functions.php文件中使用此函数,它将返回完整的数组(print_r($ vid_att);),但在请求时不会返回ID!
如果我手动插入附件ID,它将起作用:
$vid_att_url = wp_get_attachment_url('138');
那会是什么?我试过将ID转换为Integer和其他内容,但无法使其正常工作。还给出了其他错误。
它返回的数组如下:
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 ) )
答案 0 :(得分:0)
尝试像这样先声明超级变量:
global $post;
echo $post->ID;
答案 1 :(得分:0)
感谢@wkille指出答案所在的帖子: Get attachment URL
在使用数组之前对其重新索引:
$vid_att = array_values($vid_att);
echo 'GUID: ' . $vid_att[0]->guid;
注意:
wp_get_attachment_url($post_id->ID);
即使重新索引数组,它仍然不能在functions.php中工作,但是它确实可以在帖子中使用,有人知道为什么吗?
答案 2 :(得分:0)
结论:
$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!';
}