[フレーム]
Last Updated: February 25, 2016
·
3.766K
· rolandfoto

Using Wordpress attached image "custom URL" outside the content

The new editor in WP3.5 gives the ability to add a "Custom URL" to an image that you can attach to a page or post. The caveat is that this URL is only available when the image is used via post_content.

I wanted to use this feature outside the content's text and hit a wall. My solution is a hack but maybe helpful for others.

<?php
//define the arguments to retrieve the posts attachments
 $args = array(
 'post_type' => 'attachment',
 'numberposts' => -1,
 'post_status' => null,
 'post_parent' => $post->ID
 );

 //get the attached images
 $attachments = get_posts( $args );
 //next, see if a custom url is entered via the "alt text" input field since this registers a post_meta
 for($i=0;$i<count($attachments);$i++){
 $url = get_post_meta($attachments[$i]->ID,'_wp_attachment_image_alt');
 $attachments[$i]->url = $url[0]; //add url to attachment
 }
?>

After that, simply loop through the attachments when needed and echo the 'url' parameter.

AltStyle によって変換されたページ (->オリジナル) /