gpt4 book ai didi

ajax - Foreach 循环遍历 cpt 类别以使用 ACF 值

转载 作者:行者123 更新时间:2023-12-04 08:35:55 26 4
gpt4 key购买 nike

我正在遍历我的 CPT 类别,并且我想在每个循环中使用我的 ACF 字段。

 <div id="response">
<?php
$args = array(
'post_type' => 'segments-overview',
'orderby' => 'date', // we will sort posts by date
'posts_per_page' => -1
);



$query = new WP_Query( $args );
$all_terms = [];
if( $query->have_posts() ) :
while( $query->have_posts() ): $query->the_post();

$terms = get_the_terms(get_the_ID(), 'category-segments-overview');
foreach($terms as $term) $all_terms[$term->term_id] = $term;

endwhile;

foreach($terms as $term):
?>

<div class="segments-card">
<div class="img">image</div>
<div class="content">
<div class="title"><?php echo $term->name; ?></div>

// ACF field
<?php echo the_field('product', $term->taxonomy) ; ?>

<a class="button transparent" href="/segments/<?php echo $term->slug; ?>">
<?php echo __('View All','axia'); ?>
</a>
</div>
</div>
</div>

<?php endforeach;

wp_reset_postdata();
else :
?>
<div class="no-posts-found">
<h2>There were no items found</h2>
<h3>Please try a different search</h3>
</div>
<?php
endif;
?>
</div>
谁能告诉我如何在 foreach 循环中检索我的“产品”字段的值。
现在我没有收到任何错误,但我也没有收到任何值(value)。
请帮忙。

最佳答案

您必须学习有关分类术语字段的文档:
https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/
在你的情况下,你可以更换

echo the_field('product', $term->taxonomy);
通过(3 种可能性)
echo the_field('product', $term);
// or
echo the_field('product', $term->taxonomy . '_' . $term->term_id);
// or
echo the_field('product', 'term_' . $term->term_id);
如何从特定术语加载字段?
基本上来自 acf 的加载字段是: get_field('key', $post_id)对于分类术语,有 3 种不同风格的 $post_id可用并在下面列出。 (这是来自acf文档)
+----------------+----------------------------+-----------------------------------------------------------------------------+
| Example | Format | Description |
+----------------+----------------------------+-----------------------------------------------------------------------------+
| 'category_123' | $taxonomy . '_' . $term_id | A string containing the taxonomy name and term ID |
+----------------+----------------------------+-----------------------------------------------------------------------------+
| 'term_123' | 'term_' . $term_id | 'term_' . $term_id |
| | | A string containing the word ‘term’ and term ID. Added in version 5.5.0 |
+----------------+----------------------------+-----------------------------------------------------------------------------+
| WP_Term | $term | A term object. You can get a term object via many of WP’s functions such as |
| | | |
| | | get_term() |
+----------------+----------------------------+-----------------------------------------------------------------------------+

关于ajax - Foreach 循环遍历 cpt 类别以使用 ACF 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64800312/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com