Wordpress: Pulling alt text from media item - php

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

Related

Adding links within ACF Image Caption

So I'm using ACF Image: https://www.advancedcustomfields.com/resources/image/, and outputting my image and caption as an Array.
I'd like to be able to add links within my captions e.g. '', but when I do this it literally outputs the code rather than turning the text into a link.
Is there a way to make it be a link rather than just outputting the HTML as text.
This is my code for my image:
<?php
$image = get_sub_field('image'); if( $image ):
// Image variables.
$url = $image['url'];
$title = $image['title'];
$alt = $image['alt'];
$caption = $image['caption'];
?>
<img class="img-fluid" src="<?php echo esc_url($url); ?>" alt="<?php echo esc_attr($alt); ?>" />
<?php if( $caption ): ?>
<p class="caption"><?php echo esc_html($caption); ?></p>
<?php endif; ?>
<?php endif; ?>
But when I add this to the caption box within Wordpress: This image is from Flickr.
It literally outputs the same as above rather than turning the word into a link.
As you can see above I had the code:
<?php echo esc_html($caption); ?>
When I removed the 'esc_html' part then I was able to add links within the caption area. So the code for the Caption was this instead:
<?php if( $caption ): ?>
<p class="caption"><?php echo ($caption); ?></p>
<?php endif; ?>

What is the best method to replace image in footer for WordPress?

What is the best method to replace the image in the footer for WordPress given the following code in the footer.php in WordPress? I would like to have a different image from the "main_logo". Keep in mind I have uploaded the image to the Media section of WordPress, and it has provided me with the URL for that image.
<?php if (!function_exists('dynamic_sidebar') ||
!dynamic_sidebar('Bottom 1')) : ?><?php endif; ?>
<?php
$logo = $data['main_logo'];
$disable_footer_logo = $data['disable_footer_logo'];
?>
<?php if($disable_footer_logo!="Yes") : ?>
<?php
if($logo!=""){
$img_url = $logo;
}else{
$img_url = get_template_directory_uri().'/images/logo2.png';
}
?>
<img src="<?php echo $img_url; ?>" alt="" class="img-left" />
<?php endif; ?>
replace $logo = $data['main_logo']; with $logo = get_template_directory_uri().'/images/your_image.png', Assuming your image is located in images folder.

WordPress get YouTube image thumbnail

I'm trying to let users post a video on one our sites with text beneath it. Is there a way display the YouTube thumbnail image as a user's post thumbnail.
Example:
I post a YouTube video on the site:
IMAGE LINK
Using HTML/PHP I pull in the posts on to my website using the below code:
<?php
$posts = get_posts('cat=3');
foreach ($posts as $post) : setup_postdata( $post );
?>
<?php the_date(); echo "<br />"; ?>
<?php the_title(); ?>
<?php endforeach; ?>
I want to load the image/video and then the title of the post. Not the date or any other content. How can this be done?
Then how can I make that thumbnail a link to go to the actual post?
The above answer got me to this part. I figured Id share my corrected code.
<?php
$posts = get_posts('cat=3');
foreach ($posts as $post) : setup_postdata( $post );
?>
<img src="http://img.youtube.com/vi/<?php echo get_post_meta($post->ID, 'video', true); ?>/0.jpg" alt="<?php the_title(); ?>" width="200" height="150">
<br />
<?php the_title(); ?>
<?php endforeach; ?>
You could grab the video id by using a custom field and place it inside the Video Thumbnail URL to get the video thumbnail.

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

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.

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