Wordpress ACF 选择:动态使用类别作为选择时仅返回第一个对象
Wordpress ACF selection: Only first object returned when dynamically using categories as choices
我正在尝试通过动态获取 Post 类别来修改 ACF selection 字段,以便用户可以 select 在不同的类别之间(它用于博客页面模板)。我正在使用ACF的函数,现在只返回第一个类别。
这是我在 functions.php
中得到的:
函数acf_load_color_field_choices($字段){
function categoryName() {
$args = array(
'type' => 'post',
'taxonomy' => 'category',
'parent' => 0, // get top level categories
'orderby' => 'name',
'order' => 'ASC',
'hierarchical' => 1,
'pad_counts' => 0
);
$categories = get_categories( $args );
foreach ( $categories as $category ){
echo $category->name;
}
}
// reset choices
$field['choices'] = array();
// get the textarea value from options page without any formatting
$choices = categoryName();
// explode the value so that each line is a new array piece
$choices = explode("\n", $choices);
// remove any unwanted white space
$choices = array_map('trim', $choices);
// loop through array and add to field 'choices'
if( is_array($choices) ) {
foreach( $choices as $choice ) {
$field['choices'][ $choice ] = $choice;
}
}
// return the field
return $field;
}
add_filter('acf/load_field/name=blog_by_cat', 'acf_load_color_field_choices');
我也试过:
function acf_load_cat_field_choices( $field ) {
// reset choices
$field['choices'] = array();
// get the textarea value from options page without any formatting
$choices = get_categories();
$value = get_categories();
// explode the value so that each line is a new array piece
$choices = explode("\n", $choices);
// remove any unwanted white space
$choices = array_map('trim', $choices);
// loop through array and add to field 'choices'
if( is_array($choices) ) {
foreach( $choices as $choice ) {
$field['choices'][ $value->slug ] = $choice->name;
}
}
// return the field
return $field;
这个returns没什么
并且:
function acf_load_cat_field_choices( $field ) {
// reset choices
$field['choices'] = array();
// get the textarea value from options page without any formatting
$choices = get_categories();
// explode the value so that each line is a new array piece
$choices = explode("\n", $choices);
// remove any unwanted white space
$choices = array_map('trim', $choices);
// loop through array and add to field 'choices'
if( is_array($choices) ) {
foreach( $choices as $choice ) {
$field['choices'][$choice] = $choice;
}
}
// return the field
return $field;
}
这个returns"Array"(一次)
categoryName()
没有 returning 任何东西。与其回显 $category->name
,不如将它们打包到一个数组中,然后 return 那个数组?
所以我找到了正确执行此操作的方法:
函数acf_load_cat_field_choices($字段){
// reset choices
$field['choices'] = array();
// get the textarea value from options page without any formatting
$getCatSlugs = array('all_posts');
$getCatNames = array('All Posts');
$args = array(
'type' => 'post',
'taxonomy' => 'category',
'parent' => 0, // get top level categories
'orderby' => 'name',
'order' => 'ASC',
'hierarchical' => 1,
'pad_counts' => 0
);
$categories = get_categories( $args );
foreach ( $categories as $category ){
$getCatSlugs[] = $category->slug;
$getCatNames[] = $category->name;
}
$values = implode("\n", $getCatSlugs);
$labels = implode("\n", $getCatNames);
$values = explode("\n", $values);
$labels = explode("\n", $labels);
// loop through array and add to field 'choices'
foreach( array_combine($values, $labels ) as $value => $label ) {
$field['choices'][ $value ] = $label;
}
// return the field
return $field;
}
add_filter('acf/load_field/name=post_by_cat', 'acf_load_cat_field_choices');
我正在尝试通过动态获取 Post 类别来修改 ACF selection 字段,以便用户可以 select 在不同的类别之间(它用于博客页面模板)。我正在使用ACF的函数,现在只返回第一个类别。
这是我在 functions.php
中得到的:
函数acf_load_color_field_choices($字段){
function categoryName() {
$args = array(
'type' => 'post',
'taxonomy' => 'category',
'parent' => 0, // get top level categories
'orderby' => 'name',
'order' => 'ASC',
'hierarchical' => 1,
'pad_counts' => 0
);
$categories = get_categories( $args );
foreach ( $categories as $category ){
echo $category->name;
}
}
// reset choices
$field['choices'] = array();
// get the textarea value from options page without any formatting
$choices = categoryName();
// explode the value so that each line is a new array piece
$choices = explode("\n", $choices);
// remove any unwanted white space
$choices = array_map('trim', $choices);
// loop through array and add to field 'choices'
if( is_array($choices) ) {
foreach( $choices as $choice ) {
$field['choices'][ $choice ] = $choice;
}
}
// return the field
return $field;
}
add_filter('acf/load_field/name=blog_by_cat', 'acf_load_color_field_choices');
我也试过:
function acf_load_cat_field_choices( $field ) {
// reset choices
$field['choices'] = array();
// get the textarea value from options page without any formatting
$choices = get_categories();
$value = get_categories();
// explode the value so that each line is a new array piece
$choices = explode("\n", $choices);
// remove any unwanted white space
$choices = array_map('trim', $choices);
// loop through array and add to field 'choices'
if( is_array($choices) ) {
foreach( $choices as $choice ) {
$field['choices'][ $value->slug ] = $choice->name;
}
}
// return the field
return $field;
这个returns没什么
并且:
function acf_load_cat_field_choices( $field ) {
// reset choices
$field['choices'] = array();
// get the textarea value from options page without any formatting
$choices = get_categories();
// explode the value so that each line is a new array piece
$choices = explode("\n", $choices);
// remove any unwanted white space
$choices = array_map('trim', $choices);
// loop through array and add to field 'choices'
if( is_array($choices) ) {
foreach( $choices as $choice ) {
$field['choices'][$choice] = $choice;
}
}
// return the field
return $field;
}
这个returns"Array"(一次)
categoryName()
没有 returning 任何东西。与其回显 $category->name
,不如将它们打包到一个数组中,然后 return 那个数组?
所以我找到了正确执行此操作的方法:
函数acf_load_cat_field_choices($字段){
// reset choices
$field['choices'] = array();
// get the textarea value from options page without any formatting
$getCatSlugs = array('all_posts');
$getCatNames = array('All Posts');
$args = array(
'type' => 'post',
'taxonomy' => 'category',
'parent' => 0, // get top level categories
'orderby' => 'name',
'order' => 'ASC',
'hierarchical' => 1,
'pad_counts' => 0
);
$categories = get_categories( $args );
foreach ( $categories as $category ){
$getCatSlugs[] = $category->slug;
$getCatNames[] = $category->name;
}
$values = implode("\n", $getCatSlugs);
$labels = implode("\n", $getCatNames);
$values = explode("\n", $values);
$labels = explode("\n", $labels);
// loop through array and add to field 'choices'
foreach( array_combine($values, $labels ) as $value => $label ) {
$field['choices'][ $value ] = $label;
}
// return the field
return $field;
}
add_filter('acf/load_field/name=post_by_cat', 'acf_load_cat_field_choices');