2013-03-13 70 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>'; 
} 
?> 

但是,你会做的查询数量的提防。希望能帮助到你!

相关问题