我如何更改我的PHP代码以使转发器中的图库正常工作?

使用ACF,可以选择在转发器字段中添加图库。但是,我的问题是,我如何编写可以展示此画廊的PHP。

我试图找到类似的问题,但哪些解决方案对我不起作用。我还尝试查找YouTube视频并搜索ACF文档网站。支持中继器中画廊的组合的代码对我似乎不起作用。

这是转发器的标准ACF PHP格式:

itemRecyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
    override fun onScrolled(recyclerView: RecyclerView,dx: Int,dy: Int) {
        super.onScrolled(recyclerView,dx,dy)
        log("onScrolled")
        val layoutManager = itemRecyclerView.layoutManager
        if (layoutManager is LinearLayoutManager) {
        log("First visible Item: " + layoutManager.findFirstVisibleItemPosition())
        log("First complete visible Item: " + layoutManager.findFirstCompletelyVisibleItemPosition())
        log("Last visible Item: " + layoutManager.findLastVisibleItemPosition())
        log("Last complete visible Item: " + layoutManager.findLastCompletelyVisibleItemPosition())
    }
}

这是画廊的标准ACF PHP格式:

<?php

// check if the repeater field has rows of data
if( have_rows('repeater_field_name') ):

    // loop through the rows of data
    while ( have_rows('repeater_field_name') ) : the_row();

        // display a sub field value
        the_sub_field('sub_field_name');

    endwhile;

else :

    // no rows found

endif;

?>

我希望我能以某种方式将两者结合起来以获得预期的结果。我的PHP知识不是很好。因此欢迎所有帮助。

zhangwei805 回答:我如何更改我的PHP代码以使转发器中的图库正常工作?

您可以选择其他解决方案, 如果您希望每个中继器都带有图像,则只需添加图像而不是图库即可。 如果每次迭代需要多个图像,则可以在中继器内添加一个中继器。

解决方案1:

<?php
// check if the repeater field has rows of data
if( have_rows('repeater_field_name') ):
    // loop through the rows of data
    while ( have_rows('repeater_field_name') ) : the_row();
        echo '<img src="' . get_sub_field('image_field_name') . '">';
    endwhile;
else :
    // no rows found
endif;
?>

解决方案2:

<?php
// check if the repeater field has rows of data
if( have_rows('repeater_field_name') ):
    // loop through the rows of data
    while ( have_rows('repeater_field_name') ) : the_row();
        echo '<ul>';
        while( have_rows('repeater_sub_field_name') ) : the_row();
            echo '<li><img src="' . get_sub_field('image_field_name') . '"></li>';
        endwhile;
        echo '</ul>';
    endwhile;
else :
    // no rows found
endif;
?>
本文链接:https://www.f2er.com/3166616.html

大家都在问