繁体   English   中英

WordPress/ACF: get_category(); 对于父类别

[英]WordPress/ACF: get_category(); for parent category

我正在使用ACF 所见即所得编辑器字段,以便能够为我的类别页面编写一些额外的文本。 我正在使用 category.php 回显文本

起初我以为我可以只回显 get_field 但发现我需要传入类别。 因此我使用了 get_the_category()。 但出于某种原因,代码仅适用于子类别:

example.com/category/parent/child/ <- 有效 example.com/category/parent/ <- 无效

我也试过 wp_get_post_categories(); 但没有运气。 我错过了什么?

<?php

$category = get_the_category();

$extra_text = get_field('ekstra_tekstfelt', $category[0] );
?>

<p> <?php echo $extra_text ?></p>

我试过 print_r($category) ,其中子类别和父类别都给了我一个数组:

儿童类别

Array ( [0] => WP_Term Object ( [term_id] => 27 [name] => Tilbehør [slug] => tilbehor [term_group] => 0 [term_taxonomy_id] => 27 [taxonomy] => category [description] => [parent] => 1 [count] => 31 [filter] => raw [cat_ID] => 27 [category_count] => 31 [category_description] => [cat_name] => Tilbehør [category_nicename] => tilbehor [category_parent] => 1 ) )

父类别

Array ( [0] => WP_Term Object ( [term_id] => 46 [name] => Bagværk [slug] => bagvaerk [term_group] => 0 [term_taxonomy_id] => 46 [taxonomy] => category [description] => [parent] => 1 [count] => 16 [filter] => raw [cat_ID] => 46 [category_count] => 16 [category_description] => [cat_name] => Bagværk [category_nicename] => bagvaerk [category_parent] => 1 ) [1] => WP_Term Object ( [term_id] => 16 [name] => Dessert [slug] => dessert [term_group] => 0 [term_taxonomy_id] => 16 [taxonomy] => category [description] => [parent] => 1 [count] => 22 [filter] => raw [cat_ID] => 16 [category_count] => 22 [category_description] => [cat_name] => Dessert [category_nicename] => dessert [category_parent] => 1 ) )

查看您的代码,似乎没问题,但需要检查我们是否正在获取每个变量的值,acf 字段是否为空?

以下是来自 ACF 官方网站的示例。 在你的代码中,你可以确保你得到了你传递的第二个参数的正确对象。

 // get the current taxonomy term
 $term = get_queried_object(); // or other way where you get the category term object
 // example
 $image = get_field('image', $term);
 $color = get_field('color', $term);
 echo $color;
 echo $image;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM