【问题标题】:Get ACF Taxonomy selection in Block and display fields在块和显示字段中获取 ACF 分类选择
【发布时间】:2022-10-13 18:07:05
【问题描述】:

认为这很容易,但我遇到了一个问题。

我正在构建的网站,客户端有一个分类列表,其中包含一个 ACF 图像字段和 ACF 描述字段。

他们想要做的是有一个块,他们可以从分类块中选择某些成分,然后将其格式化(在页面上)(此时它不需要链接到实际类别)但他们想做这样一来,当成分更改描述或图像时,他们不需要逐页更新,他们只需在分类列表中进行更改即可。

下面是我用来尝试从文档中获取它的代码,它不会呈现名称或原始描述它会呈现 slug 但跳过名称但 slug 是正确的

我一直在尝试这个没有运气,它只返回 3 li,这是正确的,但我可以获得一个名称或自定义字段来通过。

如果我只是 the_field('ingredients_selector');我得到 ID 就好了但是对于我的生活,我无法获得术语名称或附加到它的 ACF 字段/

$terms = get_field('ingredients_selector');
if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
    echo '<ul class="ingredients-list">';
    foreach ( $terms as $term ) {
        echo '<li class="ingredients-list__item">' . $term->name . '</li>'; ?>
        <p>Description: <?php the_field('description', $term); ?></p>
        <p>Image: <?php the_field('image', $term); ?></p>
    <?php }
    echo '</ul>';
}
?>

我也尝试过这种方式,这给了我相同的结果,但工作的蛞蝓,它会再次跳过术语名称,但“查看全部”将至少链接

<?php
$terms = get_field('ingredients_selector');
if( $terms ): ?>
<ul>
<?php foreach( $terms as $term ): ?>
<li>
<h2><?php echo esc_html( $term->name ); ?></h2>
<p>Term description: <?php the_field('description', $term); ?></p>
<p>Term Image: <?php the_field('image', $term); ?></p>
<p><?php echo esc_html( $term->description ); ?></p>
<a href="<?php echo esc_url( get_term_link( $term ) ); ?>">View all '<?php echo esc_html( $term->name ); ?>' posts</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>

附件是我设置的 ACF 字段

【问题讨论】:

  • 您是否尝试将&lt;?php the_field('image', $term); ?&gt; 更改为&lt;?php the_field('image', 'term_name_'.$term-&gt;term_id); ?&gt;。 (用你的实际术语替换term_name_

标签: wordpress advanced-custom-fields taxonomy


【解决方案1】:

试试这个并用你的实际术语 slug 替换 term_name_

<?php
$terms = get_field('ingredients_selector');
if( $terms ): ?>
<ul>
    <?php foreach( $terms as $term ): ?>
    <li>
        <h2><?php echo esc_html( $term->name ); ?></h2>
        <p>Term description: <?php the_field('description', 'term_name_'.$term->term_id); ?></p>
        <p>Term Image: <?php the_field('image', 'term_name_'.$term->term_id); ?></p>
        <p><?php echo esc_html( $term->description ); ?></p>
        <a href="<?php echo esc_url( get_term_link( $term ) ); ?>">View all '<?php echo esc_html( $term->name ); ?>' posts</a>
    </li>
    <?php endforeach; ?>
</ul>
<?php endif; ?>

【讨论】:

    猜你喜欢
    • 2017-08-18
    • 1970-01-01
    • 2019-11-17
    • 2021-10-06
    • 2018-11-25
    • 2015-11-21
    • 1970-01-01
    • 2019-05-22
    • 2018-11-15
    相关资源
    最近更新 更多