2013-12-02 2 views
0

Я пытаюсь добавить параметр категории в мою тему Wordpress, используя дерево вариантов. Я использую этот код в теме-option.phpКак добавить опцию выбора категории в теме?

<?php 
    $categories = get_categories('hide_empty=0&orderby=name'); 
    $wp_cats = array(); 
    foreach ($categories as $category_list) { 
    $wp_cats[$category_list->cat_ID] = $category_list->cat_name; 
    } 


add_action('admin_init', 'custom_theme_options', 1); 

function custom_theme_options() { 

    $saved_settings = get_option('option_tree_settings', array()); 


    $custom_settings = array(
    'sections'  => array(
     array(
     'id'   => 'general', 
     'title'  => 'Home Page Settings' 
    ) 
    ), 
    'settings'  => array(

     array(
     'id'   => 'Great-Product', 
     'label'  => 'Great Product', 
     'desc'  => 'select great Product category', 
     'type'  => 'select', 
     'section'  => 'general', 

    'std' => 'Choose a category', 
    'options' => $wp_cats 
    ) 
    ) 
); 

    if ($saved_settings !== $custom_settings) { 
    update_option('option_tree_settings', $custom_settings); 
    } 

} 
?> 

Но он не показывает ни одной категории. Где моя вина? Пожалуйста, скажите мне.

+0

Где ваш '$ wp_cats' определено? –

ответ

0

Я нашел решение. Просто нужно написать

'type'  => 'category-select', 

Insted из

'type'  => 'select', 
0

Вам нужно определить $wp_cats переменную внутри вашей функции, или использовать global, чтобы привести его в.

Вариант 1 (глобальная переменная)

<?php 
    $categories = get_categories('hide_empty=0&orderby=name'); 
    $wp_cats = array(); 
    foreach ($categories as $category_list) { 
    $wp_cats[$category_list->cat_ID] = $category_list->cat_name; 
    } 


add_action('admin_init', 'custom_theme_options', 1); 

function custom_theme_options() { 

    global $wp_cats; /** GLOBAL!! */ 

    $saved_settings = get_option('option_tree_settings', array()); 


    $custom_settings = array(
    'sections'  => array(
     array(
     'id'   => 'general', 
     'title'  => 'Home Page Settings' 
    ) 
    ), 
    'settings'  => array(

     array(
     'id'   => 'Great-Product', 
     'label'  => 'Great Product', 
     'desc'  => 'select great Product category', 
     'type'  => 'select', 
     'section'  => 'general', 

    'std' => 'Choose a category', 
    'options' => $wp_cats 
    ) 
    ) 
); 

    if ($saved_settings !== $custom_settings) { 
    update_option('option_tree_settings', $custom_settings); 
    } 

} 
?> 

Вариант 2 (построить переменную внутри функции)

<?php 

add_action('admin_init', 'custom_theme_options', 1); 

function custom_theme_options() { 

    $categories = get_categories('hide_empty=0&orderby=name'); 
    $wp_cats = array(); 
    foreach ($categories as $category_list) { 
    $wp_cats[$category_list->cat_ID] = $category_list->cat_name; 
    } 


    $saved_settings = get_option('option_tree_settings', array()); 


    $custom_settings = array(
    'sections'  => array(
     array(
     'id'   => 'general', 
     'title'  => 'Home Page Settings' 
    ) 
    ), 
    'settings'  => array(

     array(
     'id'   => 'Great-Product', 
     'label'  => 'Great Product', 
     'desc'  => 'select great Product category', 
     'type'  => 'select', 
     'section'  => 'general', 

    'std' => 'Choose a category', 
    'options' => $wp_cats 
    ) 
    ) 
); 

    if ($saved_settings !== $custom_settings) { 
    update_option('option_tree_settings', $custom_settings); 
    } 

} 
?> 
+0

Без изменений. тот же результат – Bir

+0

Перед использованием переменной '$ wp_cats' (т.е. перед началом строки' $ custom_settings = array (...) ') почему бы вам не поместить' var_dump ($ wp_cats); 'который выведет содержимое переменной $ wp_cats. Если переменная определена и включена правильно, проблем не должно быть. Мы знаем, что теперь это включено. Поэтому проблема, вероятно, связана с заполнением этой переменной. (шаг за шагом цикла $ foreach()) –

Смежные вопросы