2013-03-13 56 views
0

我正在使用名爲Types的wordpress插件。WordPress類型插件獲取帖子標題而不是圖片標題

我正在使用自定義字段功能將圖片上傳到我的自定義邊欄中的圖庫中。我也使用燈箱來顯示這些圖像。

所以我想獲取每個圖像的標題出現

<?php $resortimages = get_post_meta(get_the_ID(), 'wpcf-r-images'); 
foreach ($resortimages as $resortimage) { 
echo '<li><a href="'. $resortimage. '" rel="lightbox" title="" ><img src="'. $resortimage. '"/></a></li>'; 
} 

我試圖讓文章的標題,但它只是得到職位本身的稱號。

回答

0

Jesper,它看起來像類型圖像域只存儲圖像的URL,而不是它的內容。如果您想檢索其他信息(例如標題,標題和說明),可能您必須嘗試通過其URL獲取圖像ID。在你的functions.php:

/** 
* Retrieve the attachment ID from its URL 
* 
* @param string $image_url 
* @return int $attachment[0] The attachment ID 
* 
* @link http://pippinsplugins.com/retrieve-attachment-id-from-image-url/ 
*/ 
function mytheme_get_attachment_id($image_url) { 
    global $wpdb; 

    $prefix = $wpdb->prefix; 
    $attachment = $wpdb->get_col($wpdb->prepare("SELECT ID FROM " . $prefix . "posts" . " WHERE guid='%s';", $image_url)); 
     return $attachment[0]; 

} 

之後,你可以用你foreach並檢查您的ID:

<?php 
$resortimages = get_post_meta(get_the_ID(), 'wpcf-r-images'); 

foreach ($resortimages as $resortimage) { 

    // Get attachment ID by its URL 
    if ($resortid = mytheme_get_attachment_id($resortimage)) 
     $resortname = get_the_title($resortid); 

    echo '<li><a href="'. $resortimage. '" rel="lightbox" title="' . $resorttitle . ' ><img src="'. $resortimage. '"/></a></li>'; 
} 
?> 

但是,你會做的查詢數量的提防。希望能幫助到你!