Wordpress 如何将ACF字段从自定义post类型检索到此CPT的存档页?

Wordpress 如何将ACF字段从自定义post类型检索到此CPT的存档页?,wordpress,advanced-custom-fields,Wordpress,Advanced Custom Fields,我创建了一个自定义的帖子类型,里面有ACF字段。一切都很好,但当我想创建archive-cpt.php页面时,我无法在该页面上看到我的ACF字段 以下是我的存档页面代码: <?php get_header(); ?> <main role="main"> <!-- section --> <section> <h1><?php _e( 'Archives', 'trad' ); ?></h1&g

我创建了一个自定义的帖子类型,里面有ACF字段。一切都很好,但当我想创建archive-cpt.php页面时,我无法在该页面上看到我的ACF字段

以下是我的存档页面代码:

<?php get_header(); ?>

<main role="main">
    <!-- section -->
    <section>

  <h1><?php _e( 'Archives', 'trad' ); ?></h1>

  <div class="container">
  <div class="row"> 

  <?php if (have_posts()): while (have_posts()) : the_post(); ?>
  <div class="col-lg-4 mx-auto">

  <!-- article -->

  <h2 class="titre-extranet-article">


        <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
        </h2>

<li>

    <?php  // display a sub field value
    get_sub_field('titre'); ?></li>
    </div>

    <!-- /post thumbnail -->

    <?php endwhile; ?>

    <?php else: ?>

    <!-- article -->
    <article>
    <h2><?php _e( 'Sorry, nothing to display.', 'trad' ); ?></h2>
    </article>
    <!-- /article -->
    <?php endif; ?>

    </div>
    </div>
        <?php get_template_part('pagination'); ?>

    </section>
    <!-- /section -->
    </main>


    <?php get_footer(); ?>

  • 子字段“titre”未出现在我的存档页面上。 我错在哪里

    谢谢。

    get\u sub\u field()用于转发器字段。所以你需要先找到父母

    if( have_rows('parent_field') ):
        while ( have_rows('parent_field') ) : the_row();
            $titre = get_sub_field('titre');
            // Do something...
        endwhile;
    endif;
    

    如果该字段是单个文本字段,则只需使用
    get\u field()
    即可获取该字段

    尝试使用
    get\u field()

    <?php echo get_field('titre'); ?>
    
    <?php echo get_sub_field( 'titre' ); ?>