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.
Related
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
Im using lightgallery so i need to load images before call them in lightgallery. Problem is that images are large so it takes too much time to load. Is there any way to load that specific gallery when user click on link.
<div id="lightgallery-<?php echo get_the_ID(); ?>" class="hidelightgallery">
<?php
foreach ($files as $image) {
$image_attributes = wp_get_attachment_url( $image->ID );
$attachment_title = get_the_title($image->ID);
$caption = get_post_field('post_excerpt', $image->ID);
?>
<a class="item" href="<?php echo $image_attributes ?>" data-sub-html="<?php echo $attachment_title; ?> <?php if($caption!= '') echo ' - ' ?> <?php echo $caption ?>"><img src="<?php echo $image_attributes ?>"></a>
<?php } ?>
</div>
Now what i want is if user click for example link with this id then do foreach. Is that possible?
Just follow these steps,
create new template / html page where you will write html and populate by foreach loop
add your id = lightgallery whole code to that html page
when you will click on your link (which you mentioned) fire an ajax
Ajax function will get some id or number of images need to show or your logic on how you will populate data in foreach loop
in php you will get all relevant data, and you will populate that data in html file you created in step 1
php function will return that data to ajax function
Ajax function will get all your dynamic html data
populate that html where ever you want or just append that html wherever you want
Go step by step, this will solve your problem.
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
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.
The explanation for this line is that Anchor doesn't display the image from the custom field correctly. So I had to add the missing part of the path. It works just fine, but now the problem is that I get horrendous icons on Safari when there is no image fetched in the field image:
<?php echo "<img src='http://www.firstpartoftheurl/" . article_custom_field('image') . "' alt=' ' height='300' >"; ?>
May I show this line only when the custom field is populated?
And how can I hide custom fields when they are empty?
This is how I solved it:
<?php
$image = article_custom_field('image');
if (!empty($image)) {
echo "<img src= 'http://www.firstpartoftheurl".article_custom_field('image')."' alt='blabla ".article_title()."'>"; //if there is image show it
} else {
//if not do nothing
} ?>
I hope it helps. It works for me but if someone has a better solution, please let us know.
Shorter still:
<?php if (article_custom_field('featured-img')) :?>
<img src="<?php echo article_custom_field('featured-img')?>" alt="<?php echo article_title(); ?>" />
<?php endif; ?>
If article_custom_field() is not returning the full URL, there might be something up with your server configuration, because this has always worked for me. Otherwise, just prepend $_SERVER['SERVER_NAME']... this is better than hard-coding the URL.
There is documentation on the function here which also explains how to use its fallback: http://anchorcms.com/docs/function-reference/articles