I'm using ACF pro 5. I create a Repeater Field with two sub_field, called sub_field_item & sub_field_value. I need a code which will hide if sub_field_value is empty. Probably sub_field_item contains text but if sub_field_value empty, then it will hide both.
I tried with this code, but it's not working.
<?php
if( have_rows('myrepeater') )
{
$field_key = "field_5aa18d1bc322c"; //KEY for Repeater main field "myrepeater
$field = get_field_object($field_key);
foreach($field['value'] as $value)
{
if(!empty($value['sub_field_item']))
{
$not_empty = true;
break;
}
}
if($not_empty == true)
{
echo '<h2>' . $field['label'] . '</h2>';
}
while ( have_rows('sub_field_item') )
{
the_row();
$subfield = get_sub_field('sub_field_value');
if( !empty($subfield) )
{
echo '<b>' . $subfield . '</b>';
}
}
}
?>
here is the magic code :
<?php
if(get_field('field_name')):
while(has_sub_field('field_name')):
if(get_sub_field('subfield_name')): ?>
<h2><?php echo get_sub_field('subfield_name'); ?></h2>
<?php endif;
endwhile;
endif;
Related
I'm running into an issue with ACF, and I just can't figure out what's going on, and nothing on the internet is helping out.
I've added some fields to the Image Slider block:
But no matter what I try inside of our custom block code: image-slider.php I cannot get the values of any of the auto_play fields. get_field always returns null. I know the value is there, because if I dump out get_fields( $postID ), I can see the ['page_builder'][2] element has the value I want. I could get to it that way, but I can't seem to determine which index I'm on (the 2) programmatically.
So if you know either, how I can access the field directly, or figure out my current 'page_builder' index, that would be extremely helpful.
It's super confusing, because the have_rows( 'slide_setting' ) call obviously knows where to look, and works as expected.
The custom block php looks like:
<?php
if(have_rows( 'slide_setting' ) ) {
$digits = 3;
$randID = rand(pow(10, $digits-1), pow(10, $digits)-1);
echo '<div class="container"><div class="row"><div id="swiper_'.$randID.'" class="col-md-12 wiche-swiper-top-navigation-wrapper">';
echo '<div class="swiper-container wiche-swiper-top-navigation">';
// var_dump( get_fields( get_the_ID() )['page_builder'][2] );
// var_dump( get_post_field( 'auto_play' ) );
// var_dump(get_field('image_slider_settings_auto_play'));
// var_dump(get_row_index());
// var_dump(get_field_objects( $post->ID ));
// var_dump( get_row_index() );
// var_dump( acf_get_field_group( 'slide_setting' ) );
// die();
if ( get_field( 'auto_play' ) ) {
echo '<div class="swiper-wrapper" data-swiper-autoplay="' . get_field( 'auto_play_delay' ) . '" data-swiper-disable-on-interaction="' . get_field( 'auto_play_disable_on_interaction' ) . '">';
} else {
echo '<div class="swiper-wrapper">';
}
while( have_rows( 'slide_setting' ) ) {
the_row();
$title = get_sub_field( 'title' );
$image = get_sub_field( 'image' );
$content = get_sub_field( 'content' );
if ( $image || $content ) {
echo '<div class="swiper-slide swiper-banner-slide swiper-no-swiping">';
if ( $title ) {
echo '<div class="text-center slider-top-title">';
echo $title;
echo '</div>';
}
if ( $image ) {
echo '<div class="banner-image">';
echo wp_get_attachment_image( $image, 'full', '', array( 'loading' => false ) );
echo '</div>';
}
if ( $content ) {
echo '<div class="banner-content">';
echo $content;
echo '</div>';
}
echo '</div>';
}
}
echo '</div>';
echo '</div>';
echo '<div class="swiper-button-next swiper-button-next-outsite">Next</div><div class="swiper-button-prev swiper-button-prev-outsite">Prev</div>';
echo '</div></div></div>';
}
So I wasn't able to get a perfect answer to my question, looks like the API to get what I want doesn't exist (dumb).
What I ended up with - I set up a new function in my theme's functions.php file that looks like the following:
$post_slider_config_index = 0;
function get_the_slider_config( $post_id ) {
global $post_slider_config_index;
$page_builder = get_fields( $post_id )['page_builder'];
$slider_config = null;
foreach ($page_builder as $key => $value) {
if ( $value['acf_fc_layout'] === 'image_slider_settings' ) {
if ( $key > $post_slider_config_index ) {
$slider_config = $value;
$post_slider_config_index = $key;
break;
}
}
}
return $slider_config;
}
And then inside my image-slider.php file I call it like so:
$slider_config = get_the_slider_config( get_the_ID() );
if ( $slider_config[ 'auto_play' ] ) {
echo '<div class="swiper-wrapper" data-swiper-autoplay="' . $slider_config[ 'auto_play_delay' ] . '" data-swiper-disable-on-interaction="' . $slider_config[ 'auto_play_disable_on_interaction' ] . '">';
} else {
echo '<div class="swiper-wrapper">';
}
The $post_slider_config_index variable keeps track of the last index retrieved so that if there are multiple sliders on a page, it'll grab the right one as its rendered.
It's not perfect, it's not super efficient, but it does what I needed. Annoying WP doesn't just give you the information it obviously has already regarding where you are in the page.
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
I created a Custom Field with the Repeater layout to add some input text.
I would like to display all the values.
I found some code on ACF documentation but I can't understand how it works
<?php
$rows = get_field('repeater_field_name');
if($rows)
{
echo '<ul>';
foreach($rows as $row)
{
echo '<li>sub_field_1 = ' . $row['sub_field_1'] . ', sub_field_2 = ' . $row['sub_field_2'] .', etc</li>';
}
echo '</ul>';
}
?>
http://www.advancedcustomfields.com/resources/repeater/
I don't know how much fields I will create with the Repeater and I would like to loop all the values with foreach. Is that possible?
Thank you in advance
Foreach version:
<?php
$rows = get_field('repeater');
if($rows)
{
echo '<ul>';
foreach($rows as $row)
{
echo '<li>sub_field_1 = ' . $row['text'] . '</li>';
}
echo '</ul>';
}
While version:
<?php
// check if the repeater field has rows of data
if( have_rows('repeater') ):
// loop through the rows of data
while ( have_rows('repeater') ) : the_row();
// display a sub field value
the_sub_field('text');
endwhile;
else :
echo 'nothing found';
endif;
?>
I would fix it like this:
<?php
if( have_rows('slide') ):
$l= 1;
while( have_rows('slide') ): the_row();
$l++;
endwhile;
endif;
?>
On a wordress site I'm building, for the first time, I'm trying to add custom loops.
<?php
$custom_loop = new WP_Query('showposts=5&category_name=Featured&orderby=rand');
if ( $custom_loop->have_posts() ) :
echo '<ul>';
while ( $custom_loop->have_posts() ):
$custom_loop->the_post();
echo '<li>' . get_the_title() . '
</li>';
endwhile;
wp_reset_query();
echo '</ul>';
endif;
?>
How do I correctly add this line if ( has_post_thumbnail() ) {
the_post_thumbnail();} inbetween the <li></li> tags?
I've tried just placing it in, but it shows up like normal text on the site.
If you want to put the_post_thumbnail() value between li tag, use below code instead of your echo '<li>.....';
echo '<li>' . get_the_title() . '';
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail();
}
echo '</li>';
Try this:
<?php
$custom_loop = new WP_Query('showposts=5&category_name=Featured&orderby=rand');
if ($custom_loop->have_posts()) {
echo '<ul>';
while ($custom_loop->have_posts()) {
$custom_loop->the_post();
$thumb = (has_post_thumbnail()) ? the_post_thumbnail() : '';
printf('<li>%s%s</li>', get_permalink(), get_the_title(), $thumb);
}
wp_reset_query();
echo '</ul>';
}
?>
I am trying to display the classic breadcrumbs on a WP based site. I have the following function,
//breadcrumb function
function the_breadcrumb() {
if (!is_home()) {
echo '<a href="';
echo get_option('home');
echo '">';
//bloginfo('name');
echo 'Home';
echo "</a> > ";
if (is_category() || is_single()) {
the_category('title_li=');
if (is_single()) {
echo " » ";
the_title();
}
} elseif (is_page()) {
echo the_title();
}
}
}
But when the page is inside a parent category (i.e., About (Parent), Advisors (Child)) it only shows the child page. Any thoughts on how I can add a condition to show the parent page as well? Any help would be greatly appreciated.
/* EDITED */
I found a perfect working function for it:
function breadcrumbTrail($crumbs = true, $title = 'Browse', $separator = '/')
{
global $post;
?>
<div class="breadcrumbs">
<?php _e('Home','options'); ?> <?php echo $separator; ?>
<?php
if(is_single()) :
the_category(', '); echo ' ' . $separator . ' ';
elseif(is_page()) :
$parent_id = $post->post_parent;
$parents = array();
while($parent_id) :
$page = get_page($parent_id);
if($params["link_none"])
$parents[] = get_the_title($page->ID);
else
$parents[] = ''.get_the_title($page->ID).' ' . $separator . ' ';
$parent_id = $page->post_parent;
endwhile;
$parents = array_reverse($parents);
foreach($parents as $val) :
echo $val;
endforeach;
endif;
the_title();
?>
</div>
<?php
}
Hope this helps someone out.
I suggest using the Breadcrumb NavXT plugin, it works not only for categories but also the pages with parent.
If you want to write your own customised code for the breadcrumb, you can see how Breadcrumb NavXT did it.