簡體   English   中英

如何在主題選項中添加categori選擇選項?

[英]How to add categori selection option in theme option?

我正在嘗試使用選項樹在我的wordpress主題中添加類別選項。 我在theme-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 );
  }

}
 ?>

但是它沒有顯示任何類別。 我的錯在哪里 請告訴我。

您需要在函數中定義$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 );
  }

}
 ?>

我找到了解決方案。 只需要寫

  'type'        => 'category-select',

裝的

  'type'        => 'select',

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM