【发布时间】: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 字段
【问题讨论】:
-
您是否尝试将
<?php the_field('image', $term); ?>更改为<?php the_field('image', 'term_name_'.$term->term_id); ?>。 (用你的实际术语替换term_name_)
标签: wordpress advanced-custom-fields taxonomy