Using WordPress ACF repeater in header.php - php

Within ACF, I have a repeater called "slider" with a radio button field. This appears on the homepage of the site.
I'd like to output the radio button field within header.php. Here's what I'vd tried:
<?php
if( have_rows('slider',$post->ID) ):
while ( have_rows('slider',$post->ID) ) : the_row();
if(get_sub_field('logo_type',$post->ID) == 'light' ) {
echo '<p>Light</p>';
}
endwhile;
endif;
?>
This is coming up empty even when I try var_dump(get_sub_field('logo_type',$post->ID));
I've also tried:
<?php
if( have_rows('slider',$post->ID) ):
global $wp_query;
$postid = $wp_query->post->ID;
while ( have_rows('slider',$postid) ) : the_row();
if(get_sub_field('logo_type',$postid) == 'light' ) {
echo '<p>Light</p>';
}
endwhile;
endif;
?>
What am I doing wrong here?

Did you try without this $post->ID
<?php
if( have_rows('slider') ):
while ( have_rows('slider') ) : the_row();
if(get_sub_field('logo_type') == 'light' ) {
echo '<p>Light</p>';
}
endwhile;
endif;
?>

I don't know if this will solve your problem but get_sub_field second parameter should not be the post id but the format value. So you would leave it empty in this case.
<?php
if( have_rows('slider',$post->ID) ):
global $wp_query;
$postid = $wp_query->post->ID;
while ( have_rows('slider',$postid) ) : the_row();
if(get_sub_field('logo_type') == 'light' ) {
echo '<p>Light</p>';
}
endwhile;
endif;
?>
I also recommend debugging what ID you're getting from $postid.
<?php
global $wp_query;
$postid = $wp_query->post->ID;
echo $postid;
?>

I think you need to add your custom field along with menu. i.e. create field group with header and assign menu is equal to your header menu name and then call that field using following code
$menu = wp_get_nav_menu_object('menuid');//replace with your menu id.
the_field('logo_type',$menu);
you can access it anywhere and you can see this field in Apperance->Menus->Menu-name->acf-field-name

Related

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

Can't Echo Custom Field In Widget

I'm trying to display a custom field in a widget I'm customizing but I can't get the custom field to display. I think it has something to do with the variable I'm using to get the post ID in the loop because when I change it to the standard the_title function, the widget works, so it has something to do with how I'm calling the custom field.
I know the key to the custom field is "wpcf-promo-title" but no matter what I've tried, the custom field (which holds a shortened version of the post title for the sidebar) just won't display. This code results in the thumbnails showing, but not the promo title. You can see this in action at http://www.cantstopshipping.com
Here's my code, including the query and the front end of the widget.
function widget($args, $instance) {
extract( $args );
$title = apply_filters( 'widget_title', empty($instance['title']) ? 'Recent Posts' : $instance['title'], $instance, $this->id_base);
$show_date = isset( $instance['show_date'] ) ? $instance['show_date'] : false;
if ( ! $number = absint( $instance['number'] ) ) $number = 5;
if( ! $cats = $instance["cats"] ) $cats='';
// array to call recent posts.
$crpw_args=array(
'showposts' => $number,
'category__in'=> $cats,
);
$crp_widget = null;
$crp_widget = new WP_Query($crpw_args);
echo $before_widget;
// Widget title
echo $before_title;
echo $instance["title"];
echo $after_title;
// Post list in widget
echo "<ul>\n";
while ( $crp_widget->have_posts() )
{
$crp_widget->the_post();
?>
<li class="crpw-item">
<p style="float:left">
<?php the_post_thumbnail('sidebar-small'); ?>
</p>
<?php $promotitle = get_post_meta($post->ID, 'wpcf-promo-title', true); ?>
<p style="float:right; width:200px">
<?php echo $promotitle; ?>
</p>
<?php if ( $show_date ) : ?>
<span class="crpw-date"><?php echo "("; ?><?php echo get_the_date(); ?><?php echo ")"; ?></span>
<?php endif; ?>
</li>
<?php
}
wp_reset_query();
echo "<div class=\"fix\"></div>";
echo "</ul>\n";
echo $after_widget;
}
It looks like your global $post is missing.
But you could try get_the_ID() instead of $post->ID.
You should also consider getting rid of extract(), it's now considered a "bad" practice.
Another thing is that you should use wp_reset_postdata() to restore the global $post object. The wp_reset_query() call should be used with the query_posts() call.

Wordpress / PHP: Get value of variable outside the loop

In my WordPress template (that I am developing), I have the header.php and a template part named header-head.php (called in the header.php). At the index.php, I have other template parte named content.php (called in the index.php). I am trying to get a information of the first post of the taxonomy (named feeling) that I created and return the value in the header-head.php.
The content.php code:
$terms = get_the_terms($post->ID, 'feeling');
foreach ($terms as $term) {
//Always check if it's an error before continuing. get_term_link() can be finicky sometimes
$term_link = get_term_link( $term, 'feeling' );
if( is_wp_error( $term_link ) )
continue;
//We successfully got a link. Print it out.
$term_return = 'Feeling ' . $term->name . '';
}
if ( has_term( '', 'feeling' ) AND $count == 0 ) {
$latest_feeling = "<p class='feeling-text'>" . $term_return . "</p>";
$GLOBALS['feeling_post'] = $latest_feeling;
}
The header-head.php code:
$feeling_return = $GLOBALS['feeling_post'];
echo $feeling_return;
You may have noticed the variable $count, it count the number of posts to check for the first post. I did it in the index.php file and then got the value in the content.php file using $GLOBALS, but now I am not getting...
The index.php loop code:
<?php if ( have_posts() ) : ?>
<?php $count = 0; ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php $GLOBALS['count_post'] = $count++; ?>
<?php
/* Include the Post-Format-specific template for the content.
*/
get_template_part( 'partials/content', get_post_format() );
?>
<?php endwhile; ?>
<?php sensibus_paging_nav(); ?>
<?php else : ?>
<?php get_template_part( 'partials/content', 'none' ); ?>
<?php endif; ?>
Can someone help me? What am I doing wrong?

Advanced Custom Fields - If/Else Statement with true/false checkbox

I have added a true/false checkbox using Advanced Custom Fields for Wordpress. I want to be able to select an option which amends the page template.
I am adding this option to the Product Category in WooCommerce / Wordpress. I have included this bit of logic in the code.
I have the following code but it does not work. I suspect it is because it is not within the loop. However the code I want to insert includes the loop. Any ideas/guidance on the code is much appreciated
<?php if (is_product_category() && get_field('field_name') == true) { ?>
<div class="custom-sidebar-right">
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php woocommerce_get_template_part( 'content', 'product' ); ?>
<?php endwhile; // end of the loop. ?>
</div>
<?php } elseif (is_product_category() && get_field('field_name') == false ) { // Added close brace
<div> Empty Test </div>
}
Ok, I re-read the docs for ACF and found the following (http://www.advancedcustomfields.com/resources/how-to/how-to-get-values-from-a-taxonomy-term/)
So I applied with some logic and now it works. Thanks for var_dump pointer as that helped me fix this.
// vars
$queried_object = get_queried_object();
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id;
$is_field_name = get_field('field_name', $taxonomy . '_' . $term_id);
if (is_product_category() && $is_field_name == false) { ?>
missing bracket in the if statement ,
change
if (is_product_category() && get_field('field_name') == true)
with
if (is_product_category() && get_field('field_name') == true) )

How to display post category inside page.php if else statement?

I have set up a few categories and want to display specific ones based on is_page().
Inside page.php, I've created an if..else statement that checks the page name and prints out the specific category. My problem at the moment is that instead of just the_title being printed out the whole post is being printed.
Where am I going wrong with this?
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', 'page' ); ?>
<?php if ( is_page( 'Greywater Recycling' ) ) { ?>
<div class="col">
<?php query_posts( 'category_name=Greywater Recycling&posts_per_page=5'); if (have_posts()) : while (have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; endif; ?>
</div>
<?php } else if ( is_page( 'Stormwater Management' ) ) { ?>
<div class="col">
<?php query_posts( 'category_name=Stormwater Management&posts_per_page=5'); if (have_posts()) : while (have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; endif; ?>
</div>
<?php } else if ( is_page( 'Rainwater Harvesting' ) ) { ?>
<div class="col">
<?php query_posts( 'category_name=Rainwater Harvesting&posts_per_page=5'); if (have_posts()) : while (have_posts()) : the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; endif; ?>
</div>
<?php } ?>
<?php endwhile; // end of the loop. ?>
Many problems with your code. For one, is_page does not work inside the loop.
Second, don't mess with query_posts: When should you use WP_Query vs query_posts() vs get_posts()?. Really, forget about it for secondary loops.
Your code can be simplified to the following. In functions.php, we drop one function to get the Category ID by its Name. And another to do a secondary loop using the ID. And then, in page.php a simple call to those functions.
Documentation: Class_Reference/WP_Query.
page.php
Notice that you don't need to open PHP tags at each line, that makes the code dreadly difficult to read.
Use it only to swap between PHP and HTML
<?php
get_template_part( 'content', 'page' );
// This page title
$the_title = get_the_title();
// Search for category with the same name of the page
$category = brsfl_get_category_id( $the_title );
// Category found, go to the secondary loop
if( $category ) {
brsfl_check_print_categories( $category );
}
functions.php
Always prefix your function names with something distinctive to avoid conflicts that may take the site down.
/**
* Get the category ID based on its name
*/
function brsfl_get_category_id( $cat_name )
{
$term = get_term_by( 'name', $cat_name, 'category' );
// term found, return its ID
if( $term ) {
return $term->term_id;
}
// not found
return false;
}
/**
* Print a loop based on a category ID
*/
function brsfl_check_print_categories( $cat_id )
{
$args = array(
'post_type' => 'post',
'cat' => $cat_id,
'posts_per_page' => 5
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() )
{
while ( $the_query->have_posts() ) :
$the_query->the_post();
echo '<h2>' . get_the_title() . '</h2>';
endwhile;
}
else
{
echo 'No posts found...';
}
}
Although I've answered within the proposed scope, I think there are better solutions to this, like:
a shortcode, just adapt the functions.
using Advanced Custom Fields to show a meta box where you can select a very specific category (don't relying in page and category names) and use only the WP_Query function to print it out in the template.
i think it's better to get the category by the slug name.
i've tried your solution, it works fine but in the case of one of my category has a name with special html characters like apostrophes or quotes it doesn't.
here is the piece of code edited from your solution :
in your functions.php
function brsfl_get_category_id($cat_name){
$term = get_term_by( 'slug', $cat_name, 'category' );
if( $term ) {
return $term->term_id;
}
return false;
}
in your page.php
$the_slug = get_post(get_the_ID())->post_name;
$category = brsfl_get_category_id( $the_slug );
if( $category ) {
brsfl_check_print_categories( $category );
}

Categories