Displaying wordpress gallery images src in Gutenberg - php

I'm using Gutenberg default post editor to edit my WordPress posts.
I have tried serval approaches to display the post's gallery, but none of them worked.
<?php
var_dump($_GET['id']);
echo '<br/>';
echo '<br/>';
$gallery = get_post_gallery( $_GET['id'] /* The post ID from $_GET */, false );
// Loop through all the image and output them one by one.
foreach ( $gallery['src'] AS $src ) {
?>
<img src="<?php echo $src; ?>" class="my-custom-class" alt="Gallery image" />
<?php
}
?>
Debugging notices:
string(3) "404"
Notice
: Trying to access array offset on value of type bool in
/home/mialuzco/domains/mia-luz.com/public_html/wp-content/themes/mia luz/page-gallery.php
on line
23
Warning
: Invalid argument supplied for foreach() in
/home/mialuzco/domains/mia-luz.com/public_html/wp-content/themes/mia luz/page-gallery.php
on line
23
Basing on this piece of knowledge I should have got it worked by now, but nothing really works..
How can I display post gallery images src?
EDIT
My post

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!

ACF Image Field, get single values from array, instead of array

I am using Advanced Custom Fields PRO Version 5.3.7. I am trying to load a picture that is within my custom post type.
I have configured the ACF field for the custom post type and it works well for text related fields.
However, I am trying to display an image. I am using an adapted example from the acf documentation. Below you can find my code.
<div class="image">
</div>
My problem ist that on my site, I get the Array back instead of the single value:
Any suggestions what is wrong with my code?
I appreciate your replies!
Dont use the_field('box_image')['url'] because the_field() echoes directly everything and echoing an array outputs array(); Instead of this, try this:
<?php $img = get_field('box_image'); ?>
<?php if( $img ): ?>
<a href="<?php the_field('link'); ?>" rel="nofollow" class="am" target="_blank" >
<img class="b-lazy wp-post-image b-loaded" alt="<?php esc_attr_e( $img['alt'] ); ?>" src="<?php echo esc_url( $img['url'] ); ?>" width="230">
</a>
<?php endif; ?>
Whn you setting a ACF, you can select options "Return value" (array, url, id) change this as id if you want get different sizes (thumbnails), and URL if you want get original image url.
if you use ID option to get image as html tag you can use wp_get_attachment_image($id, $size); i always prefer to store only image ID.

Illegal string offset for 'url' when using WordPress Advanced Custom Fields

I am converting a Bootstrap template into a WordPress theme.
For custom fields I am using the plugin Advanced Custom Fields. The issue is when I am trying to add an image with the help of Advanced Custom Fields, I get the following error:
Warning: Illegal string offset 'url' in C:\wamp64\www\my-site\wordpress\wp-content\themes\bootstraptowordpress\page-home.php on line 31
However when I am adding text through this plugin it shows no error.
I have no other plugins installed other than ACF.
Here is my code:
$home_page_logo = get_field('home_page_logo');
<div class="front_logo">
<?php if( !empty($home_page_logo)): ?>
<img src="<?php echo $home_page_logo['url']; ?>" alt="<?php echo $home_page_logo['alt']; ?>" />
<?php endif; ?>
</div>
There are three ways that an image field can be returned in ACF (array, URL, or ID). It sounds like your field is set to return the URL - which returns as a string.
Therefore, you need to access it like this:
<?php echo $home_page_logo; ?>
instead of this:
<?php echo $home_page_logo['url']; ?>
Alternatively, you can edit the set up for the field in your WordPress admin and configure it to return an image array instead of the URL:
If it's set to the array option, you can access the url like you are currently doing so, as well as access a range of other data pertinent to the image (such as its width and height, WordPress attachment ID, caption if entered, etc.)
It's better to use gravity forms plugin. You can to download here:
Rocket Genius Gravity Forms WordPress Plugin
But if want to fix your code, try to test if is array:
<?php
$yes = array('this', 'is', 'an array');
echo is_array($yes) ? 'Array' : 'not an Array';
echo "\n";
$no = 'this is a string';
echo is_array($no) ? 'Array' : 'not an Array';
?>
http://php.net/manual/function.is-array.php

how can I use the to_image_src in wordpress?

I am very new in wordpress and I noticed that at my DB im saving values as 20 on a custom tag, to print it they use something like this:
print_custom_field('my_logo:to_image_src')
And basically, it prints the image URL.
Reading this I noticed it says accepts the ID of an image and returs the URL
I have on my meta_key and meta_value fields the following
meta_key = my_logo
meta_value = 20
Still, not sure how to make it work?
I bring the meta_key and meta_value value with a query. So, basically, I dont know how can I come from there to get the path (as I have it on a previous template).
Thanks
Try adding the following code where you want the images to appear:
<?php $image = wp_get_attachment_image_src(get_post_meta( get_the_ID(), 'my_logo' ), 'full'); ?>
<img src="<?php echo $image[0]; ?>" alt="<?php echo get_the_title(get_post_meta( get_the_ID(), 'my_logo' )) ?>" />
This will get the full-sized image for you using the ID that is returned from the my_logo key.

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