foreach循环内的ACF功能

时间:2017-11-08 08:20:39

标签: php advanced-custom-fields

我有一个名为singlePromotion的函数,它基于ACF工作,我在主页中调用了函数但是我不能用foreach循环添加HTML,这是我的代码:

<?php
                // WP_Query arguments
                $args = array(
                    'post_type' => array('post'), 'posts_per_page' => -1
                );

                // The Query
                //$edition = new WP_Query($args);
                if(!$post || !$post->ID)
                        $post = get_post(HOME_ID);
                // The Loop
                if ($post && $post->ID) {


                        // get projects
                        $posts = get_field('home_promotions',HOME_ID);
                        foreach ($posts as $key => $promo) {
                            $items2 = singlePromotion($promo,0,5);
                            //exit(print_r($items,1));

                        foreach ($items2 as $value) {

                            ?>
                    <div class="col-sm-1-5 grid-item" onclick="">
                        <article class="entry-item">
                            <div class="front">
                                <div class="entry-thumb">
                                    <img src="<?php echo $image; ?>"  alt="">
                                </div>
                                <?php if( get_field('imdb_rate') ): ?>
                                <div class="rate"><?php the_field('imdb_rate') ?></div>
                            <?php endif; ?>
                            </div>
                            <div class="back">
                                    <h4 class="entry-title mbm"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
                                    <div class="pg mbm">G</div>
                                    <div class="desc-mv"><div>
                                        <span>تاریخ انتشار:</span><?php the_field('movie_release'); ?>
                                    </div>
                                    <div>
                                        <span>ژانر:</span>
                                        <?php
                                        $categories = get_the_category();
                                $separator = ' | ';
                                $output = '';
                                if ( ! empty( $categories ) ) {
                                    foreach( $categories as $category ) {
                                        $output .= '<a href="' . esc_url( get_category_link( $category->term_id ) ) . '" alt="' . esc_attr( sprintf( __( 'View all posts in %s', 'textdomain' ), $category->name ) ) . '">' . esc_html( $category->name ) . '</a>' . $separator;
                                    }
                                    echo trim( $output, $separator );
                                }
                                ?>
                                    </div>
                                    <div>
                                        <span>مدت نمایش:</span>
                                        <?php the_field('movie_time') ?>
                                    </div>
                                    <div>
                                        <span>زبان:</span>
                                        <?php the_field('movie_lang'); ?>
                                    </div>
                                </div>
                                <div class="entry-button">
                                    <a data-fancybox data-type="iframe" data-src="https://player.vimeo.com/video/38156752"><i aria-hidden="true" class="fa fa-play"></i>تریلر</a>
                                    <a href="<?php the_permalink(); ?>"><i aria-hidden="true" class="fa fa-exclamation"></i>جزییات</a>
                                        </div>
                                    </div>
                        </article>
                    </div>

                            <?php
                }                    
            }




                }

                // Restore original Post Data
                wp_reset_postdata();
                ?>

如果我取消注释此行exit(print_r($items,1));,我会正确看到数组。但在foreach内部,我没有看到相关的帖子。

请帮助

1 个答案:

答案 0 :(得分:0)

如果您要查找的acf字段来自HOME_ID页面/帖子,则使用$ post添加给定的行设置帖子数据,如下所示:

if ($post && $post->ID) {

setup_postdata($post); // Add this line in your code

                    // get projects
                    $posts = get_field('home_promotions',HOME_ID);