简体   繁体   English

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

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

I am using an ACF Wysiwyg Editor field in order to be able to write some extra text for my category pages.我正在使用ACF 所见即所得编辑器字段,以便能够为我的类别页面编写一些额外的文本。 I am echoing the text using category.php我正在使用 category.php 回显文本

At first I thought I could just echo out get_field but found out that I need to pass in the category.起初我以为我可以只回显 get_field 但发现我需要传入类别。 Therefore I've used get_the_category().因此我使用了 get_the_category()。 But for some reason code only works for child categories:但出于某种原因,代码仅适用于子类别:

example.com/category/parent/child/ <- works example.com/category/parent/ <- doesn't work example.com/category/parent/child/ <- 有效 example.com/category/parent/ <- 无效

I've also tried with wp_get_post_categories();我也试过 wp_get_post_categories(); but no luck.但没有运气。 What am I missing ??我错过了什么?

<?php

$category = get_the_category();

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

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

I've tried print_r($category) where both child and parent category gave me an array:我试过 print_r($category) ,其中子类别和父类别都给了我一个数组:

Child 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 ) )

Parent category父类别

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 ) )

Going through your code, it seems to be fine but need to check whether we are getting values for every variable or not, whether the acf fields are empty or not?查看您的代码,似乎没问题,但需要检查我们是否正在获取每个变量的值,acf 字段是否为空?

Below is the example from official site of ACF.以下是来自 ACF 官方网站的示例。 In your code, you can make sure you are getting proper object of second argument you are passing.在你的代码中,你可以确保你得到了你传递的第二个参数的正确对象。

 // 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