I have a website with several categories and now I'm implementing the languages and I need to add a custom field to category page but I can't get the field.
Next I go to category page and introduce the text in each language.
In my code I do this:
<?php
$obj = get_queried_object();
$ar = array('child_of' => $obj->term_id);
$categories = get_categories( $ar );
foreach($categories as $category) {
$custom_field = get_field('descricao_traducoes', $obj->term_id);
var_dump($custom_field);
}
?>
But returns me null
.
How can I do that?
I think you're just passing the wrong variable as a second parameter in get_field
!
Do $category->term_id
instead.
(Unless you meant to dump the descricao_traducoes
of the queried object for as many times as there are categories)
<?php
$obj = get_queried_object();
$ar = array('child_of' => $obj->term_id);
$categories = get_categories( $ar );
foreach($categories as $category) {
$custom_field = get_field('descricao_traducoes', $category->term_id);
var_dump($custom_field);
}
From the ACF docs :
get_field($selector, [$post_id], [$format_value]);
$selector
(string) (Required) The field name or field key.$post_id
(mixed) (Optional) The post ID where the value is saved. Defaults to the current post.$format_value
(bool) (Optional) Whether to apply formatting logic. Defaults to true.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.