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

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/

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)

Get, modify, then print post title in wordpress

Is there a way native way in wordpress to make uppercase the first letter of the first word of the scentence / the_title of the post?
If not, how do I do it in php, while it is wrapped in a <a></a> tag?
Here is the full line of code.
<?php ucfirst(the_title());?>
As you see, I have tried ucfirst, but it does not work. The other thing that I tried is:
<?php
$temp_title = the_title();
$final_title = ucfirst($temp_title);
?>
<?php echo $final_title;?>
ucfirst() returns a string. You need to echo the value returned from ucfirst().
Furthermore, the WordPress function the_title() prints the title directly, rather than returning it as a string. Use get_the_title() instead.
<?php echo ucfirst(get_the_title());?>
I think this is your problem: http://core.trac.wordpress.org/browser/tags/3.8/src/wp-includes/post-template.php#L51. As you can see the_title() is using get_the_title() and then if($echo) it echoes it out. I would experiment and try get_the_title().
First off you had an ending parantheses that didn't have a matching parantheses in your echo $final_title. Second you should try to apply ucfirst to the echo of your title. I removed $finaltitle, because it no longer serves a purpose. I haven't tried the code, but it should work. Please note that ucfirst() only works if the first letter is in the alphabet.
<?php
$temp_title = the_title();
?>
<?php echo ucfirst($temp_title);?>

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