Wordpress ACF repeater field > radio button value and babel - wordpress

I have a repeater field in wich I have a radio field. I need to output the label and the value.
In ACF field, I putted value:label such as :
red: Red Carpet
green: Green leaf
I tried a piece of code :
$field = get_sub_field_object(‘field_name’);
$value = get_sub_field(‘field_name’);
$label = $field[‘choices’][ $value ];
I tried to replace the fieldname by the field_id, but it returns "Array" instead of the value.
I need to use the value in a class, and the label in a title. Can you help me ?

get_sub_field_object() has to be used within a has_sub_field() loop, such as this:
<?php while( has_sub_field('repeater_fields_name') ): ?>
<?php
// vars
$select = get_sub_field_object('radio_field_from_your_code');
$value = get_sub_field('radio_field_from_your_code');
?>
<ul>
<?php foreach( $select['choices'] as $k => $v ): ?>
<li>
<?php if( $k == $value ): ?>
<span class="selected">Selected!</span>
<?php endif; ?>
<?php echo $v; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endwhile; ?>
You're probably close to getting the right values. Just tweak things to follow this general pattern. More info on this function can be found at ACF's docs site: https://www.advancedcustomfields.com/resources/get_sub_field_object/

Related

Unable to get ACF repeater field value

I created an ACF repeater field on my options page but I'm unable to retrieve the value using get_field(). How do I retrieve the repeater value?
According to ACF - Get values from an options page, you need to add 'option' as the second parameter in get_field().
See this example:
$variable = get_field('field_name', 'option');
<?php if( have_rows('repeater', 'option') ): ?>
<ul>
<?php while( have_rows('repeater', 'option') ): the_row(); ?>
<li><?php the_sub_field('title'); ?></li>
<?php endwhile; ?>
</ul>
Replace repeater with the name of your field and inside the_sub_field() with what you have included.
To get ACF Repeater field you have to use 'echo get_sub_field();' or 'the_sub_field();'
For ACF option page you can use the following code:
<?php if( have_rows('repeater_field_name', 'option') ):
while ( have_rows('repeater_field_name', 'option') ) : the_row();
the_sub_field('yourfieldname');
endwhile;
endif; ?>

Exclude from list empty value

Hello i create a function to display on list all subcategory of master category of WooCommerce and not display the subcategory that are empty without any products.
I use this code but i continue to display on list also the subcategory empty. Where wrong?
<ul>
<?php foreach($term_children as $child) :
$term = get_term_by( 'id', $child, 'product_cat' );
?>
<li><?php echo $term->name; ?></li>
<?php // Skip empty terms
if( $child->count <= 0 ) {
continue;
} ?>
<?php endforeach; ?>
</ul>

Wordpress - List all the post's categories with custom code in the single page

First of all it's supposed to be used in the single page. After the single post I want to display the categories that the post belongs to.
The basic code for this is <?php the_category(' | '); ?> which outputs a simple link. Then we have <?php echo get_the_category_list(); ?> which outputs a more specific code (http://codex.wordpress.org/Function_Reference/get_the_category_list):
<ul class="post-categories">
<li>
Business
</li>
</ul>
However I need to decompose this code. For example, I want the <a> tag to be before the <li> tag. I've got a code that does what I want, but it's supposed to display all the categories available in my page, which is:
<?php
$categories = get_categories();
foreach($categories as $category)
{
?>
<li><?php echo $category->name ?> <span class="lower">(<?php echo $category->count ?>)</span></li>
<?php
}
?>
Any idea how I can make this work?
Thanks!
You can use get_the_category() its return you category objects that you can loop and do with them what you want.
$category = get_the_category( /* $post_id */ );
print_r($category);
foreach($category as $cat) {
?>
<?php echo $cat->name; ?>
<?php
}
<?php
$category = get_the_category($post_id);
foreach($category as $cat)
{
?>
<li><?php echo $cat->name ?></li>
<?php
}
?>

Skip custom Field if not exsist?

i have a code like this, to display a custom field on my Page...
<?php
global $wp_query;
$postid = $wp_query->post->ID;
if($immopress_property['assistedLiving']): ?>
<li class="assistedLiving">
<span class="attribute" style="width:200px;display:block;float:left;"><?php echo 'Seniorengerechtes Wohnen' ; ?><span class="wpp_colon">:</span></span>
<span class="value"><?php if (get_post_meta($post->ID, 'assistedLiving' , true)) echo "Ja"; else echo "Nein"; ?> </span>
</li>
<?php wp_reset_query(); ?>
<?php endif; ?>
But when the custom field NAME & VALUE not exist it shall skip to the next Value and Name. I try different ways to do that but no one works.
Hope someone can help me with that.
changed to this and it works...
if ( get_post_meta($post->ID, 'assistedLiving', true) ) { ?>

WordPress - How to do "current-page-item" classes in a list of custom taxonomy terms?

I have the following code that generates a list of terms in a taxonomy term, and then POSTS that are under each term.
I want to have a current-page-item class added to the current item, so that when you are on a page under the taxonomy term, its related item in the nav is styled. Here is my code:
<?php $terms = get_terms('benefit-cats');
echo "<ul>";
foreach ($terms as $term) {
$wpq = array ('taxonomy'=>'benefit-cats','term'=>$term->slug,'order'=>'asc','orderby'=>'title');
$query = new WP_Query ($wpq);
echo "<li class=".$term->slug."><span class=\"list-item\"><span class=\"text-arrow\">►</span> ".$term->name."</span>"; ////
echo "<ul class=\"children\">";
?>
<?php
if ($query->have_posts() ) : while ($query->have_posts() ) : $query->the_post(); ?>
<li><span class="text-arrow">►</span> <?php the_title();?></li>
<?php endwhile; endif; wp_reset_query(); ?>
<?php
echo "</ul></li>";
}
echo "</ul>";
?>
You can try something like this...
Specify the current post_id before your loop, then condition to see if post loop contains your post_id.
// before loop
$page_id = $wp_query->get_queried_object_id();
// replace <li><span class="text-arrow">►</span>
if($page_id ==$query->post->ID ) $class = " current-page-item";
<li><span class="text-arrow<?php echo $class; ?>">►</span>

Resources