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=........';
}
Related
newbie here. I'm using the following code to extract the secondary image source of my wordpress posts, but I think I could shorten it someway...
Here is the code I'm currently using inside img src=""
<?php $images = get_attached_media('image'); $featured_image_id = get_post_thumbnail_id(); if ( has_post_thumbnail() ) { unset($images[ $featured_image_id ] ); } $harukunt = wp_get_attachment_image_src( key($images),'large'); echo '' . $harukunt[0] . ''; ;?>
Maybe I could simplify this by defining some values in the header.php file, so then I can call the image in a shorter way in the posts?
<?php
$images = get_attached_media('image');
$featured_image_id = get_post_thumbnail_id();
if ( has_post_thumbnail()
)
{ unset($images[ $featured_image_id ] );
}
$harukunt = wp_get_attachment_image_src( key($images),'large');
echo '' . $harukunt[0] . '';
;
?>
And then call the image on posts by simply using img src="<?php harukunt("$post->ID"); ?>" or something. But it isnt working this way, due to my poor knownledge.
Can anybody please help me?
You can create a function and add it to your functions.php file like this:
function get_secondary_img($post_id, $print = true ){
if(has_post_thumbnail($post_id)) {
$attachments = get_attached_media('image',$post_id);
if(count($attachments) < 2){
$attachment_id = get_post_thumbnail_id($post_id);
} else {
foreach($attachments as $key => $attachment){
if($key !== get_post_thumbnail_id($post_id)){
$attachment_id = $key;
break;
}
}
}
if($attachment_id){
$attachment = wp_get_attachment_image_src($attachment_id,'large');
$src = $attachment[0];
$width = $attachment[1];
$height = $attachment[2];
$alt = get_post_meta($attachment_id,'_wp_attachment_image_alt',true);
if($print == true){
echo '<img width="'.$width.'" height="'.$height.'" src="'.$src.'" alt="'.$alt.'" />';
} else {
return $src;
}
}
}
}
Then if you want to output the html for the image you can just do
<?php echo get_secondary_img( $post_id ); ?>
Or if you want to just return the src attribute, you can just do
$yourvariable = get_secondary_img( $post_id, false );
I am using CMB2 file_list for uploading photos e.g. photo gallery. On the home page I need to get only the first photo and not the entire gallery. Below is the function to get all the photo. How can i retrieve only the first photo in the list?
function cmb2_output_file_list( $file_list_meta_key, $img_size = 'medium' ) {
$files = get_post_meta( get_the_ID(), $file_list_meta_key, 1 );
echo '<div class="file-list-wrap">';
foreach ( (array) $files as $attachment_id => $attachment_url ) {
echo '<div class="file-list-image">';
echo wp_get_attachment_image( $attachment_id, $img_size );
echo '</div>';
}
echo '</div>';
}
cmb2_output_file_list( 'wiki_test_file_list', 'small' );
You'd use something like this, make sure to change the meta key to the correct one, this will get the ID and URL of first file in the list:
$file_list_meta_key = 'wiki_test_file_list';
$files = get_post_meta( get_the_ID(), $file_list_meta_key, 1 );
$first_id = key($files);
$first_url = reset($files);
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;
I am trying to get all the gallery images from a single post. Here in get_post_gallery() the variable $image returns the thumbnail URL. Can any one help me with retrieving full size image URL from the post.
$gallery = get_post_gallery(get_the_ID(), false )
foreach( $gallery['src'] as $image ) {
$image_list . = '<li>' . $image . '</li>';
}
This work for me:
$gallery = get_post_gallery( $post, false );
$ids = explode( ",", $gallery['ids'] );
foreach( $ids as $id ) {
$link = wp_get_attachment_url( $id );
$image_list . = '<li>' . $link . '</li>';
}
Thanks to Matt for this code, see the original post
You can try this
<?php wp_get_attachment_image( $attachment_id, $size, $icon, $attr ); ?>
where you give the attachment id of the gallery image in $attachment_id and
$size = (thumbnail, medium, large or full)
here basically you choose full as you want to display the full size image.
Let me know if this helped you . :)
I am using the following code to loop through all images in an array which works great, but I want to be able to just return the first image in the array. Can anyone point me in the right direction?
<?php
$books = get_post_meta( $post->ID, 'Images', true );
foreach( $books as $book){
$image_id = $book['single_image'];
echo '<img src="';
echo wp_get_attachment_url( $image_id );
echo '" />';
}
?>
If it's a numerical array, just get the values from index 0. Like:
$books = get_post_meta($post->ID, 'Images', true);
$image_id = $books[0]['single_image'];