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

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"

Related

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

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/

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

Wordpress save metabox checkboxes selection

Having an issue whereby the checkboxes inside a metabox aren't saving. I can save one value and have it return the value of checked to the checkboxes, just not multiple values, which I need. I've tried adding a foreach loop to the update_post_meta code but it didn't work. Slightly confused as to where I'm going wrong.
checkbox code is:
$areas = $wpdb->get_results("SELECT * FROM locations ORDER BY locationName ASC");
if( count($areas) ) :
?>
<div id="locationAssignedBoxes" size="1">
<?php
foreach($areas as $area) :
?>
<input type="checkbox" id="locationAssigned" name="locationAssigned" value="<?php echo $area->id; ?>"<?php if(get_post_meta($post->ID, 'locationAssigned', true) == $area->id) { ?> checked="checked"<?php } ?> /> <?php echo $area->locationName; ?><br>
<?php
endforeach;
?>
</div>
<?php
endif;
?>
Update_post_meta code is:
update_post_meta($post->ID, 'locationAssigned', $_POST['locationAssigned']);
Thanks very much!
This isn't a cut-and-paste answer to your question, but it should help. (I'm trying to solve a similar problem.)
Your problem in general: update_post_meta() saves a single meta value, not a collection of values. Because you want to save the values of multiple checkboxes, you have two choices:
Call update_post_meta() once for each of the checkboxes, creating a
collection of meta values associated with the post.
Combine all of the checkbox values into a single string and save those as a single value with one update_post_meta() call.
These two earlier questions are related and might point you in the right direction.
Save checkboxes of a metabox with a foreach loop (invalid argument)
Listing Pages With Checkboxes In a Metabox (and saving them)

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>

Adding a static text on top of view when it is not filtered

I want to add a static text (some sort of explanation/welcome text) on the very top of a view (over the filter) in Drupal 6.x with Views "2". I want this text appear only when the view is not filtered (i.e. on initial load of the page).
My problem is that the only place I figure out to make it work partially is in the views-exposed-form--MYVIEW.tpl.php. The problem is that when I place the code in this template, I don't know if the view is filtered or not, so the text appear on every single page! I don't have access to this info in that template so the only place this is available ($rows or $empty variables for example) is in views-view--MYVIEW.tpl.php.
But there I got an another problem. The order in witch it seams the variables are output are not the same as the order in witch they appeared in the file. For example, the $exposed variable content is render always on top, then $admin_links, $header and so forth.
<?php if ($header): ?>
<div class="view-header">
<?php print $header; ?>
</div>
<?php endif; ?>
<?php if (!$rows): ?>
<h3>This static text appear AFTER $exposed !!!</h3>
<?php endif; ?>
<?php if ($exposed): ?>
<div class="view-filters">
<?php print $exposed; ?>
</div>
<?php endif; ?>
<?php if ($attachment_before): ?>
<div class="attachment attachment-before">
<?php print $attachment_before; ?>
</div>
<?php endif; ?>
So even if I place my static content before this code, the filter form always appear on top!
I found the reason why is doing this: the exposed filter form is rendered as a part of the content-top <div></div>, but not the result (and $header, $footer, etc).
So is this by design? Do I miss something? How can I get my static text on the very top of the content-top!?
Well, after some tweaking and lecture on preprocess function in the theming system, I found a solution to my problem. I share it with you and if you find a more elegante approach; let me know!
What I did is this...
1) In the template.php file of the theme, I add two fonctions:
function YOURTHEME_preprocess_views_view__MYVIEW(&$variables) {
if ($variables['rows'] || $variables['empty']) {
$GLOBALS['dont_show_static_text'] = TRUE;
}
}
function YOURTHEME_preprocess_views_exposed_form__MYVIEW(&$variables) {
if ($GLOBALS['dont_show_static_text']) {
$variables['custom_flag1'] = TRUE;
}
}
So, if my views is showing some results ($row) or a blank result ($empty), then I set a flag to be use in the template file....
2) In my views-exposed-form--MYVIEW.tpl.php
...
<?php if (!$custom_flag1): ?>
<h2>Some static text here</h2>
<?php endif; ?>
...
And voilĂ ! My static text is showing up only on the initial load of the view and it shows on TOP of the filter (not under!).
Hope this help someone else!

Resources