如何从自定义帖子类型检索ACF字段到此CPT的存档页面?

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

我创建了一个带有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”没有出现在我的存档页面上。我哪里错了?

谢谢。

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

如果是单个文本字段,则只需使用get_field()即可获得该字段。

尝试使用get_field()

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

还有一件事,您忘记在get_sub_field()之前添加回声。因此,请尝试使用echo

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

0
投票

get_sub_field()用于转发器字段。因此,您需要先获得父母,例如

if( have_rows('parent_field') ):
    while ( have_rows('parent_field') ) : the_row();
        $titre = get_sub_field('titre');
        // Do something...
    endwhile;
endif;
© www.soinside.com 2019 - 2024. All rights reserved.