首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ACF直放随机序全域

问ACF直放随机序全域
EN

Stack Overflow用户
提问于 2022-11-15 09:20:55
回答 1查看 26关注 0票数 0

希望以随机顺序输出高级自定义字段中继器。我有一个中继器字段“配置文件”包含5“配置文件”的帖子对象。

我今天的准则是:

代码语言:javascript
复制
// Randomize and shuffle the rows
$rows = get_sub_field('profiles');
shuffle($rows);
$rand_repeater_fields = array_rand( $rows , 4 ); ?>

<?php if( have_rows('profiles') ):
$stage_index = 0; ?>
<?php while ( have_rows('profiles') ) : the_row();
    // print rows only if in array
    if (in_array(get_row_index() - 1, $rand_repeater_fields)) { ?>

        <?php $post_object = get_sub_field('profile'); //row w. post object start
            if( $post_object ): 
            $post = $post_object;
            setup_postdata( $post ); ?>
                <?php the_permalink();?>
            <?php wp_reset_postdata(); ?>
        <?php endif; ?> //row w. post object end

    <?php // increment index
        $stage_index++;
    } ?>
<?php endwhile; ?>

这段代码成功地以随机顺序输出了5行中的4行,这意味着它几乎可以按照我的意愿工作。

如何更新这个片段以随机顺序输出中继器字段的所有字段,即使它们的总数为3或10?

感谢你的建议!

EN

回答 1

Stack Overflow用户

发布于 2022-11-15 10:28:58

代码语言:javascript
复制
<?php
$args = array(
    'post_type' => 'page',
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'orderby' => 'rand',
    'meta_query' => array(
        array(
            'key' => 'profiles',
            'compare' => 'EXISTS'
        )
    )
);
$query = new WP_Query( $args );
if ( $query->have_posts() ) {
    while ( $query->have_posts() ) {
        $query->the_post();
        $profiles = get_field('profiles');
        if( $profiles ):
            foreach( $profiles as $profile ):
                echo '<div class="profile">';
                    echo '<h2>' . $profile['profile'] . '</h2>';
                echo '</div>';
            endforeach;
        endif;
    }
}
wp_reset_postdata();
?>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74443199

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档