Wordpress gallery displays all the images attached to a post/page - php

Wordpress galleries have a strange way to work, in my opinion. I explain:
Wordpress gallery shows in the post ONLY the images that have been
uploaded in the current post. If you add in the gallery an image that
already was in Media library, it will not be showed!
After having create a gallery, if I remove an image from the gallery
it will be showed anyway
The gallery displays all the images attached to the post (also
featured thumbnail and embedded images) although these images were
not included in the gallery
I'd call all of this a bug.
The question: Is it possible to show in the post ONLY the images (both the uploaded ones and the ones that already was in Media libray) that are included in the gallery?
Notice: without using the shortcode [gallery exclude="..."] and without uploading the images directly from Media library?
P.S. To display the gallery in the post I'm using this script:
<ul>
<?php $images = get_children('post_type=attachment&post_mime_type=image&post_parent=' . $post->ID . '&orderby=menu_order&order=ASC');
foreach( $images as $img_id => $img_r ) :
$thumb = wp_get_attachment_image_src( $img_id, 'thumb', true );
$full = wp_get_attachment_image_src( $img_id, 'full', true ); ?>
<li>
<a href="<?php echo $full[0] ?>">
<img src="<?php echo $thumb[0] ?>" alt="<?php echo get_the_title( $img_id ) ?>" title="<?php echo get_the_title( $img_id ) ?>">
</a>
</li>
<?php endforeach; ?>
</ul>

That is not a bug, that's just how the gallery works. It will only display images attached to a post, if you want to add an already-uploaded image to the gallery then you'll need to attach the image to the post from the Media menu.
You can use gallery shortcode to display the images or alternatively you can use the codes on http://www.wpcodesnipps.com/display-images-attached-post/ to display the images.

Related

Return custom image size in ACF

I am working on a WordPress site that uses ACF (Advanced Custom Fields) to display a custom slider gallery. Problem is, I can't seem to get it to display custom image sizes within the slider.
In ACF the field type that is being used is content -> Gallery, I am then using the following code within my theme files:
<?php
$images = get_field( 'slider' );
$size = "archive-slider";
if( ! empty( $images ) ) :
?>
<ul class="rslides">
<?php
foreach( $images as $key => $value )
{
echo '<li><img src="' . $value['url'] . '" /></li>';
}
?>
</ul>
<?php endif; ?>
I have defined the custom image size in the functions file and regenerated the thumbnails but I can't get the size attribute to work.
Update
I updated my code to (this works):
<li>
<img src="<?php echo $image['sizes']['archive-slider']; ?>" alt="<?php echo $image['alt']; ?>" />
</li>
Per http://www.advancedcustomfields.com/resources/gallery/
You should be able to access the custom image size via $value['sizes']['custom-image-size']

Wordpress: Pulling alt text from media item

I'm working on designing an image blog but am running into a few issues. I was able to pull the "caption" area of the media item for the lightbox, but I'm unable to pull the "alt" area or the "description" area. Any ideas?
<?php if($attachment->post_excerpt): ?>alt="<?php echo $attachment->post_excerpt; ?>"<?php endif; ?>
(That is what I use to pull from the media items caption area)
<?php $image = wp_get_attachment_image_src($attachment->ID, 'full'); ?>
<li><p style="display:none;"><?php if($attachment->post_excerpt): ?><?php echo $attachment->post_excerpt; ?><?php endif; ?></p><img src="<?php echo $thumbnail[0]; ?>" alt="gallery-blog" /></li>
<?php endforeach; endif; ?>
(This is the extended code for the homepage thumbs)
I'm basically looking for something to replace the alt="gallery-blog" area so that it will display the media items alt text from the media page
you can get the media title,caption and description as like below
$alt = get_post_meta($attachment->ID, '_wp_attachment_image_alt', true);
$image_title = $attachment->post_title;
$caption = $attachment->post_excerpt;
$description = $image->post_content;
the wp_get_attachment_image(); function echos out all the information regarding the image including the alt, src, width, height and class
Codex

Display featured images on a different page wordpress site

I need to be able to display featured images (category 4 in my wordpress site) on a separate page just the images not the rest of the post.
I created a template named 'ban' and attached it to a page named banners in which to display the category 4 images.
I was able to list the categories but that is as far as I got:
<?php wp_list_categories('include=4') ?>
I'm not sure how to go about doing this, if someone could explain it to me it would be greatly appreciated!
If you want to get all featured images from the posts under category 4 then this might be the solution
$the_query = new WP_Query();
$the_query->query("cat=4&nopaging=true");
if ($the_query->have_posts()) :
while($the_query->have_posts()) : $the_query->the_post();
if(has_post_thumbnail()) :
the_post_thumbnail();
endif;
endwhile;
endif;
wp_reset_postdata();
If you want to get the real width/height of then images you can use this:
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );
?>
<img src='<?php echo $image[0]; ?>' />
<?php
Just replace the the_post_thumbnail() function. Hope that helps!

how to get url ONLY of featured image in wordpress

I'm using timthumb.php and need to be able to get the url of the featured image of a post, when I use functions like the_post_thumbnail it does more than just the url.
Here is the code so, the capital letters is where I need to insert the exact url, any help would be great, thanks.
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<img src="<?php bloginfo('template_url'); ?>/wp-content/themes/limerickfc/timthumb.php?src=<?php URL OF FEATURED IMAGE ?>&h=760&width=474" alt="" title="<?php the_title(); ?>" />
</a>
If your "thumbnail" images are large enough to do what you need:
<?php wp_get_attachment_url(get_post_thumbnail_id($post->ID)); ?>
Otherwise you'll need to go for something like:
<?php $src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), array(300, 300), false, ''); echo $src[0]; ?>
EDIT: The array(300, 300) bit is the image size you'd like to fetch. WP will substitute any image it has that is at least as large as what you asked for. You can also use 'thumbnail', 'medium', 'large' or 'full' to select one of the pre-configured sizes, as well as any name defined by add_image_size() in your template or plugins.
First get the id of the featured thumbnail image:
$thumb_id = get_post_meta( $post_id, '_thumbnail_id', true );
One way to do it:
$thumb = get_post($thumb_id);
$thumb_url = $thumb->guid;
Another way would be:
$thumb_url = wp_get_attachment_url( $thumb_id );
Be careful about what is stored in the GUID and what is returned by get_attachment_url. The attachment url (depending on configuration) can be the page where the attachment is visitable (not the raw file URL but the page where attachment.php is used).

Thumbnail or Preview in the Wordpress Loop

Spent a while on this now with little to no avail.
I'm trying to alter the loop so that it checks each article for a featured image and if it finds one it displays that instead of the truncated article and link.
So basically (in logic)
Check for featured image
if there is a featured image display the featured image (and nothing else).
else display a truncated version of the article.
Can anybody help?
Here you go;
<?php if (function_exists('has_post_thumbnail') && has_post_thumbnail()) { ?>
<?php $img_src = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), array( 960,960 )); ?>
<img src="<?php echo $img_src[0]; ?>" alt="My image" />
<?php } else { ?>
<?php the_excerpt(); ?>
<?php } ?>
Have a look at the following in the codex for more info;
Wordpress Featured images
Wordpress Excerpts

Categories