2016-10-28 7 views
0

Я пытаюсь изменить поле выбора ACF, динамически выбирая категории Post, чтобы пользователь мог выбирать между разными (это шаблон страницы в блоге). Я использую функцию в соответствии с ACF, прямо сейчас возвращается только первая категория.Выбор Wordpress 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; 

Это ничего не возвращает

И:

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; 

} 

Это возвращает "Array" (один раз)

ответ

0

Так что я нашел способ сделать это правильно:

функции 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/имя = post_by_cat', ' acf_load_cat_field_choices');

0

categoryName() это ничего не возвращает. Вместо эха $category->name вы должны упаковать их в массив и вернуть этот массив?

+0

Thanx, есть ли у вас пример кода? –