希望以随机顺序输出高级自定义字段中继器。我有一个中继器字段“配置文件”包含5“配置文件”的帖子对象。
我今天的准则是:
// 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?
感谢你的建议!
发布于 2022-11-15 10:28:58
<?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();
?>https://stackoverflow.com/questions/74443199
复制相似问题