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 ) )
get_field()instead ofthe_field()here, sincethe_field()actually echoes immidiatly, so it is not used when you are trying to save a variable. also -get_the_categoryis missing its()and is normally used on post objects - you could try to useget_queried_object()to try to get the category object. - Stender