簡體   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