Get image meta in Wordpdess - php

I'm trying to get the image meta of my thumbnails in WordPress, but it just keeps failing. I have no idea why is that. Reading the documentation I can't really see what I'm doing wrong. See (https://codex.wordpress.org/Function_Reference/wp_read_image_metadata). How would you do this?
This is my code basically:
$url = get_the_post_thumbnail_url();
var_dump(wp_read_image_metadata($url));
All this does is to return bool(false)

Check the code below and let me know if it works for you.
<?php
$feat_image = wp_get_attachment_url( get_post_thumbnail_id( $post->ID ) );
echo $feat_image;
?>

Related

How to echo out a post url within a src attribute as background image in WordPress?

I'm trying to create a conditional statement that checks to see if a post has a thumbnail, and if so, echos out a div that has the post thumbnail as the background image.
<?php if( the_post_thumbnail() ) {
echo '
<div class="post-hero" style="background-image: url(' . wp_get_attachment_url( get_post_thumbnail_id( $post->ID ) ); ');">';
'</div>';
};?>
The problem is that my editor (vsCode) is throwing an error: an unexpected echo. I'm thinking it has to do with the second echo within the url.
I've tried changing echo to print, but that didn't work as well.
I'm stuck, and I'm new to PHP programming. Anything glaring in my code?
There is a error in your echo string and the_post_thumbnail() needs to be replaced by has_post_thumbnail(). See this fixed version:
if(has_post_thumbnail() )
echo '<div class="post-hero" style="background-image: url(\'' . wp_get_attachment_url( get_post_thumbnail_id($post->ID)). '\');"></div>';
}
There were two errors in my code, one answered by #Amacado, and I changed
if( the_post_thumbnail() )
to
if( has_post_thumbnail() )
It worked then. Thanks!

WordPress Advanced Custom Field gallery doesn't return an array

I have ACF Plugin installed and I have a gallery filed in my post. I've tried all these docs but still getting the error :
Invalid argument supplied for `foreach()`
this happens because the input of the for each is not an array!
Do you have any clue what's wrong with this?
Do you think if I have to set something while I've defined the custom field?
<?php
$images = get_field('mygall');
$size = 'full'; // (thumbnail, medium, large, full or custom size)
if( $images ): ?>
<ul>
<?php foreach( $images as $image ): ?>
<li>
<?php echo wp_get_attachment_image( $image['ID'], $size ); ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
I think your problem comes from the fact that you are using get_field() instead get_fields(). That's way you don't get an array.
If it still doesn't work check the documentation for get_fields() here. Try to debug it like using only get_fields() and see what is the output. If it is an empty array then it means that you are calling the function out of the loop and it can't get the post id. So do a second test with manually setting the post id like get_fields(123); and check the results. If there are no results then there is something wrong with that post. And if there are results then you can do a final test with checking what will be the result of get_fields(123, 'gallery').
All the above debugging can be wrapped in something like:
echo '<pre>';
print_r( get_fields(123) );
echo '</pre>';
Basically this will give you some idea of what is the structure of the data that you get from this function and how you can manipulate it so to get what you need.

Put the output of one PHP function into another in Wordpress

I am using a plugin that outputs data with a simple tag reference
<?php echo $property['building_brochure']; ?>
This outputs the reference number for an uploaded pdf which in this example is 352.
I want to return the actual link for this, the following works
<?php echo wp_get_attachment_url( 352 ); ?>
However, I want the combine these so on the single page template for the entry the relevant PDF is called i.e.
<?php echo wp_get_attachment_url( 'building_brochure' ); ?>
Is there a straightforward way of doing this?
You need to place the function as input into the other function the way Spectre-d has shared in comments.
i.e
<?php echo wp_get_attachment_url( $property['building_brochure'] ); ?>
However if you face issues,like not correct attachment_URL, then it could be Type issue i.e. first function might be outputting string and not integer as expected by attachment_url function in that case I recommend:
<?php
$pdf_id = (int)$property['building_brochure'];
echo wp_get_attachment_url( $pdf_id );
?>

How to get url of uploaded file in Pods Framework 2?

I havve created a pod called teams and within this pod I created a field for uploading imagethat is called team_flag
I can echo the title of that row in a while loop, but not the file(image) link.
E.g. this code for title output is working nice:
<?php echo $teams->field( 'name' ); ?>
However, echoing link to the file is not:
<?php echo $teams->field( 'team_flag' ); ?>
Any advice how to fix it?
The .guid is not a reliable property to use. If you migrate your site to another domain, all your URLs will break.
You should use
wp_get_attachment_url( $teams->field( 'team_flag.ID' ) )
instead.
To clarify, #rclai89 is correct, it is better to use:
wp_get_attachment_url( $teams->field( 'team_flag.ID' ) )
For some new comers this might be a bit confusing.
What that line will do, is get the URL of the ID given from that field.
if you did a error log:
error_log(print_r($teams->field( 'team_flag.ID' ),1 ));
You can see why using .guid would give you the correct "url".
but it is best to let WP do the hard work in this case:
<?php echo wp_get_attachment_url( $teams->field( 'team_flag.ID' ) ); ?>
Will in fact be more efficient.
SOLUTION is to add guid at the end:** <?php echo $teams->field( 'team_flag.guid' ); ?>

Multiple loop working, function inside isn't

I'm using this piece of code I found (http://impnerd.com/wordpress-hack-add-post-images-to-your-homepage) to display the first image uploaded to a post on the homepage next to the excerpts of posts. I'm doing this outside the main loop on the homepage, and have been having problems. When I do rewind_posts() to get the same loop results, it works fine, but when I try to create a different loop, this code snippet breaks down:
$images =& get_children( 'post_type=attachment&post_mime_type=image&post_parent=' . $post->ID );
if ($images)
{
$keys = array_keys($images);
$num = $keys[0];
$firstImageSrc = wp_get_attachment_thumb_url($num);
echo "<li><img src=\"{$firstImageSrc}\" width=\"288\" height=\"216\" alt=\"\" title=\"\" /></li>";
}
I have tried the methods in The_Loop#Multiple_Loops_in_Action in the docs, and they work, meaning I can get normal output after the loop, but my snippet above doesn't work. Any idea if there is a conflicting method call or something that's going on that's stopping it from working? Would appreciate some help, thanks!
To be more specific:
<?php $my_query = new WP_Query('category_name=Daily Photo&showposts=1');
while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID; ?>
<p>a</p>
<?php $images =& get_children( 'post_type=attachment&post_mime_type=image&post_parent=' . $post->ID );
if ($images) {
$keys = array_keys($images);
$num = $keys[0];
$firstImageSrc = wp_get_attachment_thumb_url($num);
echo "<li><img src=\"{$firstImageSrc}\" width=\"288\" height=\"216\" alt=\"\" title=\"\" /></li>";} ?>
<?php endwhile; ?>
will output <p>a</p>, but not the <li><img /></li> code I need in the snippet. Whereas if I use rewind_posts();, everything works, and I get the <li><img /></li> code, but I don't want to use the same loop that I had been using previously. I'm using this to display a daily photo in the sidebar, that pulls from the "Daily Photo" category. I will exclude Daily Photos from the main loop, and only want to use them to draw images from in that snippet.
Try removing the if statement for starters. Remove any references to images and see if it outputs your html without the image source. If that's the case then images isn't getting properly assigned and it's never executing the code.
$images =& get_children( 'post_type=attachment&post_mime_type=image&post_parent=' . $post->ID );
This code checks to see if there is an
image uploaded to the gallery.
Now you need to troubleshoot the above statement and see why you're not getting any images.
Also, it looks like there is a plugin to do just this if you don't want to continue messing with the code.
Another thing to keep in mind is that this code is pretty old and may not be compatible with your version of wordpress.

Categories