使用单选输入代替“选择类别-WordPress”上的复选框 [英] Use radio input instead of checkbox on Select Category - Wordpress

查看:84
本文介绍了使用单选输入代替“选择类别-WordPress”上的复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在为教堂开发网站并使用wordpress。这个教会是一个国际组织,在阿根廷和乌拉圭周围有几个社区。我创建了一个自定义帖子类型社区,以便用户可以添加新社区或添加/修改与位置(地图)管理器,活动相关的信息。这些社区与WordPress类别相关,

I'm currently developing a site for a Church and using wordpress. This Church is an international organization, and have several communities around Argentina and Uruguay. I created a custom post type Community so the users can add a new community or add/modify info related like location (maps) managers, activities. Those communities are related to the wordpress categories with

'taxonomies' => array( 'category' )

我担心的是,如何仅针对Community Post表单进行修改,而不是使用复选框小部件来选择类别,而要使用另一个组件,至少要将复选框更改为单选,因为我想删除每个社区选择多个类别的选项。

My concern is, how I can modify just for the Community Post form, to instead of having a checkbox widget to select the categories, another component to be use, at least change the checkbox to a radio because I want to remove the option to select more than 1 category per Community.

问候

推荐答案

您可以使用此帮助程序类,因此代码看起来像这样

You can use this helper class, so the code will look like this

<?php

if ( !class_exists( 'WDS_Taxonomy_Radio' ) ) {
   /**
    * Removes and replaces the built-in taxonomy metabox with our radio-select metabox.
    * @link  http://codex.wordpress.org/Function_Reference/add_meta_box#Parameters
    */
   class WDS_Taxonomy_Radio {

      // Post types where metabox should be replaced (defaults to all post_types associated with taxonomy)
      public $post_types = array();
      // Taxonomy slug
      public $slug = '';
      // Taxonomy object
      public $taxonomy = false;
      // New metabox title. Defaults to Taxonomy name
      public $metabox_title = '';
      // Metabox priority. (vertical placement)
      // 'high', 'core', 'default' or 'low'
      public $priority = 'high';
      // Metabox position. (column placement)
      // 'normal', 'advanced', or 'side'
      public $context = 'side';
      // Set to true to hide "None" option & force a term selection
      public $force_selection = false;


      /**
       * Initiates our metabox action
       * @param string $tax_slug      Taxonomy slug
       * @param array  $post_types    post-types to display custom metabox
       */
      public function __construct( $tax_slug, $post_types = array() ) {

         $this->slug = $tax_slug;
         $this->post_types = is_array( $post_types ) ? $post_types : array( $post_types );

         add_action( 'add_meta_boxes', array( $this, 'add_radio_box' ) );
      }

      /**
       * Removes and replaces the built-in taxonomy metabox with our own.
       */
      public function add_radio_box() {
         foreach ( $this->post_types() as $key => $cpt ) {
            // remove default category type metabox
            remove_meta_box( $this->slug .'div', $cpt, 'side' );
            // remove default tag type metabox
            remove_meta_box( 'tagsdiv-'.$this->slug, $cpt, 'side' );
            // add our custom radio box
            add_meta_box( $this->slug .'_radio', $this->metabox_title(), array( $this, 'radio_box' ), $cpt, $this->context, $this->priority );
         }
      }

      /**
       * Displays our taxonomy radio box metabox
       */
      public function radio_box() {

         // uses same noncename as default box so no save_post hook needed
         wp_nonce_field( 'taxonomy_'. $this->slug, 'taxonomy_noncename' );

         // get terms associated with this post
         $names = wp_get_object_terms( get_the_ID(), $this->slug );
         // get all terms in this taxonomy
         $terms = (array) get_terms( $this->slug, 'hide_empty=0' );
         // filter the ids out of the terms
         $existing = ( !is_wp_error( $names ) && !empty( $names ) )
            ? (array) wp_list_pluck( $names, 'term_id' )
            : array();
         // Check if taxonomy is hierarchical
         // Terms are saved differently between types
         $h = $this->taxonomy()->hierarchical;

         // default value
         $default_val = $h ? 0 : '';
         // input name
         $name = $h ? 'tax_input['. $this->slug .'][]' : 'tax_input['. $this->slug .']';

         echo '<div style="margin-bottom: 5px;">
         <ul id="'. $this->slug .'_taxradiolist" data-wp-lists="list:'. $this->slug .'_tax" class="categorychecklist form-no-clear">';

            // If 'category,' force a selection, or force_selection is true
            if ( $this->slug != 'category' && !$this->force_selection ) {
               // our radio for selecting none
               echo '<li id="'. $this->slug .'_tax-0"><label><input value="'. $default_val .'" type="radio" name="'. $name .'" id="in-'. $this->slug .'_tax-0" ';
               checked( empty( $existing ) );
               echo '> '. sprintf( __( 'No %s', 'wds' ), $this->taxonomy()->labels->singular_name ) .'</label></li>';
            }

         // loop our terms and check if they're associated with this post
         foreach ( $terms as $term ) {

            $val = $h ? $term->term_id : $term->slug;

            echo '<li id="'. $this->slug .'_tax-'. $term->term_id .'"><label><input value="'. $val .'" type="radio" name="'. $name .'" id="in-'. $this->slug .'_tax-'. $term->term_id .'" ';
            // if so, they get "checked"
            checked( !empty( $existing ) && in_array( $term->term_id, $existing ) );
            echo '> '. $term->name .'</label></li>';
         }
         echo '</ul></div>';

      }

      /**
       * Gets the taxonomy object from the slug
       * @return object Taxonomy object
       */
      public function taxonomy() {
         $this->taxonomy = $this->taxonomy ? $this->taxonomy : get_taxonomy( $this->slug );
         return $this->taxonomy;
      }

      /**
       * Gets the taxonomy's associated post_types
       * @return array Taxonomy's associated post_types
       */
      public function post_types() {
         $this->post_types = !empty( $this->post_types ) ? $this->post_types : $this->taxonomy()->object_type;
         return $this->post_types;
      }

      /**
       * Gets the metabox title from the taxonomy object's labels (or uses the passed in title)
       * @return string Metabox title
       */
      public function metabox_title() {
         $this->metabox_title = !empty( $this->metabox_title ) ? $this->metabox_title : $this->taxonomy()->labels->name;
         return $this->metabox_title;
      }


   }

   $custom_tax_mb = new WDS_Taxonomy_Radio( 'category', array('community') );

   // Update optional properties

   // $custom_tax_mb->priority = 'low';
   // $custom_tax_mb->context = 'normal';
   // $custom_tax_mb->metabox_title = __( 'Custom Metabox Title', 'yourtheme' );
   // $custom_tax_mb->force_selection = true;

}

来源如何替换WordPress默认分类法Metabox-WebDevStudios

这篇关于使用单选输入代替“选择类别-WordPress”上的复选框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆