0

我爲自定義帖子類型創建了自定義字段。當我檢索返回值爲22的主題模板圖像值中的值。 如何獲取圖像URL?在主題中回收自定義字段的圖像URL

<?php 
    $args = array(
    'posts_per_page' => -1, 
    'post_type'  => 'projects', 
    'post_status'  => 'publish', 
    ); 
    $projects = get_posts($args); 

    foreach ($projects as $project) { 
     echo $project->project_title; // Title Text 
     echo $project->project_description; // Description Content 
     echo $project->project_image; // 22 

    } ?> 

當我打印$項目

WP_Post Object 
(
    [ID] => 35 
    [post_author] => 1 
    [post_date] => 2017-04-07 05:50:29 
    [post_date_gmt] => 2017-04-07 05:50:29 
    [post_content] => Tekzenit 
    [post_title] => Tekzenit 
    [post_excerpt] => 
    [post_status] => publish 
    [comment_status] => closed 
    [ping_status] => closed 
    [post_password] => 
    [post_name] => tekzenit 
    [to_ping] => 
    [pinged] => 
    [post_modified] => 2017-04-07 06:12:54 
    [post_modified_gmt] => 2017-04-07 06:12:54 
    [post_content_filtered] => 
    [post_parent] => 0 
    [guid] => http://10.180.82.8/fareed/?post_type=projects&p=35 
    [menu_order] => 0 
    [post_type] => projects 
    [post_mime_type] => 
    [comment_count] => 0 
    [filter] => raw 
) 
+1

你的問題不夠清楚。你爲自定義帖子類型創建了**自定義元域**(project_title,project_description,project_image)嗎?你是否也獲得了project_title和project_description的價值? – Ninja

+1

您是否記得將ACF設置爲返回URL或對象?如果你沒有設置它,它可能只是返回ID。 – Stender

回答

0

$project->project_image返回ID

您可以將用戶wp_get_attachment_image_url函數來獲取圖像URL

if($project->project_image){ 
    $imagePath = wp_get_attachment_image_url($project->project_image, '');                  
    echo imagePath 
} 
1

一些更多的信息將是對像,如果你採用了先進的自定義字段或自己寫的代碼,你如何創建自定義字段幫助。

但如果你有圖像ID,那麼你可以使用:

$img_id = $project->project_image; 
$img_array = wp_get_attachment_image_src($img_id, 'full'); 
$img_url = $img_array[0]; 
echo $img_url; 
相關問題