Wordpress使用“高级自定义字段关系”返回选定的值

问题描述 投票:0回答:1

我有一个自定义字段,它是一个选择框,类型是关系。我通过帖子过滤关系,现在我想在特定页面上显示所选帖子:

这是我的查询:

$ids = get_field('choose_cars', false, false); // name of the select box

$query = new WP_Query(array(
    'post_type'         => 'cars', // is my custom post type
    'posts_per_page'    => 3,
    'post__in'          => $ids,
    'post_status'       => 'publish',
    'orderby'           => 'post__in',
));

$query = new WP_Query($args);

 if($query->have_posts()) {
    while($query->have_posts() ){
        $query->the_post();
        ?>

    <?php $image = get_field('car_image'); ?>
    <div>
        <a href="<?php the_permalink(); ?>">
            <div class="car-wrapper" style="position: relative;">
                <div class="car-image" style="background-image:url('<?php echo $image['url']; ?>')"></div>
                <div class="overlay">
                    <div class="content">
                            <p><?= get_field('car_name') ?></p>
                    </div>
                </div>
            </div>
        </a>
    </div>
    <?php }
}
die();

什么都没发生,所以也许有人可以告诉我我做错了什么?

php wordpress advanced-custom-fields
1个回答
1
投票

忽略我的评论(虽然它总是值得指出)问题是你正在定义$query然后立即覆盖它

$query = new WP_Query(array(...));

$query = new WP_Query($args);

所以,将第一个更改为

$args = array(
    'post_type'         => 'cars', // is my custom post type
    'posts_per_page'    => 3,
    'post__in'          => $ids,
    'post_status'       => 'publish',
    'orderby'           => 'post__in',
);

它应该工作(假设这是根据我的评论的字段数据的帖子!)

© www.soinside.com 2019 - 2024. All rights reserved.