我认为这很容易,但我遇到了一个问题。
我正在建立的网站,客户有一个分类列表,其中有一个ACF图像字段和ACF描述字段。
他们想做的是有一个块,在那里他们可以从分类块中选择某些成分,然后将其呈现出来(在页面上((此时不需要链接到实际类别(,但他们想这样做,这样当成分更改描述或图像时,他们就不需要逐页更新,他们只需要在分类列表中更改它。
下面是我用来尝试从文档中获取它的代码,它不会呈现名称或原始描述,它会呈现段塞,但跳过名称,但段塞是正确的
我一直在尝试这个,但没有运气,它只返回3个li,这是正确的,但我可以获得一个名称或自定义字段。
如果我只是字段('gredients_selector'(;我拿到身份证很好,但就我的一生而言,我无法获得术语名称或附加的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
$tax = 'ingredients';
$terms = get_terms( $tax, $args = array(
'hide_empty' => false, // do not hide empty terms
));
foreach( $terms as $term ) {
$term_link = get_term_link( $term );
$image = get_field('image', 'ingredients_' . $term->term_id );
$description = get_field('description', 'ingredients_' . $term->term_id );
if( $term->count > 0 ) {
echo '<a href="' . esc_url( $term_link ) . '">';
echo '<img src="' . $image['url'] . '" alt="' . $image['alt'] .'">';
echo $term->name .'</a>';
echo $description;
} elseif( $term->count !== 0 ) {
echo '' . $term->name .'';
}
}
?>
试试这个,用实际的术语段塞:代替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; ?>
您可以在此处找到有关此的更多信息。