dongmi19720408 2013-11-30 03:59
浏览 19
已采纳

如何迭代Wordpress类别以按侧栏上的类别创建过滤器复选框

enter image description here

In my sidebar.php, I want to create filter checkbox on the sidebar as depicted in the above image. So my filter checkboxes would like in this hierachical format:

Animals

  • Bears
  • Gibbons
  • Rhinos
  • Orangutan

Campaigns

  • Free the Bears
  • The Orangutan Project
  • The Silvery Gibbon Projet

I looked up the codex, and found a couple of useful properties on the array object get_categories.

However, I'm struggling to come up with my own data structure algorithm to produce the above results so my knowledge in data structure algorithm is fairly weak and I haven't practiced much, especially in PHP. I'm just wondering is there anybody in the community who's done similar functionality as I'm trying to do right now and be able to show me how it's to be done?

Look forward to your feedback.

Thanks.

  • 写回答

1条回答 默认 最新

  • doulin2025 2013-12-09 12:30
    关注

    Finally able to do this.

    Here's my code structure.

    <h2>Sidebar</h2>
    
    <?php 
    
    //Retrieve top-level categories
    $parent_args = array(
      'orderby' => 'name',
      'parent' => 0,
      'hide_empty' => 0
    );
    
    //Retrieve first-level categories
    $firstchild_args = array(
        'orderby' => 'name',
        'parent' => 0, //needs dynamic code here
        'hide_empty' => 0
    );
    
    
    $parent_categories = get_categories($parent_args);
    
    
    foreach($parent_categories as $parent_category){
    
    
    //create main headings for other categories other than Uncategorized.
    if($parent_category->name!="Uncategorized"){
        $category_label = "Filter By ";
        echo '<h3 style="font-size: 20px; color: rgb(255, 255, 255);text-shadow: 2px 3px 0px rgb(0, 0, 0);">'.$category_label.''.$parent_category->name.'</h3>';
    
        //fetch the parent category's id
        $firstchild_args['parent'] = $parent_category->term_id;
        $firstchild_categories = get_categories($firstchild_args);
    
        //fetch all the first level children categories
        foreach($firstchild_categories as $firstchild_category){
            $output = "";
            $output = $output."<label class=\"checkbox\">";
            $output = $output."    <input type=\"checkbox\"><span style='font-size: 17px; color: rgb(255, 255, 255);text-shadow: 2px 3px 0px rgb(0, 0, 0);'>".$firstchild_category->name;
            $output = $output."</label>";
    
            echo $output;
        }
    
    }
    
    }
    
    ?>
    

    It will have to do for now till I refactor this into make better code structure.

    Next to create post events for each filter checkbox selected... :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。