如何在WordPress中从图像附件中检索“自定义url”

时间:2013-11-06 作者:gteh

我已经上传了一堆图片到帖子中,并在媒体上传器中为每个图片设置了“自定义url”。

如何将分配给每个图像的自定义URL检索到底部的$caption\\u text=中?

    $images =& get_children( array( 
                            \'post_parent\' => $featured_page,
                            \'post_status\' => \'inherit\',
                            \'post_type\' => \'attachment\',
                            \'post_mime_type\' => \'image\',
                            \'order\' => \'ASC\',
                            \'numberposts\' => $thelimit,
                            \'orderby\' => \'menu_order\' )
                            );

// Loop through the images
foreach ( $images as $id => $image ) {
    $attatchmentID = $image->ID;

    $imagearray = wp_get_attachment_image_src( $attatchmentID , \'fullsize\', false);
    $imageURI = $imagearray[0];
    $imageID = get_post($attatchmentID);

    $thumb_imagearray = wp_get_attachment_image_src( $attatchmentID , \'fullscreen-thumbnails\', false);
    $thumb_imageURI = $thumb_imagearray[0];

    $imageTitle = $image->post_title;
    $imageDesc = $image->post_content;
    $postlink = get_permalink($image->post_parent);
    // If linking is On
    if ($featured_linked == 1 || $featured_linked == true) {
        $attatchmentURL = get_attachment_link($image->ID);
    }
    // Count
    $count++;
    if ($count>1) { echo ","; }

    $slideshow_title="";
    $slideshow_caption="";

    //Find and replace all new lines to BR tags
    $find   = array("\\r\\n", "\\n", "\\r");
    $replace = \'<br />\';
    $imageDesc = str_replace($find, $replace , $imageDesc);
    $image_post_link = get_permalink();


    if ($imageTitle) $slideshow_title=\'<div class="slideshow_title">\'. esc_attr($imageTitle) .\'</div>\';
    if ($imageDesc) $slideshow_caption=\'<div class="slideshow_caption">\'. $imageDesc .\'</div>\';


    $caption_text= \'<div class="slideshow_captionwrap">\' . $slideshow_title . $slideshow_caption . \'> <a class="slideshow_cap_link" href="\' . $image_post_link . \'">Preview Article</a></div>\';


    echo "{image : \'".$imageURI."\', title : \'". $caption_text . "\', thumb : \'".$thumb_imageURI."\', url : \'\'}";
}

1 个回复
SO网友:gteh

我在这里找到了解决方案:http://wp.tutsplus.com/tutorials/creative-coding/how-to-add-custom-fields-to-attachments/

这是一个简单的插件,可以向附件媒体编辑器添加自定义字段。您可以修改字段,然后在主题中显示值。

谢谢大家的建议。

结束

相关推荐

Define Folder to save Images

插件将图像保存到/PLUGINNAME/Images,但我希望它移动到/uploads插件中的代码如下define(\'WPR_URLPATH\', WP_PLUGIN_URL.\'/\'.plugin_basename( dirname(__FILE__) ).\'/\' ); $wpr_saveurl = WPR_URLPATH . \"images\"; $wpr_cache = ABSPATH . \"wp-content/plugins/\". plugin_basename(