Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
195 views
in Technique[技术] by (71.8m points)

php - ACF repeater field not rendering the rows

I have this website that I'm working on for a client. I jumped into the project and I have to create the accordion on the template file for specific pages. I just started getting deeper into PHP so I found a solution online in this article https://wpbeaches.com/create-an-acf-repeater-accordion-in-wordpress/ Now, my problem is this: I created the repeater field with two subfields https://prnt.sc/xfg3lv and I choose that it only shows on this page template https://prnt.sc/xfg6lw Then I created the fields on the actual page with that template https://prnt.sc/xfgdhp and inserted that piece of code from the article into the template file. Of course, I modified the names with my field names. The problem is that on the front it is not showing the rows even if they exist and just skips to the else statement. I will paste the complete code of that template page so if anyone can help it would be awesome. I'm busting my head for two days now on this issue.

The code (I marked where my code starts and ends):

<?php
/**
* Template Name: Services
*
**/
global $post;
      $post_slug = $post->post_name;
      $grandParent_title=get_page(get_ancestors($post->ID,'page')[0] )->post_title;
      $grandParent_Image=wp_get_attachment_url( get_post_thumbnail_id(get_ancestors($post->ID,'page')[0] )) ;
      get_header();



global $wp;

$current_slug = add_query_arg( array(), $wp->request );
$url_slug=explode('/', $current_slug);

$uri_string=$url_slug[1];
$_SESSION['current_url']=$uri_string;


function find_string_in_array ($arr, $string) {

    return array_filter($arr, function($value) use ($string) {
        return strpos($value, $string) !== false;
    });

}

if(find_string_in_array ($url_slug, 'poly')==true)
{
  $nav_theme_location='polystyrene';
  $BannerClass='';
  $post_type='polystyrene';
  $galleryClass='';
}elseif(find_string_in_array ($url_slug, 'fiber')==true){
  $nav_theme_location='fiberglass';
  $BannerClass='fiberbanner';
  $post_type='fiberglass_type';
  $galleryClass='fiber';
}elseif (find_string_in_array ($url_slug, 'wood')==true) {
  $nav_theme_location='woodwork';
  $BannerClass='woodworkbanner';
  $post_type='woodworks';
  $galleryClass='wood';
}
elseif (find_string_in_array ($url_slug, 'creative')==true) {
  $nav_theme_location='creative_production';
  $BannerClass='creativebanner';
  $post_type='creative_services';
  $galleryClass='creative';

}elseif (find_string_in_array ($url_slug, 'f-and-b')==true) {
  $nav_theme_location='fb';
  $BannerClass='fandbbanner';
  $post_type='creative_services';
  $galleryClass='';

}else{
    $nav_theme_location='';
    $BannerClass='';
    $post_type='';
   $galleryClass='';

}
?>
<section class="banner inner-banner <?= $BannerClass;?>">
    <!-- <div class="bannerBox" style="background-image: url(<?php // echo get_the_post_thumbnail_url())?get_the_post_thumbnail_url():;?>)"> -->
  
    <div class="bannerBox" style="background-image: url(<?= (get_the_post_thumbnail_url()!='')?get_the_post_thumbnail_url():$grandParent_Image;?>)">
        <div class="bannerContent">
            <div class="overlay"></div>
            <h1><?= $grandParent_title;?></h1>
        </div>
    </div>
</section>



<section class="secondrynavigation">
    <div class="container">
        <div class="innerMenu">
        
               <?php
                  $args=array(
                     'theme_location'=>$nav_theme_location,
                     'container'     =>false,
                     'menu_class'    =>'',
                      );
                  wp_nav_menu($args);
                  ?>

        </div>
    </div>
</section>
            
            <section class="pad">
                <div class="container-fluid">
                    <div class="innerHeading">
                        <h3><?php the_title();?></h3>
                        <p><?php
                        $content_post = get_post($post->ID);
                        echo $content = $content_post->post_content;?></p>

       

                    </div>
                </div>
                
                <div class="gallery <?= $galleryClass?>">
                    <div class="container">
                        <div class="row">
                            <?php 
                           // echo $post_type;exit;
                                $loops = new WP_Query(array('posts_per_page'=> -1,
                                                            'post_type' => $post_type, 
                                                            'orderby' => 'id',
                                                            'order' => 'asc',
                                                            'category_name'=>$uri_string));
// echo "<pre>";
  //                              print_r($loops);exit;
                                if($loops->have_posts()):
                                while ($loops->have_posts()) :
                                $loops->the_post();
                              global $post;
                              $post_slug = $post->post_name;
                             ?>
                            <div class="col-lg-4">
                                <a href="<?= get_permalink();?>">
                                    <div class="galleryBox">
                                        <div class="overlay">
                                            <p><i>Read More</i></p>
                                        </div>
                                        <div class="image" style="background-image: url(<?= get_the_post_thumbnail_url();?>)"></div>
                                        <div class="galleryText">
                                            <h5><?php the_title();?></h5>
                                        </div>
                                    </div>
                                </a>
                            </div>
                            <?php endwhile;
                            endif;?>
                            
                        
                          
                        </div>
                    </div>
                </div>
            </section>
            
            <!--MY CODE-->
            
                <?php
    if( have_rows('services_accordion') ):
    
    
    // loop through the rows of data for the tab header
    while ( have_rows('services_accordion') ) : the_row();
        
//      $title = get_sub_field('services_title');
//      $description = get_sub_field('servises_description');

    ?>
    
        <button class="accordion"><?php echo get_field('services_title'); ?></button>
        <div class="panel">
          <p><?php echo get_field('services_description'); ?></p>
        </div>    
        <?php
    endwhile; 

else :


    echo 'In progress';

endif; ?>

<!--MY CODE END-->
            
       
<?php get_footer();?>  

In the end this is the result on the page https://prnt.sc/xfh0z7.

Thanks in advance!

question from:https://stackoverflow.com/questions/65841454/acf-repeater-field-not-rendering-the-rows

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try wrapping your content into:

<?php while (have_posts()) : the_post(); ?>

// all your content related to the post including repeater code.

<?php endwhile; // End of the loop.?>

This way repeater will be in scope of your current post/page. Alternatively you can indicate post id as an argument in the repeater function.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...