Need output to display all images - php

I have this code where I'm trying to display all three images attached to visual composer's custom "attach_images" element type that has param "macimgs". The problem is that only the last one image will display and if I inspect the container that holds images, I see only one image inside instead of three.
Any ideas of what to modify here?
$gallery = shortcode_atts(
array(
'macimgs' => 'macimgs',
), $atts );
$image_ids=explode(',',$gallery['macimgs']);
$image_no = 1;
foreach( $image_ids as $image_id ){
$images = wp_get_attachment_image_src( $image_id, 'full' );
$output ='
<img src="'. $images[0] .'" alt="" />
';
$image_no++;
}
return $output;
}

i think you forget to concatenate the $output variable.
try this code
$gallery = shortcode_atts(
array(
'macimgs' => 'macimgs',
), $atts );
$image_ids = explode(',',$gallery['macimgs']);
$output = '';
$image_no = 1;
foreach( $image_ids as $image_id ){
$images = wp_get_attachment_image_src( $image_id, 'full' );
$output .='<img src="'. $images[0] .'" alt="" />';
$image_no++;
}
return $output;

Related

Getting image alt from WordPress. get_post_thumbnail_id() not working

In WPBakery, I've mapped out a module which requires an image:
public function image_html($atts) {
extract(
shortcode_atts(
array(
'image' => '',
),
$atts
)
);
$getImage = shortcode_atts(
array(
'image' => 'image',
),
$atts
);
$image_ids = explode(',',$getImage['image']);
foreach($image_ids as $image_id) {
$images = wp_get_attachment_image_src($image_id, 'full');
$image = $images[0];
$thumb_id = get_post_thumbnail_id(get_the_ID());
$alt = get_post_meta($thumb_id, '_wp_attachment_image_alt', true);
}
}
I've used wp_get_attachment_image_src to get the attached image alt text, but it doesn't seem to work. And yes, the image does have alt text in WordPress > Media > Alternative Text.
The markup is as follows:
<img src="<?php echo $image; ?>" alt="<?php echo $alt; ?>" />
$image appears fine, $alt shows up empty.
Try with alternative way :
$thumb_id = get_post_meta( get_the_ID(), '_thumbnail_id', true );

Wordpress get the attachment link for an image

The images that I'm querying from my wordpress media gallery are appearing correctly. But, I also want them to link to my custom attachment page and I can't get it to work
<?php
$query_images_args = array(
'post_type' => 'attachment', 'post_mime_type' =>'image', 'post_status' => 'inherit', 'posts_per_page' => -1,
);
$query_images = new WP_Query( $query_images_args );
$images = array();
foreach ( $query_images->posts as $image) {
$images[]= wp_get_attachment_url( $image->ID );
}
foreach ($images as $img) {
$url = get_attachment_link($img->ID);
echo '' . '<img src="' . $img . '" alt="" />' . '';
}
?>
Skip the second foreach loop. You have everything you need in the first one:
$query_images = new WP_Query( $query_images_args );
$images = array();
foreach ( $query_images->posts as $image) {
$images[]= wp_get_attachment_url( $image->ID );
$url = get_attachment_link($image->ID);
echo '' . '<img src="' . wp_get_attachment_url( $image->ID ) . '" alt="" />' . '';
}
wp_get_attachment_url() returns the URI of the image, see http://codex.wordpress.org/Function_Reference/wp_get_attachment_url; you were trying to get the ID from this non-object.

Get image caption in Wordpress

I have this code to retrieve the image url for the images in a gallery and it works ok but i cannot figure out how i could get the caption for each image.
I tried searching all over but i cannot seem to put all the information out there together! Any suggestions on how i could retrieve the captions?
function show_related_gallery_image_urls( $content ) {
global $post;
// Only do this on singular items
if( ! is_singular() )
return $content;
// Make sure the post has a gallery in it
if( ! has_shortcode( $post->post_content, 'gallery' ) )
return $content;
// Retrieve all galleries of this post
$galleries = get_post_galleries_images( $post );
$image_list = <<<END
<div class="side_bar">
<div class="related">
<h3>Related Images</h3>
END;
// Loop through all galleries found
foreach( $galleries as $gallery ) {
// Loop through each image in each gallery
foreach( $gallery as $image ) {
$src = $image;
$image_list .= '<a href="' . $src . '" rel="' . get_the_title() . '">'
. '<img src="' . $src .'" />'
. '</a>';
}
}
$image_list .= '</div></div>';
// Append our image list to the content of our post
$content .= $image_list;
return $content;
}
add_filter( 'the_content', 'show_related_gallery_image_urls' );
I hope i explained myself well! Thanks!
This hasn't been tested by try it, I cleaned up some of your code a bit:
1) Combined the first 2 IF statements into 1
2) Used get_post_gallery() (Codex) which returns the src and the image ID. We use the image ID to return the caption, we can also get the description and more if we needed to.
3) Removed the containing Foreach Statement since both my method only returns 1 gallery, not multiple so no need to loop through.
function show_related_gallery_image_urls( $content ) {
global $post;
// Only do this on singular items
if( ! is_singular() || !has_shortcode( $post->post_content, 'gallery' ) )
return $content;
// Retrieve all galleries of this post
$galleries = get_post_gallery( $post, false );
$image_list = <<<END
<div class="side_bar">
<div class="related">
<h3>Related Images</h3>
END;
// Loop through each image in each gallery
$i = 0; // Iterator
foreach( $gallery['src'] as $src ) {
$caption = wp_get_attachment($gallery['id'][$i])['caption'];
$image_list .= '<a href="' . $src . '" rel="' . get_the_title() . '">'
. '<img src="' . $src .'" />'
. '<div class="caption">'
. $caption
. '</div>'
. '</a>';
$i++; // Incremenet Interator
}
$image_list .= '</div></div>';
// Append our image list to the content of our post
$content .= $image_list;
return $content;
}
add_filter( 'the_content', 'show_related_gallery_image_urls' );
function wp_get_attachment( $attachment_id ) {
$attachment = get_post( $attachment_id );
return array(
'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ),
'caption' => $attachment->post_excerpt,
'description' => $attachment->post_content,
'href' => get_permalink( $attachment->ID ),
'src' => $attachment->guid,
'title' => $attachment->post_title
);
}
On a sidenote, there is a gallery filter function where you can change how the gallery is displayed post_gallery Filter, here's a question that kind of shows how to edit it. There's also a great WordPress Stack Exchange where that may be helpful in the future!

Add attribute to wp_get_attachment_image

I'm trying to add an attribute to the result of wp_get_attachment_image.
I want to use jquery lazyload to handle loading of my post thumbnails and to do that I need to add a data-original= attribute to the <img> tag wp_get_attachment_image is creating.
I've tried:
$imgsrc = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), "full" );
$imgsrc = $imgsrc[0];
$placeholderimg = wp_get_attachment_image( 2897, "full", array('data-original'=>$imgsrc) );
But it doesn't add the data attribute as I expected.
<img class="attachment-full" width="759" height="278" alt="..." src="..."></img>
Looking at the wp_get_attachment_image function it would seem that this ought to work though:
function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = false, $attr = '') {
$html = '';
$image = wp_get_attachment_image_src($attachment_id, $size, $icon);
if ( $image ) {
list($src, $width, $height) = $image;
$hwstring = image_hwstring($width, $height);
if ( is_array($size) )
$size = join('x', $size);
$attachment =& get_post($attachment_id);
$default_attr = array(
'src' => $src,
'class' => "attachment-$size",
'alt' => trim(strip_tags( get_post_meta($attachment_id, '_wp_attachment_image_alt', true) )), // Use Alt field first
'title' => trim(strip_tags( $attachment->post_title )),
);
if ( empty($default_attr['alt']) )
$default_attr['alt'] = trim(strip_tags( $attachment->post_excerpt )); // If not, Use the Caption
if ( empty($default_attr['alt']) )
$default_attr['alt'] = trim(strip_tags( $attachment->post_title )); // Finally, use the title
$attr = wp_parse_args($attr, $default_attr);
$attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment );
$attr = array_map( 'esc_attr', $attr );
$html = rtrim("<img $hwstring");
foreach ( $attr as $name => $value ) {
$html .= " $name=" . '"' . $value . '"';
}
$html .= ' />';
}
return $html;
}
Where am I going wrong?
[update] Sometimes it just takes a fresh pair of eyes to spot the idiocy... Thanks to hobo I realised I simply missed out a parameter in my function call :D :P
I haven't tested, but I think the problem is your array should be the fourth argument to wp_get_attachment_image, not the third.
So
$placeholderimg = wp_get_attachment_image( 2897, "full", array('data-original'=>$imgsrc) );
should be
$placeholderimg = wp_get_attachment_image( 2897, "full", false, array('data-original'=>$imgsrc) );
assuming you're happy with the default value (false) of the $icon argument.
I personally solved this problem doing a string replacement:
$imgsrc = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), "full" );
$imgsrc = $imgsrc[0];
$placeholderimg = wp_get_attachment_image( 2897, "full" );
$placeholderimg = str_replace( "<img ", "<img data-original='$imgsrc'", $placeholderimg );
It's not an elegant solution but it can work in some contexts (for example, you may have the attributes saved in a string and not as an array as in your case).

Store results of a php foreach loop as an array

I am using a foreach loop to generate a set of thumbnail links. I am using Wordpress and for one reason or another the place my PHP is executing is not the place I would like to render the list. So my question is: can I replace the echo statement with something that will store all of the generated html (for each image, not just the last one) and allow me to generate it further down the same page?
Thanks for any help. Here's my php so far:
foreach ($gallery_images as $galleryID) {
$attachment = get_post( $galleryID );
$thumb_img = wp_get_attachment_image_src( $galleryID, 'thumbnail' ); //thumbnail src
$full_img = wp_get_attachment_image_src( $galleryID, 'full' ); //full img src
echo '<img src="' . $thumb_img[0] .'">';
$gallery_images_count++;
}//end forEach
You can store the results to an array so that you can "echo" the results later:
$links = array();
foreach ($gallery_images as $galleryID) {
$attachment = get_post( $galleryID );
$thumb_img = wp_get_attachment_image_src( $galleryID, 'thumbnail' ); //thumbnail src
$full_img = wp_get_attachment_image_src( $galleryID, 'full' ); //full img src
$links[] = '<img src="' . $thumb_img[0] .'">';
$gallery_images_count++;
}
And then later in your code, you can print it out:
echo implode("\n", $links);
$arr = array();
foreach(...) {
$arr[] = '<a href=........';
}

Categories