【问题标题】:ACF repeater field not rendering the rowsACF 转发器字段未呈现行
【发布时间】:2021-04-26 16:50:23
【问题描述】:

我有一个正在为客户开发的网站。我跳进了这个项目,我必须在模板文件上为特定页面创建手风琴。我刚开始深入了解 PHP,所以我在这篇文章中在线找到了解决方案 https://wpbeaches.com/create-an-acf-repeater-accordion-in-wordpress/ 现在,我的问题是: 我创建了带有两个子字段 https://prnt.sc/xfg3lv 的转发器字段,我选择它只显示在此页面模板 https://prnt.sc/xfg6lw 然后我使用该模板https://prnt.sc/xfgdhp 在实际页面上创建了字段,并将文章中的那段代码插入到模板文件中。当然,我用我的字段名称修改了名称。问题是在前面它没有显示行,即使它们存在,只是跳到 else 语句。我将粘贴该模板页面的完整代码,因此如果有人可以提供帮助,那就太好了。在这个问题上,我已经绞尽脑汁两天了。

代码(我标记了代码的开始和结束位置):

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

最后这是https://prnt.sc/xfh0z7页面上的结果。

提前致谢!

【问题讨论】:

    标签: php wordpress advanced-custom-fields


    【解决方案1】:

    尝试将您的内容包装成:

    <?php while (have_posts()) : the_post(); ?>
    
    // all your content related to the post including repeater code.
    
    <?php endwhile; // End of the loop.?>
    
    

    这样转发器将在您当前帖子/页面的范围内。或者,您可以将帖子 ID 指示为转发器函数中的参数。

    【讨论】:

    • 非常感谢,问题解决了!!!您能否解释一下为什么这是必要的,因为这是特定帖子类型和特定页面模板的模板?在启动这样的循环之前,我是否总是需要检查是否有帖子?
    • the_post();在那里设置所有与帖子相关的变量,因此如果您查看任何标准页面公关帖子模板,它也有围绕与帖子相关的内容的那些代码行。可能 wordpress codex 可以比我更好地解释事情,所以你可以在那里查找 the_post 函数以及它的作用:)
    猜你喜欢
    • 2018-10-06
    • 1970-01-01
    • 2019-03-19
    • 2018-04-12
    • 2019-05-24
    • 2014-07-07
    • 1970-01-01
    • 2014-02-03
    • 2016-06-17
    相关资源
    最近更新 更多