简体   繁体   English

Wordpress ACF 获取块字段

[英]Wordpress ACF Get Block Field

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.我遇到了 ACF 的问题,我只是不知道发生了什么,互联网上没有任何帮助。 I've added some fields to the Image Slider block:我在 Image Slider 块中添加了一些字段: 在此处输入图像描述

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.但无论我在我们的自定义块代码中尝试什么: image-slider.php ,我都无法获取任何auto_play字段的值。 get_field always returns null. get_field始终返回 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.我知道值在那里,因为如果我转储get_fields( $postID ) ,我可以看到['page_builder'][2]元素具有我想要的值。 I could get to it that way, but I can't seem to determine which index I'm on (the 2) programmatically.我可以那样做,但我似乎无法以编程方式确定我在哪个索引(2)上。

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.因此,如果您知道我如何直接访问该字段,或者找出我当前的“page_builder”索引,那将非常有帮助。

It's super confusing, because the have_rows( 'slide_setting' ) call obviously knows where to look, and works as expected.这非常令人困惑,因为have_rows( 'slide_setting' )调用显然知道在哪里查看,并且按预期工作。

The custom block php looks like:自定义块 php 如下所示:

<?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).所以我无法得到我的问题的完美答案,看起来获得我想要的东西的 API 不存在(愚蠢)。

What I ended up with - I set up a new function in my theme's functions.php file that looks like the following:我最终得到了什么 - 我在主题的functions.php文件中设置了一个新函数,如下所示:

$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:然后在我的image-slider.php文件中,我这样称呼它:

$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. $post_slider_config_index变量跟踪检索到的最后一个索引,因此如果页面上有多个滑块,它将在呈现时抓取正确的一个。

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.烦人的 WP 不仅仅为您提供它显然已经拥有的关于您在页面中的位置的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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