【问题标题】:How to iterate Wordpress categories to create filter checkboxes by categories on a sidebar如何迭代 Wordpress 类别以在侧边栏上按类别创建过滤器复选框
【发布时间】:2013-12-09 20:30:57
【问题描述】:

在我的 sidebar.php 中,我想在侧边栏上创建过滤器复选框,如上图所示。所以我的过滤器复选框希望采用这种分层格式:

动物

  • 长臂猿
  • 犀牛
  • 猩猩

广告系列

  • 释放熊
  • 猩猩计划
  • 银色长臂猿项目

我查阅了 codex,并在数组对象 get_categories 上发现了一些有用的属性。

但是,我正在努力想出自己的数据结构算法来产生上述结果,所以我在数据结构算法方面的知识相当薄弱,而且我没有太多实践,尤其是在 PHP 方面。我只是想知道社区中是否有人完成了与我现在尝试做的类似的功能并且能够向我展示如何完成?

期待您的反馈。

谢谢。

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    终于可以做到了。

    这是我的代码结构。

    <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;
        }
    
    }
    
    }
    
    ?>
    

    在我将它重构为更好的代码结构之前,它现在必须做。

    接下来为每个选中的过滤器复选框创建发布事件...:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-02
      • 1970-01-01
      • 1970-01-01
      • 2015-02-25
      • 2019-08-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多