Advanced Custom Fields checkbox inside repeater - php

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/

Related

Show images in slider from acf repeater

I'm trying to show the ACF repeater image in the dynamic slider in oxygen via the PHP function from the specified page id.
ACF field: slajder
Subfield with img repeater: obraz_slajdera
Page id: 7219
I always get background-img unknown.
My code:
function get_slider() {
$image = get_field( 'img', 7219 )['sizes']['large'];
return $image;
}
Please help.
You may want to do some reading through the ACF developer documentation, your code is fragmented, and the get_field() function isn't being used properly. At any rate, I'll do my best to explain what you should be directing your solution towards. You'll first need to loop through your repeater fields, check to see if there's any "rows" in the repeater field, fetch your value, and then do whatever you need to do with your respective image.
So, technically your function should look something like this:
function fetchSliderImages() {
//Empty array for holding your slider images, assuming there's more than one
$sliderImages = array();
//Check to see if the slider even has rows
if( have_rows('slajder') ):
//Loop through all the rows of your slider
while( have_rows('slajder') ) : the_row();
//Assuming this is coming back as a URL (can be adjusted within ACF)
$imageRepeaterValue = get_sub_field('obraz_slajdera');
//Check to see that we actually got something back that isn't blank
if($imageRepeaterValue != '') {
array_push($sliderImages, $imageRepeaterValue);
}
endwhile;
endif;
return $sliderImages;
}
You can then use this function to return an array of URL's which are slider images.
Used this for reference: https://www.advancedcustomfields.com/resources/repeater/

How to run Wordpress Query by ACF field using Select on front-end

Basically what I'm trying to do is create filters on front end, so that users can filter all posts by specific ACF field.
There's a cattery website and the owner will need to add new litters, when new generations of kittens arrive. I've created option page for this new field group and created a repeater field with text, so that I can add new rows with names of the litters, I want to filter by later.
I've included basic loop for repeater field:
<select name="litter" id="litter">
<?php
if( have_rows('cat-litters', 'option') ):
while ( have_rows('cat-litters', 'option') ) : the_row(); ?>
<option value="<?php the_sub_field('new-litter', 'option'); ?>"><?php the_sub_field('new-litter', 'option'); ?></option>
<?php endwhile;
else :
endif;
?>
</select>
And it is working for now:
I will be adding a field to cats' posts named cat-litter, so that I can find specific posts with a litter "02" for example.
But now I'm stuck with using this select from front-end to run a query to display fitting posts.
I'm working in index.php of twentytwentyone child-theme btw.
It doesn't matter if it will need to reload page or not (however if the ajax way won't be complicated I would very much appreciate that) but I have to point out that it needs to work with pagination and I already have it set up with pre_get_posts to display 9 posts per page (3x3 grid).
Taken and modified from the Dynamic $_GET parameters section of this page per the ACF documentation: https://www.advancedcustomfields.com/resources/query-posts-custom-fields/
function my_pre_get_posts( $query ) {
// do not modify queries in the admin
if( is_admin() ) {
return $query;
}
// only modify queries for 'cat' post type
if( isset($query->query_vars['post_type']) && $query->query_vars['post_type'] == 'cat' ) {
// allow the url to alter the query
if( isset($_GET['cat-litter']) ) {
$query->set('meta_key', 'cat-litter');
$query->set('meta_value', $_GET['cat-litter']);
}
}
// return
return $query;
}
add_action('pre_get_posts', 'my_pre_get_posts');
Then on the front end for reloading the page with the 'cat-litter' from the dropdown could be done with jQuery:
<script>
$('select#litter').on('change', function() {
window.location.href = "www.website.com/cats?cat-litter=" + $(this).val();
});
</script>

output table in ACF that is in a repeater field

I'm doing some displaying of tables and my table field is in a repeater field since i want to display 4 tables after each other that is a part of each other.
I'm looking at the code and see that it returns an array of 4.
I have looked at this code: https://wordpress.org/plugins/advanced-custom-fields-table-field/#screenshots
But that only seem to work if the table is in a normal field and not inside a repeater field. I can't get any information out of it.
I have been trying to loop trough the repeater field and then run the code for the table, but that does not seem to work, all I get from the dump is NULL.
if( have_rows($table) ): // loop through the rows of data
while ( have_rows($table) ) : the_row();
var_dump($table['information_table'])
endwhile;
else :
// no rows found
endif;
Anyone that has any tips on how i can get the tables to show from a repeater field?
Thanks.
repeater tables
The plugin author provides this code example:
Not sure what you are missing. Try without the $table variable and use just the field name. Verify that you are using the correct sub_field name. Double check that you have the data saved in your database. It looks like you should be using sub_field('information_table') instead of $table['information_table']
// check if the repeater field has rows of data
if( have_rows('repeater_field_name') ):
// loop through the rows of data
while ( have_rows('repeater_field_name') ) : the_row();
// get a sub field value (table field)
$table = sub_field('sub_field_name');
// use the “Output Table HTML” code with $table
endwhile;
else :
// no rows found
endif;

Repeater Field empty in ACF

I'm fairly new in using ACF so I used the code that was displayed in their site to display what is inside of my repeater field.
However, when I try to show the repeater's content it is empty??
This is my code, this is still in trial mode just to see if it's working-which it isn't. My repeater field already has 2 rows but it's not showing up any of those and just displays the else:
// check if the repeater field has rows of data
if( have_rows('map_infogrp') ):
// loop through the rows of data
while ( have_rows('map_infogrp') ) : the_row();
// display a sub field value
the_sub_field('google_map');
the_sub_field('branch_name');
//$street = get_sub_field('street');
//$district = get_sub_field('district');
//$phonenum = get_sub_field('phone_number');
//$email = get_sub_field('email');
endwhile;
else:
echo 'why is this empty???';
endif;
You need to specify the page id that you have set the ACF Repeater, otherwise it will get from the current page ID
have_rows($field_name, $post_id); //syntax
So, update your loop inserting the page ID you've entered the repeater data:
if( have_rows('map_infogrp', 'page_id') ):
// loop through the rows of data
while ( have_rows('map_infogrp', 'page_id') ) : the_row();
...
If you have the fields filled in on the specific page it should be showing up. If not, double check the field name(not label) that you used. If you have more than one row make sure you're printing it out as an array too

Advanced Custom Fields checkbox display value

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 } ?>

Categories