php – 如何使选择列表的输出依赖于父列表?

前端之家收集整理的这篇文章主要介绍了php – 如何使选择列表的输出依赖于父列表?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有两个具有父类别和子类别的数组,每个数组都出现在一个选择列表中,如何使子类别仅显示父类别中的项目?
  1. <?PHP $carMakes = array(
  2. 'show_option_all' => '','show_option_none' => ('All Makes'),'orderby' => 'ID','order' => 'ASC','show_count' => 0,'hide_empty' => 1,'child_of' => 25,'exclude' => 0,'echo' => 1,'selected' => 0,'hierarchical' => 0,'name' => 'cat','id' => '','class' => 'postform','depth' => 0,'tab_index' => 0,'taxonomy' => 'category','hide_if_empty' => false
  3. ); ?>
  4.  
  5. <?PHP $carModels = array(
  6. 'name' => 'subcat','hierarchical' => 1,'parent' => get_cat_id('model'),'show_option_none' => ('All Models'),'hide_empty' => 0 );
  7. ?>
  8.  
  9. <?PHP wp_dropdown_categories($carMakes); ?>
  10.  
  11. <?PHP wp_dropdown_categories($carModels); ?>

例如,只需要展示属于汽车的汽车模型

  1. Make=Toyota Model=Supra
  2. Model=Corolla
  3. Model=Tundra

以下是类别结构的示例

  1. Make (parent category)
  2. -Toyota
  3. -Nissan
  4. -Mazda
  5. -Ford
  6.  
  7. Model (parent category)
  8. -Supra
  9. -Skyline
  10. -Mustang
  11. -Rx7
  12. -Corolla
一直想使用Ajax对链式选择进行练习,所以,在这里我们去;)

这是一个完整的插件,应该安装在wp-content / plugins / your-plugin-name文件夹中.由三个文件组成,插件本身,Javascript文件和Ajax加载器映像.

安装插件并激活,并在以下主题模板文件中插入以下内容

  1. <?PHP
  2. if( class_exists( 'BRSFL_Chained_Selection' ) ) {
  3. // Parameters: ( $cat_id,$dropdown_text )
  4. BRSFL_Chained_Selection::print_cats( 1,'All Makes' );
  5. }
  6. ?>

另外,根据需要调整两次调用wp_dropdown_categories.查看代码注释以获取详细信息.

子类别下拉列表会根据类别下拉列表中的更改进行修改

链-categories.PHP

  1. <?PHP
  2. /**
  3. * Plugin Name: Chained Categories
  4. * Plugin URI: https://stackoverflow.com/q/15748968/1287812
  5. * Description: Demonstration of chained categories with Ajax.
  6. * Plugin structure based on <a href="https://gist.github.com/3804204">Plugin Class Demo</a>,by Thomas Scholz.
  7. * Use the dropdowns in the theme with this PHP method call: BRSFL_Chained_Selection::print_cats();
  8. * Author: Rodolfo Buaiz
  9. * Author URI: http://wordpress.stackexchange.com/users/12615/brasofilo
  10. */
  11.  
  12. add_action(
  13. 'plugins_loaded',array ( BRSFL_Chained_Selection::get_instance(),'plugin_setup' )
  14. );
  15.  
  16. class BRSFL_Chained_Selection
  17. {
  18. /**
  19. * Plugin instance.
  20. *
  21. * @see get_instance()
  22. * @type object
  23. */
  24. protected static $instance = NULL;
  25.  
  26. /**
  27. * URL to this plugin's directory.
  28. *
  29. * @type string
  30. */
  31. public $plugin_url = '';
  32.  
  33. /**
  34. * Path to this plugin's directory.
  35. *
  36. * @type string
  37. */
  38. public $plugin_path = '';
  39.  
  40. /**
  41. * Access this plugin’s working instance
  42. *
  43. * @wp-hook plugins_loaded
  44. * @since 2012.09.13
  45. * @return object of this class
  46. */
  47. public static function get_instance()
  48. {
  49. NULL === self::$instance and self::$instance = new self;
  50. return self::$instance;
  51. }
  52.  
  53. /**
  54. * Used for regular plugin work.
  55. *
  56. * @wp-hook plugins_loaded
  57. * @since 2012.09.10
  58. * @return void
  59. */
  60. public function plugin_setup()
  61. {
  62. $this->plugin_url = plugins_url( '/',__FILE__ );
  63. $this->plugin_path = plugin_dir_path( __FILE__ );
  64. $this->load_language( 'chainedselections' );
  65.  
  66. add_action( 'wp_enqueue_scripts',array( $this,'script_enqueuer' ) );
  67. add_action( 'wp_ajax_custom_query','custom_query' ) );
  68. add_action( 'wp_ajax_nopriv_custom_query','custom_query' ) );
  69. }
  70.  
  71. /**
  72. * Constructor. Intentionally left empty and public.
  73. *
  74. * @see plugin_setup()
  75. * @since 2012.09.12
  76. */
  77. public function __construct() {}
  78.  
  79. /**
  80. * Enqueue frontend scripts
  81. */
  82. public function script_enqueuer()
  83. {
  84. wp_register_script(
  85. 'ajax-quote',plugin_dir_url( __FILE__ ) . '/ajax.js',array( 'jquery' )
  86. );
  87.  
  88. wp_enqueue_script( 'ajax-quote' );
  89.  
  90. wp_localize_script(
  91. 'ajax-quote','wp_ajax',array(
  92. 'ajaxurl' => admin_url( 'admin-ajax.PHP' ),'ajaxnonce' => wp_create_nonce( 'ajax_chained_selection_validate' ),'icon' => plugin_dir_url( __FILE__ ) . '/ajax-loader.gif'
  93. )
  94. );
  95. }
  96.  
  97. /**
  98. * Ajax create sub-categories dropdown
  99. */
  100. public function custom_query()
  101. {
  102. // Security
  103. check_ajax_referer( 'ajax_chained_selection_validate','security' );
  104.  
  105. // Check if jQuery posted the data
  106. if( !isset( $_POST[ 'chained_subcat_id' ] ) )
  107. return false;
  108.  
  109. // Adjust parameters
  110. $carMakes = array(
  111. 'show_option_all' => '','show_option_none' => 'All ' . $_POST[ 'chained_subcat_name' ],'hide_empty' => 0,'child_of' => $_POST[ 'chained_subcat_id' ],'name' => 'chained-subcontainer','depth' => 1,'hide_if_empty' => false
  112. );
  113.  
  114. // Print sub-categories
  115. wp_dropdown_categories( $carMakes );
  116. exit();
  117. }
  118.  
  119. /**
  120. * Loads translation file.
  121. *
  122. * Accessible to other classes to load different language files (admin and
  123. * front-end for example).
  124. *
  125. * @wp-hook init
  126. * @param string $domain
  127. * @since 2012.09.11
  128. * @return void
  129. */
  130. public function load_language( $domain )
  131. {
  132. $locale = apply_filters( 'plugin_locale',get_locale(),$domain );
  133.  
  134. // Load translation from wp-content/languages if exist
  135. load_textdomain(
  136. $domain,WP_LANG_DIR . $domain . '-' . $locale . '.mo'
  137. );
  138.  
  139. // Load regular plugin translation
  140. load_plugin_textdomain(
  141. $domain,FALSE,$this->plugin_path . '/languages'
  142. );
  143. }
  144.  
  145. /**
  146. * Print the dropdown in the frontend
  147. */
  148. public static function print_cats( $cat_id,$dropdown_text )
  149. {
  150. // Adjust parameters
  151. $carMakes = array(
  152. 'show_option_all' => '','show_option_none' => $dropdown_text,'child_of' => $cat_id,'name' => 'chained-categories','hide_if_empty' => false
  153. );
  154.  
  155. // Print categories
  156. wp_dropdown_categories( $carMakes );
  157.  
  158. // Empty dropdown for sub-categories
  159. echo '<div id="chained-subcontainer">
  160. <select name="chained-subcategories" id="chained-subcategories">
  161. <option value="">- Select a category first -</option>
  162. </select>
  163. </div>';
  164. }
  165. }

ajax.js

  1. jQuery( document ).ready( function( $)
  2. {
  3. var data = {
  4. action: 'custom_query',security: wp_ajax.ajaxnonce
  5. };
  6.  
  7. $( "#chained-categories" ).on( "change",function( e )
  8. {
  9. // Add specific data to the variable,used to query the sub-categories
  10. data[ 'chained_subcat_id' ] = $( this ).val();
  11. data[ 'chained_subcat_name' ] = $(
  12. '#chained-categories option[value='
  13. + $( this ).val()
  14. + ']'
  15. ).text();
  16.  
  17. // A sub-category was selected
  18. if( $( this ).val() > 0 )
  19. {
  20. // Ajax loader icon
  21. $( '#chained-subcontainer' ).html( '<img src="' + wp_ajax.icon + '">' );
  22.  
  23. // Ajax call
  24. $.post(
  25. wp_ajax.ajaxurl,data,// No error checking is being done with the response
  26. function( response )
  27. {
  28. $( '#chained-subcontainer' ).html( response );
  29. }
  30. );
  31. }
  32. // No selection,show default
  33. else
  34. {
  35. $( '#chained-subcontainer' ).html( '<select name="chained-subcategories" id="chained-subcategories"><option value="">- Select a category first -</option></select>' );
  36. }
  37. });
  38. } );

Ajax的loader.gif

猜你在找的PHP相关文章