Home › Forums › Backend Issues (wp-admin) › Modify HTML output of load_field
In my load field function I build and array of CPT -> Tax -> Cats like:
Array(
[CPT] => Array(
[taxonomy] => Array(
[0] => cat 1
[1] => cat 2
[taxonomy] => Array(
[0] => cat 1
[CPT] => etc....
}
with:
function tug415_post_type_categories_to_display_choices($field){
$field['choices'] = array();
$choices = array();
$args = array(
'public' => true,
'_builtin' => false,
);
$output = 'objects';
$post_types = get_post_types($args, $output);
foreach($post_types as $pt){
$args = array(
'object_type' => array($pt->name),
'public' => true,
'_builtin' => false,
);
$output = 'objects';
$taxes = get_taxonomies($args, $output);
foreach($taxes as $tax){
$args = array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => false,
'taxonomy' => $tax->name
);
$cats = get_categories($args);
$choices[$pt->label][$tax->label] = $cats;
}
}
if(is_array($choices) ){
foreach($choices as $choice){
foreach($choice as $choi){
foreach($choi as $c){
$field['choices'][$c->slug] = $c->name;
}
}
}
}
return $field;
}
add_filter('acf/load_field/name=post_type_categories_to_display', 'tug415_post_type_categories_to_display_choices');
Then the if(is_array($choices) ){...
part put’s all the category values into one ul and displays them, like it should.
What I am trying to do is modify the output so that the user, instead of seeing all the categories in one visual list (clump) be rendered like:
CPT
- Tax1: cat1, cat2, cat3
- Tax2: cat1, cat2
CPT
-Tax1: etc...
Now I can crudely do something like:
$list = '<ul>';
foreach($choices as $choice => $val){
$list .= '<li>'.$choice;
$list .= '<ul>';
foreach($val as $choic => $va){
$list .= '<li>'.$choic;
$list .= '<ul>';
foreach($va as $choi){
$list .= '<li>'.$choi->name;
$list .= '</li>';
}
$list .= '</ul>';
$list .= '</li>';
}
$list .= '</ul>';
$list .= '</li>';
}
$list .= '</ul>';
echo $list;
Then grab this output, use JS to remove the ACF generated ul and replace it with my own but, is there a function or method I’m not finding that allows me to modify the output in my load_filed function and have ACF render it how I define?
Try using a format value filter https://www.advancedcustomfields.com/resources/acf-format_value/
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We've just released ACF 5.12.3 with a security fix to prevent arbitrary file uploads to forms with ACF fields.
— Advanced Custom Fields (@wp_acf) July 14, 2022
Now that we've released this update, we recommend updating your sites as soon as possible.
© 2022 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.