0
votes

I am using an ACF Wysiwyg Editor field in order to be able to write some extra text for my category pages. I am echoing the text using category.php

At first I thought I could just echo out get_field but found out that I need to pass in the category. Therefore I've used 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

I've also tried with 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:

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 ) )
1
First thing to notice, is that you should use get_field() instead of the_field() here, since the_field() actually echoes immidiatly, so it is not used when you are trying to save a variable. also - get_the_category is missing its () and is normally used on post objects - you could try to use get_queried_object() to try to get the category object. - Stender
also - are you sure that the parent cat has the field filled out? and is it using the same cat file? or is it using archive.php? - Stender
Thank you, Stender. My appologies. I was using get_field and get_the_category();... get_queried_object broke site and gave me error. I've just tried with a simple echo on the category.php and it came out on both the parent and child cat. Yes, the parent cat has the field filled out. - TurboTobias
Figured out why get_queried_object(); was breaking site... Since I was using $category[0] - Deleted [0] and it gave me same result: works for child category but not parent. - TurboTobias

1 Answers

0
votes

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?

Below is the example from official site of 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;