繁体   English   中英

如何在 WordPress 循环中获取 ACF 字段值?

[英]How can I get ACF field value in WordPress loop?

我正在尝试为循环中的帖子获取 ACF 归档值。 但由于某种原因,该值未显示。

我已经试过了

<?php $field = get_field('field_name'); echo $field;  ?>

<?php the_field('field_name', $post->ID); ?>

这些方法都不起作用。 请参阅下面的循环代码:

<?php 
    $args = array( 
    'post_type' => 'post',
    'posts_per_page' => 4,
    $the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    <div class="col-sm-6">

        <h2 class="the-title"><?php the_field('field_name', $post->ID); ?> +  <?php the_title() ;?> </h2>


    </div>

<?php endwhile; else: ?> Nothing here <?php endif; ?>
<?php wp_reset_query(); ?>

如何在循环中获取 ACF 字段值?

尝试使用

the_field('field_name', get_the_ID());

您没有正确关闭数组。请试试这个

<?php 
        $args = array( 
        'post_type' => 'post',
        'posts_per_page' => 4
       );
        $the_query = new WP_Query( $args );
    ?>
    <?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
        <div class="col-sm-6">

            <h2 class="the-title"><?php the_field('field_name', $post->ID); ?> +  <?php the_title() ;?> </h2>


        </div>

    <?php endwhile; else: ?> Nothing here <?php endif; ?>
    <?php wp_reset_query(); ?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM