Advanced Custom Fields checkbox display value - php

I'm struggling with ACF checkboxes, I'm trying to check the array to see if 'car' has been ticked:
if( in_array( 'car', the_sub_field('tyres_available') ) )
{
echo 'some html';
}
'car' is one of the checkbox options. If it's ticked I want to echo out some html. At the moment it's outputting the whole array for every checkbox that has been ticked in the field 'tyres_available'.
Any ideas where am I going wrong?
Thanks

Sorted. It's get_sub_field, not the_sub_field:
<?php $tyreServices = get_sub_field('tyres_available'); ?>
<?php if(in_array("car", $tyreServices )){ ?>
Some html
<?php } ?>

Related

PHP - Display text if checkbox selected

I am using the user registration plugin (https://wpeverest.com/wordpress-plugins/user-registration/). When a user signs up, there is an option to ask them if they're 'new team' or not. If they check 'new team' I then want to display text when they are signed in.
I have tried the following:-
<?php
$user = wp_get_current_user();
if ( in_array( 'msp_team', (array) $user->user_registration_msp_team ) ) {
echo ('TESTTTT');
}
?>
But the text is not displaying. Could someone please advise what I am doing wrong here?
Thank you!
Managed to sort this with the following code:-
<?php if ( get_field( 'user_registration_msp_team', 'user_' . $current_user->ID ) ): ?>
FIELD CODE....
<?php else: ?>
OTHER CONTENT IF FIELD IS EMPTY
<?php endif; ?>

ACF - get all fields of a page or post (including sub fields)

Im using ACF pro and for a search loop I need to get all fields of a page or post including subfields (repeaterfield) into an array for processing, so i can extract data like "name" and "value" from the array-objects.
I was trying "get_field_objects(mypageid);" which seems neat, but it only gets the first level fields. I need to get the subfields too.
I was looking around and right now it seems there isnt anything out of the box for it with ACF, but what would be the best way to get this data? Especially since every subfield could have subfields themself. I fail to wrap my head around a loop function for that. Has anyone ever written a loop for exactly that?
Any help would be much appreciated!
Thanks in advance,
ANB_Seth
How to Get ACF Repeater Field?
add below code in your page or post
Example :
main ACF filed name :homepage_slider
Sub Field name : slider_title, slider_description
<?php
if( have_rows('homepage_slider') ):
// loop through the rows of data
while ( have_rows('homepage_slider') ) : the_row();
?>
<h3><?php the_sub_field('slider_title'); ?></h3>
<p><?php the_sub_field('slider_description'); ?></p>
<?php
endwhile;
else :
// no rows found
endif;
?>
Okay, so I have worked myself to this functons which seem to do the trick:
function abGetAllFields($pro_num){
$tmpArr = get_field_objects($pro_num);
$fillArray = array();
foreach( $tmpArr as $tmpFieldObject ) {
$fillArray = abGetAllFieldsCycle($fillArray, $tmpFieldObject["name"], $tmpFieldObject["value"]);
}
return $fillArray;
}
function abGetAllFieldsCycle($fillArray, $name, $value) {
if (is_array($value)) {
foreach( $value as $key => $value ) {
$fillArray = abGetAllFieldsCycle($fillArray, $key, $value);
}
} else {
array_push($fillArray, [$name, $value]);
}
return $fillArray;
}
$myFieldArray = abGetAllFields($pageId);
There might be a better or easier way to do it, but heck, it did what I needed so I'll stop right there and thought I'd share my result :-)

Sort values from get_field_objects() - ACF

I have several custom fields in every custom post type called "product", I grab those fields with get_field_objects() this way:
$fields = get_field_objects();
if ( !empty($fields) ){
echo "<dl class='clearfix'>";
foreach ( $fields as $field ){
if (empty($field['value'])) continue;
echo "<dt>".$field['label']."</dt><dd>".$field['value']."</dd>";
echo '<br class="clear">';
}
echo "</dl>";
}
My client wants to be able to sort the the fields by the menu order (so he can just drag and drop in the plugin area).
But I didn't see any ordering documentations here- http://www.advancedcustomfields.com/resources/get_field_objects/
Right know the order is- the field that create last is the last one.
Is there any good solution for this?
What you could do is to make use of the repeater type of acf.
That way, you client will be able to add as many fields as he require, and sort them like he want by drag and dropping them.
If you need to know the "type" of each field (to change the style for example), you could add a select field (in your repeater field) with all your types of field.
You would then have something like this :
And something like this to display the fields :
<?php $fields = get_field('fields'); ?>
<?php if ( !empty($fields) ) : ?>
<dl class='clearfix'>
<?php foreach ( $fields as $field ) : ?>
<dt><?= $field['label']; ?></dt><dd><?= $field['value']; ?></dd>
<br class="clear">
<?php endforeach; ?>
</dl>
<?php endif; ?>

Unable to print post meta value custom post type

I have a plugin I want to customize. I could add first hand a custom field and was able to add it to post header (added a meta tag).
Now I want to print another custom field along with other data that initially existed in the plugin but I don't succeed in it.
I'm sure the custom field is added to postmeta wordpress table with the name I gave; I could get it and print it but I cannot do it where I want to.
Code excerpt is here :
To add the custom field
array( 'id' => 'price', 'name' => 'Price:', 'type' => 'text' ),
So price is the name of the custom field, and this is a line in the array of custom fields of course. Same line for credit field.
To get the value from postmeta table :
$v_credit = get_post_meta(get_the_ID(),'credit', true );
$v_price = get_post_meta(get_the_ID(),'price', true );
Credit was there, I added price.
To print the value :
<?php if($v_credit){ ?>
<?php _e('CREDIT:','mytheme'); ?>
<div class="course-info"><?php echo $v_credit; ?></div>
<?php }?>
<?php if($v_price){ ?>
<?php _e('Price:','mytheme');?>
<div class="course-info"><?php echo $v_price; ?></div>
<?php }?>
So credit is showing but not price.
I even tried to print static text but with no success.
if you are sure that the key is correct based on the database then your code should work fine unless you added it outside the loop
get a single post ID with the price value then try this,
$x = get_post_meta( 21,'price', true );
echo '<pre>',print_r( $x , 1 ), '</pre>';
if it doesn't throw anything then your key is wrong, if it throw something then your above code is outside the loop thus get_the_ID() returns nothings

Advanced Custom Fields checkbox inside repeater

I'm trying to use checkbox inside a repeater field to show different images. If user checks "chicken" and "roast" it must display two images to front-end. If nothing is checked, nothing is displayed.
I have tried the Advanced Custom Fields documentation and all I can get is NULL or exactly these names "chicken" and "roast" to front-end. This is my piece of code that is inside repeater loop.
<?php if (get_field('selection') == 'chicken') { ?>
//some piece of code
<?php } else if (get_field('selection') == 'roast') { ?>
//some more code
<?php } ?>
A checkbox creates a list of check-able items. In other words, you need to check that your string is in an array(). For example:
if( in_array( 'chicken', get_field('field_name') ) ) {
//...
}
Read more about checkboxes in the ACF codex: http://www.advancedcustomfields.com/resources/checkbox/

Categories