I can't debug whats wrong with my ACF get_field() to display an image array's url value - wordpress

So my code is pretty straightforward. I am just getting a field value from the global fields (Options). So this field is on Dashboard -> Options (I have ACFpro) and I am trying to echo out a logo which is an image array.
This is how my code looks like
<ul class="text-white p-0">
<a class="brand " href="/">
<?php
$image = get_field('logo');
echo '<p id="debug">'.($image ['url']).'</p>';
?>
</a>
</ul>
al i am trying to do is pull the url value and insert it to a <p> tag just to make sure it pulls the right value. but instead I am getting this error.
error when trying to echo $image
Any idea on what am I doing wrong?

It is one of two things. You need to add the ID or options to the get_field() as a parameter or you need to adjust the return format. By default it tries to pull the ID of the page get_field() is on.
I would change it to
get_field( 'logo', 'options' );
Or change options to whatever you called your options page.
https://www.advancedcustomfields.com/add-ons/options-page/

Related

I'm developing a WordPress theme and using custom fields. I'm facing the following problem

My code
<`li><img src="<?php echo get_post_meta($post->ID, '_for-gallery', true); ?> " alt=""></li>`
The code should give this:-
<img src="Array" alt="" draggable="false">
But the code is giving this:-
<img src=" " alt="" draggable="false">
Look at the code reference for get_post_meta() - it says that the function will return an array if the 3rd parameter ($single) is false:
Return: (mixed) An array if $single is false. The value of the meta
field if $single is true. False for an invalid $post_id.
In your code you're sending true, try this instead:
get_post_meta($post->ID, '_for-gallery', false);
More info here: https://developer.wordpress.org/reference/functions/get_post_meta/
When you use get_post_meta function with $single as true just get the first element, but false get all elements separated by comma.
Looking your code, i suppose that custom_field is an url, in that case, why you need get an array as a url? you must get a string...
You can check here: https://developer.wordpress.org/reference/functions/get_post_meta/

How to show multiple Google Maps with different locations created in Advance Custom Fields Wordpress plugin?

I have created two Maps fields in Advanced Custom Fields (ACF) Plugin in its field group area, please check the screenshot
But on the front end when I try to add the code as per Google Maps custom field code only one Map is showing not the second one https://www.advancedcustomfields.com/resources/google-map/
Ah yes, I'm able to figure this out. There has no need of any other different settings, just need to add one more field in ACF fields group area and show that field in the front end by doing the same settings as did on the first field but just change the location variable name and pass the second field value in it. Below is the complete code example to show the first and second map in different HTML elements.
First Map:
<?php
$location = get_field('map');
if( !empty($location) ):
?>
<div class="acf-map">
<div class="marker" data-lat="<?php echo $location['lat']; ?>" data-lng="<?php echo $location['lng']; ?>"></div>
</div>
<?php endif; ?>
Second Map:
<?php
$location_second = get_field('map_second');
if( !empty($location_second) ):
?>
<div class="acf-map">
<div class="marker" data-lat="<?php echo $location_second['lat']; ?>" data-lng="<?php echo $location_second['lng']; ?>"></div>
</div>
<?php endif; ?>
As I have created two map fields in ACF plugin, first map field's name is Map and Second map field name is Map Second, so these will be used as map and map_second in code as given above.
I hope you have ACF Pro version :)
then you can use repeater field
Create repeater field "locations" then inside only one field with name "location" and use code from example "Render multiple markers onto a map"

Wordpress ACF Plugin - wp_get_attachment_image function doesn't work with ACF Options Page?

I'm trying to get an image asset shown on the front-end using the wp_get_attachment_image WordPress function, but it doesn't seem to work with my options page on ACF.
Here is my code:
<?php
$image = get_field('logo', 'option');
$size = 'full'; // (thumbnail, medium, large, full or custom size)
if( $image ) {
echo wp_get_attachment_image( $image, $size );
}
?>
Which looks like this on the back-end:
The picture you see above is an options page in ACF and when I try to query that image into the wp_get_attachment_image function, it doesn't work. However, when I run this code:
<img data-src="<?php the_field('logo', 'option'); ?>" alt="footer logo" class="lazyload" />
which is within an image tag, it works just fine.
I copied and pasted what was shows on the ACF docs located here (Basic Display ID), but it's not showing the image on the front-end.
Anything I'm missing guys?
Thanks a lot!
Return value in field should be Image ID. See Screenshot
What is the return value type you used? Image Array, Image URL, Image ID
And you need to get a field like this:
get_field('logo'); why do you add option?
More info here:
https://www.advancedcustomfields.com/resources/image/
wp_get_attachment_image requires the first parameter to be an image ID. In your case, if you are seeing the image using the code <img data-src="<?php the_field('logo', 'option'); ?>" alt="footer logo" class="lazyload" /> then get_field('logo', 'option') is returning the url of the image.. not the ID which is what you need if you are using wp_get_attachment_image.
What you need to do is change the Return Value of your logo field to Image ID.
Then you might have to re upload the image.
And also change this code <img data-src="<?php the_field('logo', 'option'); ?>" alt="footer logo" class="lazyload" /> to <img data-src="<?php echo wp_get_attachment_url(get_field('logo', 'option')); ?>" alt="footer logo" class="lazyload" />
ACf plugin returns value as you have set the return type.
If you have sent the return type as Image array then :
$image[0] -> full or $image[0][$full] or $image[$full] depending on the number of image uploaded.
If you have set return type as Image url:
<img src="<?php $image; ?>"> would do the work.
If you are setting return type as Image Id:
$img2 = wp_get_attachment_image_src(get_post_thumbnail_id($image),
$full); echo $img2[0];
I guess above methods will surely help you out.
Thanks

In Wordpress get category ID within a blog roll

I'm not a fan of the default blog roll for a theme that I'm using, so I'm deconstructing it, so far I was able to pull into the blog roll template:
Author: get_the_author_link()
Date with formatting: the_time('F jS, Y');
What I'm having trouble with is pulling in the categor(eis) for each post. I've tried:
get_the_category(); // with or witout the get_the_ID()
But All I get back the array object. I then tried traversing to no avail. What am I doing wrong? Again, this is not an individual post, but a blog roll.
You're looking for get_the_category_list().
This function takes three parameters - separator, parents and post id (in that order).
So, you'll want to do something like this:
<?php echo get_the_category_list(', ', '', get_the_ID()); ?>
This will create a an unordered (ul) list with commas in between the category names. You will most likely need to use some CSS to make the li items float or set them to inline block. Something like this:
<ul>
<li>Cat 1, </li>
<li>Cat 2, </li>
<li>Cat 3</li>
<ul>
You can also use get_the_term_list(). This has a little more flexibility in the final output of the category terms.
get_the_term_list() accepts five parameters: post id, taxonomy, before, separator, after.
For your use you would do this:
<?php echo get_the_term_list( get_the_ID(), 'category', '<span>', ', ', '</span>' ); ?>
This will output something like this:
<span>Cat 1, Cat 2, Cat 3</span>
You can change the wrapper (<span>), I just used that as an example.
Here is documentation on both functions from the Codex:
https://codex.wordpress.org/Function_Reference/get_the_category_list
https://codex.wordpress.org/Function_Reference/get_the_term_list

Advanced Custom Fields/PHP issue

I apologize if this is answered elsewhere, but I'm having an issue with this ACF code here: http://goo.gl/9onrFN I want the client to be able to add a portfolio site link (if applicable) to the artist page and the link would say "View Artist's Website" and the link would take the user to the artist's site in a new window. How would I go about not making this text not visible, unless there was a url entered into the custom field on the post? Here's the code:
<p><?php the_field('contact_phone_number'); ?><br />
or <?php the_field('contact_email'); ?><br />
View Artist's Website</p>
Thanks in advance!
You can check if a ACF field is set with:
if(get_field('artist_website')) {
the_field('artist_website');
}
Using the_field will simple echo the contents of your field, whereas get_field will return the value which is a lot more helpful. For example you could write the above code as:
Note: get_field simple returns the value of the field, if you want to check if a valid url has been entered you will have to use a regular expression.
Below is your code with an if statement performing an empty field check:
<p>
<?php the_field('contact_phone_number'); ?><br />
or <?php the_field('contact_email'); ?>
<?php if(get_field('artist_website')) { ?>
<br />View Artist's Website
You may find your code easier to read by pre-setting your variables and including HTML in the echo:
<p>
<?php
$contact_phone_number = get_field('contact_phone_number');
$contact_email = get_field('contact_email');
$artist_website = get_field('artist_website');
echo "{$contact_phone_number}<br />";
echo "or <a href='mailto:{$contact_email}'>{$contact_email}</a><br/ >;
if($artist_website) {
echo "View <a href='{$artist_website}' target='_blank'>Artist's website</a>";
}
?>
</p>

Resources