I am using ACF and dont know how to manage to replace div class name if the custom_field number is
equal and less then 30 class="color1"
equal and less then 50 class="color2"
equal and less then 90 class="color3"
Can you please tell me how to do that?
if( $post_objects ):
foreach( $post_objects as $post_object):
echo the_field("casino_rating", $post_object->ID);
endforeach; endif;
Thanks
Atif
I'm assuming you want to get the casino rating and display a different class depending on what appears?
In this case you could use this code:
if( $post_objects ):
foreach( $post_objects as $post_object ):
$casino_rating = get_field("casino_rating", $post_object->ID);
// This part here will decide what class to get
if( $casino_rating < 30 ){
echo 'class="color1"';
} elseif( $casino_rating < 50 ){
echo 'class="color2"';
} elseif( $casino_rating < 90 ){
echo 'class="color3"';
}
endforeach;
endif;
You might want to be careful though and make sure this is only outputting class= if there's no other class attached to the element, otherwise there'll be HTML errors.
Slightly more compact version that should achieve the same thing
if( $post_objects ):
foreach( $post_objects as $post_object ):
$rating = get_field("casino_rating", $post_object->ID);
$color = ($rating >= 31) ? "color2" : "color1";
$color = ($rating >= 51 ) ? "color3" : $color;
echo 'class="'.$color.'"';
endforeach;
endif;
or if you are going to use this a lot make a function for it At the bottom of your page
function color($rating) {
$color = ($rating >= 31) ? "color2" : "color1";
$color = ($rating >= 51 ) ? "color3" : $color;
return $color;
}
Then you can do
if( $post_objects ):
foreach( $post_objects as $post_object ):
$rating = get_field("casino_rating", $post_object->ID);
echo 'class="'.color($rating).'"';
endforeach;
endif;
Related
I really need some help, My problem is that I cannot show the label from a group field using ACF
Script below is displaying the name and value, I need the "Label" to be displayed and its "value" and I can't find anything.
if( have_rows('product_specifications') ):
while( have_rows('product_specifications') ): the_row();
$subfields = get_field('product_specifications');
if( $subfields ) { ?>
<ul>
<?php
foreach ($subfields as $spec => $value) {
if ( !empty($value) ) { ?>
<li><?php echo $spec; ?> : <?php echo $value; ?></li>
<?php }
} ?>
</ul>
<?php }
endwhile;
endif;
Here is my current output:
lamp_type : E27
wattage : 1x 60W Max
globe_included : 1
colour_cord : Clear
when It should be:
Lamp Type : E27
Wattage : 1x 60W Max
Globe : 1
Colour Cord : Clear
Please anyone help me...
Use get_row() to get the sub fields:
$subfields = get_row();
And use get_sub_field_object() to get the sub field object:
$field = get_sub_field_object( $key );
So, try this: (no re-indentation so that you can easily compare with your code)
if( have_rows('product_specifications') ):
while( have_rows('product_specifications') ): the_row();
if( $subfields = get_row() ) { ?>
<ul>
<?php
foreach ($subfields as $key => $value) {
if ( !empty($value) ) { $field = get_sub_field_object( $key ); ?>
<li><?php echo $field['label']; ?> : <?php echo $value; ?></li>
<?php }
} ?>
</ul>
<?php }
endwhile;
endif;
What you are looking forward within the foreach loop is using the get_field_object() function.
Within here, you can get the label and value of any field.
For examples / uses of the get_field_object(), take a look at https://www.advancedcustomfields.com/resources/get_field_object/.
So, for example, you'll have:
$field = get_field_object($spec);
echo $field['label'] . ': ' . $field['value'];
Hope this helps.
Have been going around and around with this and can't make it work - probably something very simple but I've got no hair left - can anyone help me?
<?php
if( have_rows('page_content') ):
while ( have_rows('page_content') ) : the_row();
if( get_row_layout() == 'specs' ):
// check if the nested repeater field has rows of data
if( get_sub_field('objectives') ):
echo '<ul>';
$field = get_field_object('objectives');
$value = $field['value'];
$choices = $field['choices'];
// loop through the rows of data
foreach( $value as $v):
echo '<li>'.$choices.'</li>';
endforeach;
echo '</ul>';
endif;
endif;
endwhile;
endif;
?>
Thanks in advance.
Can you try with the Key of ACF field like this,
$field_key = "field_5039a99716d1d";
$field = get_field_object($field_key);
https://www.advancedcustomfields.com/resources/get_field_object/
Working now ... thanks to the great support team at ACF. I was missing the [$v] after .$choices. Working code below:
<?php
if( have_rows('page_content') ):
while ( have_rows('page_content') ) : the_row();
if( get_row_layout() == 'specs' ):
// check if the nested repeater field has rows of data
if( get_sub_field('objectives') ):
echo '<ul>';
$field = get_field_object('objectives');
$value = $field['value'];
$choices = $field['choices'];
// loop through the rows of data
foreach( $value as $v):
echo '<li>'.$choices.'</li>';
endforeach;
echo '</ul>';
endif;
endif;
endwhile;
endif;
?>
I need to be able to have different classes for repeater items based on the count. In other words, the first repeater item needs class="single-item active" and all other repeaters need class="single-item not-visible move-right".
This is what I have so far:
<?php $count = 0; ?>
<?php if (have_rows('features')): while (have_rows('features')) : the_row(); ?>
<?php if(!$count): ?>
<li class="cd-single-item cd-active"></li>
<?php else: ?>
<li class="cd-single-item cd-not-visible cd-move-right"></li>
<?php $count++; endif; endwhile; endif; ?>
Simply set a boolean to false after the first check:
$repeater = get_field('my_repeater_field');
$first = true;
foreach ($repeater as $sub) {
$class = $first ? 'single-item active' : 'single-item not-visible move-right';
// do whatever with $sub and $class here...
$first = false;
}
Your code is almost correct, but you're only increasing $count if (!$count) is false and this never happens because you're only increasing $count if… and so on.
Just put your $count++ after the first endif. I rewrote it like this:
<?php
$count = 0;
if ( have_rows('features') ):
while ( have_rows('features') ) : the_row();
if ( ! $count ): ?>
<li class="cd-single-item cd-active"></li>
<?php else: ?>
<li class="cd-single-item cd-not-visible cd-move-right"></li>
<?php
endif;
$count++;
endwhile;
endif;
?>
Hope this helps!
I need some insight on how to create a formula that will determine what class to add to a div element based on the number of div's being displayed.
Currently I am using ACF to create a repeater field that will have one sub-field that links to a page based what the admin selects. These fields will display in their own div's based on a while loop. I am using Bootstrap classes such as col-sm-12, col-sm-6, and col-sm-4.
while( have_rows( 'project_repeater' ) ): the_row();
$post_object = get_sub_field( 'project_type' );
$count = count( get_field('project_repeater') ); // Output 4
$mod = $count % 3; // Output 1
if( $post_object ):
$post = $post_object;
setup_postdata( $post );
$classname = '';
if( $mod == 1 ) {
if( $count == 1 ) {
$classname .= 'col-sm-12';
} elseif( $count == 4 ) {
$classname .= 'col-sm-6';
} else { // $count == 7
$classname .= 'col-sm-4';
}
} elseif( $mod == 2 ) {
if( $count == 2 ) {
$classname .= 'col-sm-6';
} elseif( $count == 5 ) {
// $classname .= 'col-sm-12';
} else { // $count == 8
// $classname .= 'col-sm-4';
}
} else { // $mod == 0
$classname .= 'col-sm-4';
}
?>
<div class="project-cat <?php echo $classname; ?>">
<a href="<?php the_permalink( $post ); ?>">
<h5><?php the_title(); ?></h5>
<p><?php the_excerpt(); ?></p>
</a>
</div>
<?php
wp_reset_postdata();
endif;
endwhile;
Now when $count == 5, I need it to set the first 3 div's to have col-sm-4 and then the last 2 div's to have col-sm-6 (this will display the divs in this order stacked on top of each other: 3,2).
When $count == 7, it will need to be similar to when $count == 5 but have the first 3 as col-sm-4 and the last 4 as col-sm-6 (3,2,2)
When $count == 8, it will be slightly different where I have the first 6 as col-sm-4 and the last 2 as col-sm-6 (3,3,2).
Instead of using if statement after if statement, there has to be a better way to create a formula that will determine which class to use based on the number of div elements that are being displayed.
This is more math and logic problem rather than coding problem.
Why don't you 1. get the number of elements.
divide by 3 and floor the result. So you can know how many rows of col-xx-4 you are going to have.
let's say you have 14 items in total. Than fist 12 element should have col-md-4.
if you round 14/3 = 4
what is left is 14 - (3*4) = 2 so if what is left is 1 than you can use col-md-12 class, if it is 2 than col-md-6.
so while outputting you assign class col-md-4 to first 12 elements (round 14/3 = 4) and then assign appropriate class to the remaining elements (14 - (3*4) = 2) based on their number (1 or 2)
Do you think it will work for you?
Sequence
set a few variables
$total = total number of items. You can simply run the loop with a counter.
$md4-limit = floor($total/3)*3;
$remaining = $total - $md4-limit;
then when you are running the loop, use the $i and for each element increment that value and when outputting check
if ($i <= $md4-limit) {
$class = ' col-md-4 ';
} else {
if($remaining == 1) { $class = ' col-md-12 '; }
if($remaining == 2) { $class = ' col-md-6 '; }
}
then echo class into column html where the class should be :)
ANOTHER option is to add row width options to the your ACF repeater field and just construct the grid on the back end.
Thanks to you Nick you helped me out a lot, I did find what I was doing wrong and why it wasn't displaying like you said it would. I was not defining $i properly. But here is the code and everything works! Thanks again to you Nick!
$count = count( get_field('project_repeater') );
$limit = floor( $count / 3 ) * 3;
$remaining = $count - $limit;
$i = 0;
while( have_rows( 'project_repeater' ) ): the_row();
$post_object = get_sub_field( 'project_type' );
if( $i < $limit ) {
$classname = 'col-sm-4';
} else {
if( $remaining == 1 ) {
$classname = 'col-sm-12';
}
if( $remaining == 2 ) {
$classname = 'col-sm-6';
}
}
if( $post_object ):
$post = $post_object;
setup_postdata( $post );
?>
<div class="project-cat <?php echo $classname; ?>">
<a href="<?php the_permalink( $post ); ?>">
<h5><?php the_title(); ?></h5>
<p><?php the_excerpt(); ?></p>
</a>
</div>
<?php
wp_reset_postdata();
endif;
$i++;
endwhile;
I'm wanting to add a class to the first four posts within my WordPress loop.
This is the code I have so far but it seems to be adding the class to all of my posts
<?php
$c = 0;
$class = '';
?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php
$c++;
if ( $c == 1 || $c == 2 || $c == 3 || $c == 4 ) $class .= ' before-load';
?>
<?php $postid = get_the_ID(); ?>
<article itemscope="" itemtype="http://schema.org/BlogPosting" id="<?php echo $postid; ?>" class="thepost<?php echo $class; ?>">
I havn't added the closing tags in this example of the code cos, well there is no point :)
Thanks in advance guys
/* EDIT */
Right, new problem, because I am running an AJAX script that is pulling the posts from essentially the next page, which is causing the count to reset. See below for image
For one you're not resetting the class variable inside the loop, so it still contains the class from the previous iteration, you need to do this:
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php
$class = '';
$c++;
if ( $c == 1 || $c == 2 || $c == 3 || $c == 4 ) $class .= ' before-load';
?>
Secondly, the code could be written much more succinctly like this:
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php
$class = '';
$c++;
if ($c <= 4) $class = ' before-load';
?>