简体   繁体   中英

Using WordPress ACF repeater in header.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. ie 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM