使用从魔术场 2 重复文本字段中获取的帖子 ID 创建自定义循环



我正在尝试创建一个自定义循环,其中包含与特定帖子ID相关的内容,我从名为"reference_posts"的魔术字段重复文本字段中获得其数字。

当我回显$testvalue时;它输出正确的帖子列表"20432,43242,34253",但是当我尝试在数组中输出它时,我只得到一遍又一遍重复的第一个值"20432,20432,20432,"。

猜问题是我必须在第一个中包裹第二个foreach,但我无法做到这一点。

谁能帮我?

<?php 
    $value  = get_field ('reference_posts') ;
    foreach ( $value  as  $my_val ) { 
    $testvalue = $my_val . ",";
    echo $testvalue;
    $post_idarray = array( 'post__in' => array( $testvalue ) );
    $postspecial = get_posts($post_idarray);
}
    foreach( $postspecial as $post ) :
    setup_postdata($post);  
    ?> 
<div>my content</div>
<?php endforeach; ?>

提前感谢!

得到它:

<?php 
    $value  = get_field ('reference_posts') ;
    foreach ( $value  as  $my_val );
    $args = array( 'include' => $value );
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) : setup_postdata($post); ?>
<div>my content</div>
<?php endforeach; ?>

最新更新