在Wordpress插件中通过Slug订购自定义分类

时间:2016-08-08 15:52:29

标签: php wordpress

CONTEXT

我们创建了一个自定义分类,将在第三方插件中以搜索过滤器参数的形式显示(以复选框的形式)。但是,我们希望按slug排序显示分类法列表。

这是生成复选框并输出分类的功能 - 我们定制的部分从"开始。 //创建属性" 。

public function checkbox($args)
{
   //init defaults & vars
   $input_args = $this->prepare_input_args($args, "checkbox");

   $input_name = $input_args['attributes']['name'];
   unset($input_args['attributes']['name']);

   //filter the input arguments before the html is generated - allowing almost all options to be modified
   if(has_filter('sf_input_object_pre')) {
      $input_args = apply_filters('sf_input_object_pre', $input_args, $this->sfid);
   }

   //prepare html
   $attibutes_html = $this->convert_attributes_to_html($input_args['attributes']);

   $open_child_count = 0;

   ob_start();

   ?>
   <ul<?php echo $attibutes_html; ?>>

      <?php

      $last_option_depth = 0;

      $option_count = count($input_args['options']);
      $current_depth = 0;

      $is_li_open = array();
      //echo "<ul>";
      for($i=0; $i<$option_count; $i++)
      {
         $option = &$input_args['options'][$i];

         if(!isset($option->attributes))
         {
            $option->attributes = array(
               'class' => '',
               'id'   => ''
            );
         }
         //check a default has been set and set it
         $option->attributes['type'] = "checkbox";
         $option->attributes['value'] = $option->value;
         $option->attributes['name'] = $input_name;
         $option->attributes['html'] = '';

         $container_attributes = array();

         if($this->is_option_selected($option, $input_args['defaults']))
         {
            $option->attributes['checked'] = 'checked';
            $option->attributes['class'] = trim($option->attributes['class']).' sf-option-active';
         }
         else
         {
            if(isset($option->attributes['checked']))
            {
               unset($option->attributes['checked']);
            }
         }

         //now we want to put the class attribute on the LI, and remove from input
         $option_class = $option->attributes['class'];
         $option->attributes['class'] = $input_args['input_class'];

         $input_id = $this->generate_input_id($input_name."_".$option->value);
         $option->attributes['id'] = $input_id;

         $container_attibutes_html = "";
         if(isset($option->count))
         {
            $container_attributes['data-sf-count'] = $option->count;

            $container_attibutes_html = $this->convert_attributes_to_html($container_attributes);

         }



         //create the attributes
         $input_attibutes_html = $this->convert_attributes_to_html($option->attributes);
         $option_label = $option->label;
         $option_html = $option->attributes['value'];

         if($input_name == "_sft_module_authors[]"){
            $tax_name = "module_authors";
         } else if ($input_name == "_sft_module_mindset[]") {
            $tax_name = "module_mindset";
         } else if ($input_name == "_sft_module_language[]") {
            $tax_name = "module_language";
         } else if ($input_name == "_sft_management_levels[]") {
            $tax_name = "management_levels";
         } else{
            $tax_name = "wpdmcategory";
         }

            // Get the ID of a given category
         $my_term = get_term_by('slug',$option->value,$tax_name);

         $category_id = $my_term->term_id;
         $category_desc = $my_term->description;
         $category_name = $my_term->name;
         $meta_image = get_wp_term_image($category_id);

         // Get the URL of this category
         $category_link = get_category_link( $category_id );




         if ($input_name == "_sft_module_authors[]") {

            echo '<li class="accordion-content '.$option_class.'"'.$container_attibutes_html.'><input '.$input_attibutes_html.'><label class="sf-label-checkbox" for="'.$input_id.'">' . $category_name . '</label><a class="eModal-post-'.$category_id.'" href="'.$category_link.'"><i class="fa fa-info-circle"></i></a>';

            echo do_shortcode("[modal id='post-". $category_id ."' size='small' title='". $category_name ."']". "<div class='col-md-8'>" . $category_desc . "</div>" . "<div class='col-md-4'>" .  "<div class='img-wrap'><img src='".$meta_image."'></div></div>" . "[/modal]");



         } else if ($input_name == "_sft_module_mindset[]") {

            echo '<li class="accordion-content '.$option_class.'"'.$container_attibutes_html.'><input '.$input_attibutes_html.'><label class="sf-label-checkbox" for="'.$input_id.'">' . $category_name . '</label><a class="eModal-post-'.$category_id.'" href="'.$category_link.'"><i class="fa fa-info-circle"></i></a>';

            echo do_shortcode("[modal id='post-". $category_id ."' size='small' title='". $category_name ."']". "<div class='col-md-12'>" . $category_desc . "</div>" . "[/modal]");



         } else if  ($input_name == "_sft_module_language[]") {

            echo '<li class="accordion-content '.$option_class.'"'.$container_attibutes_html.'><input '.$input_attibutes_html.'><label class="sf-label-checkbox" for="'.$input_id.'">' . $category_name;


         } else if  ($input_name == "_sft_management_level[]") {

            echo '<li class="accordion-content '.$option_class.'"'.$container_attibutes_html.'><input '.$input_attibutes_html.'><label class="sf-label-checkbox" for="'.$input_id.'">' . $category_name;



         } else {

            echo '<li class="accordion-content '.$option_class.'"'.$container_attibutes_html.'><input '.$input_attibutes_html.'><label class="sf-label-checkbox" for="'.$input_id.'">' . $category_name . '</label><a class="eModal-post-'.$category_id.'" href="'.$category_link.'"><i class="fa fa-info-circle"></i></a>';

            echo do_shortcode("[modal id='post-". $category_id ."' size='small' title='". $category_name ."']". "<div class='col-md-12'>" . $category_desc . "</div>" . "[/modal]");

         }


         if(isset($option->depth))
         {//then we do depth calculations

            $current_depth = $option->depth;

            $close_li = true;
            $open_child_list = false;
            $close_ul = false;

            $next_depth = -1;

            if(isset($input_args['options'][$i+1]))
            {
               $next_option = $input_args['options'][$i+1];
               $next_depth = $next_option->depth;
            }

            if($next_depth!=-1)
            {
               if($next_depth!=$current_depth)
               {//there is a change in depth

                  if($next_depth>$current_depth)
                  {//then we need to open a child list
                     //and, not close the current li
                     $open_child_list = true;
                     $close_li = false;
                  }
                  else
                  {
                     $close_ul = true;
                  }
               }
            }

            if($open_child_list)
            {
               $open_child_count++;
               echo '<ul class="children">';
            }
            if($close_li)
            {
               echo "</li>";
            }
            if($close_ul)
            {
               $diff = $current_depth - $next_depth;
               $open_child_count = $open_child_count - $diff;
               $str_repeat = str_repeat("</ul></li>", $diff);
               echo $str_repeat;
            }
         }
      }

      $str_repeat = str_repeat("</ul></li>", (int)$open_child_count);
      echo $str_repeat;

      ?>
   </ul>
   <?php

   $output = ob_get_clean();

   return $output;
}

0 个答案:

没有答案