从自定义字段wordpress打印图像


Print out images from custom field wordpress

我有一个名为images的自定义字段,我试图循环浏览我的自定义帖子,每个帖子打印出3张图片。我已经在一个数组中启动了它们,但每当我试图打印该区域的内容时,我都会得到奇怪的输出,比如复制的图像。我尝试了一个简单的while循环,但它没有将其限制为3,而是打印出所附的每个图像。

<?php $i=0; while ($page_query->have_posts()): $page_query->the_post(); ?> 
        <section class="featured-block">
            <div class="container">
                <div class="row">
                    <div class="col-md-12">
                        <button class="title-button center-block"><?php echo get_the_title(); ?></button>
                    </div>
                    <!-- end of col md 12 -->
                </div>
                <div class="row home-img-row">
                    <div class="col-md-4 <?php print the_ID(); ?>">
                    <?php $meta_values = get_post_meta( $post->ID, 'images');
                    $i = 0; ?>
                    <? while ($i < 3) {
                        print($meta_values[$i]);
                        $i++;
                    }?>

调试$meta_values变量可能会有所帮助。此外,保持PHP开放标签的一致性(<?php是非常可取的)。您可能还想对数组执行foreach循环,而不是while循环。

<?php
$meta_values = get_post_meta( $post->ID, 'images');
var_dump($meta_values); // Debug
foreach ($meta_values as $value) {
    echo $value;
}
?>