Changing Repeater class based on count in Advanced Custom Fields - php

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!

Related

list all the label and value from a group field type in ACF

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.

ACF - get checkbox values inside flexible content field

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

Nav menu current item not highlihting

I was working on a site with categories as primary menu .I have a new post type and added an archive content as menu item with label 'City Guide'.Now when I on City Guide page the menu item is not highlighting.Or with code the class 'current' is not printing in the menu item class.Here is my code:
<?php
$menu_name = PRIMARY_NAVIGATION_NAME;
$menu_items = array();
if( ( $locations = get_nav_menu_locations() ) && isset( $locations[ $menu_name ] ) ){
$menu = wp_get_nav_menu_object( $locations[ $menu_name ] );
$menu_items = $menu ? wp_get_nav_menu_items($menu->term_id) : array();
}
?>
<nav class="visible-sm visible-md visible-lg" role="navigation">
<ul id="menu-primary-navigation" class="main-navigation">
<?php if( !empty($menu_items) ): ?>
<?php
$current_category_object = null;
if( !empty($wp_query->query_vars['category_name']) ){
$category_name = $wp_query->query_vars['category_name'];
$current_category_object = get_category_by_slug($wp_query->query_vars['category_name']);
}
?>
<?php foreach( $menu_items as $item): ?>
<?php
$active_class = false;
if ($item->object == 'category' && !empty($current_category_object) ){
$cat_object = get_category($item->object_id);
if ($cat_object->term_id == $current_category_object->term_id || $cat_object->term_id == $current_category_object->parent) {
$active_class = true;
}
}
?>
<li class="<?php echo sanitize_title($item->title); ?><?php echo ' menu-item-object-category'; if ($active_class) echo ' current'?>">
<a href="<?php echo $item->url; ?>" class="main-category"<?php if( $item->target) echo ' target="' . $item->target . '"'; ?>><?php echo $item->title ?></a>
<?php if( $item->object == 'category'): ?>
<?php
$sub_categories = get_terms( array('category'), array('child_of' => $item->object_id, 'hide_empty' => 0) );
if (count($sub_categories) <= 4) {
include(locate_template('partials/sub-menu-two-featured.php'));
} else {
include(locate_template('partials/sub-menu-one-featured.php'));
}
?>
<?php endif; ?>
<?php if ($item->title == 'City Guide'): ?>
<?php include(locate_template('partials/sub-menu-city-guide.php')); ?>
<?php endif; ?>
</li>
<?php wp_reset_query(); ?>
<?php endforeach; ?>
<?php endif; ?>
</ul>
</nav>
How can i implement it into it using a condition to add current for my archive page menu item in the above code .
I tried
<?php if ($item->title == 'City Guide'): ?>
but it was echoing in all instance not when the menu is active.
Please help.
One possible solution is to use get_queried_object() function for this.
Codex says
if you're on a single post, it will return the post object
if you're on a page, it will return the page object
if you're on an archive page, it will return the post type object
if you're on a category archive, it will return the category object
if you're on an author archive, it will return the author object
If you start using $qo = get_queried_object() then no need to use this code
if( !empty( $wp_query->query_vars['category_name'] ) ){
$category_name = $wp_query->query_vars['category_name'];
$current_category_object = get_category_by_slug(
$wp_query->query_vars['category_name']
);
}
because:
If you are in category-archive page, $qo will contain category object, and $qo->term_id and $qo->parent will be available.
If you are in CPT archive page, then $qo will contain CPT object, and properties like CPT name, query_var, label and so on will be available.
So when you have custom queried object and with the help of some conditional tags like is_tax(), is_tag(), is_category(), is_post_type_archive() you will be able to determine exactly where you are.
P.S. You must check the codex first, because there have some notes for precedence when using get_queried_object()
<?php
$active_class = false;
$this_category = get_queried_object();
$this_category->name;
if ($this_category->name == 'city-guide' ){
$active_class = true;
} ?>
<li class="<?php echo sanitize_title($item->title); ?><?php echo ' menu-item-object-category'; if ($active_class ==1 && sanitize_title($item->title) =='city-guide') echo ' current'; ?>">
this worked thanks #pgk for pointing me out

Add class to first four posts

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';
?>

Combining Multiple if into one?

I have two wordpress queries and am curious if there is a way to combine them into one?
I have tried else if, but to no avail.
<?php
$posts = get_field('appeal_forms', 'options');
if( $posts ): ?>
<ul>
<?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
<?php setup_postdata($post); ?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>
And here the second:
<?php
$posts = get_field('misc', 'options');
if( $posts ): ?>
<ul>
<?php foreach( $posts as $post): // variable must be called $post (IMPORTANT) ?>
<?php setup_postdata($post); ?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach; ?>
</ul>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php endif; ?>
You need to combine both arrays of posts into a single array then loop through it as you were doing.
In the example below I'm getting both arrays, checking they are valid using is_array and then merging them into one using array_merge.
<?php
// Get both post arrays.
$appeal_forms = get_field('appeal_forms', 'options');
$misc = get_field('misc', 'options');
// Check both are arrays.
if ( is_array( $appeal_forms ) && is_array( $misc ) ) {
// Combine the posts into a single array.
$posts = array_merge( $appeal_forms, $misc ); ?>
<ul>
<?php foreach ( $posts as $post ) {
setup_postdata( $post ); ?>
<li>
<?php the_title(); ?>
</li>
<?php } ?>
</ul>
<?php wp_reset_postdata(); ?>
<?php } ?>
If both arrays won't always have posts then the code needs to be adapted. The check I perform means that if appeal_forms or misc is empty then nothing is displayed.
If that's the case the solution I'd propose would be:
$posts = array();
// Check each array individually and add their contents to the post array.
if ( is_array( $appeal_forms ) ) {
$posts = array_merge( $posts, $appeal_forms );
}
if ( is_array( $misc ) ) {
$posts = array_merge( $posts, $misc );
}
if ( $posts ) {…

Categories