代码之家  ›  专栏  ›  技术社区  ›  AlexB

Wordpress中带有本机列表类的自定义类别列表

  •  2
  • AlexB  · 技术社区  · 7 年前

    尝试在侧栏中构建自定义类别列表,第一部分成功,但无法使用wordpress wp_list_categories() 正确地

    基本上,我的小部件创建了一个类别ID数组,这些ID被标记为“New”

    Array
    (
        [2] => Array
            (
                [title] => Blog Categories
                [categories] => Array
                    (
                        [0] => 1
                        [1] => 5
                    )
    
            )
    
        [_multiwidget] => 1
    )
    

    现在我需要的是创建一个所有类别的列表,其中 ID - 1 和类别 ID - 5 将显示为

    <li class="cat-item cat-item-1"><span style="color:red;">New!</span> <a href="/blog/category/mycategory1" title="mycategory special offer"><b>My Category 1</b></a></li>
    <li class="cat-item cat-item-3"><a href="/blog/category/mycategory2" title="mycategory special offer">My Category 2</a></li>
    <li class="cat-item cat-item-5"><span style="color:red;">New!</span> <a href="/blog/category/mycategory3" title="mycategory special offer 3"><b>My Category 2</b></a></li>
    

    一、 由于某些原因,无法使用

    `wp_list_categories()` as it prints all categories out on the screen.
    
            $widget_instances = get_option('widget_custom_categories_widget');
            echo '<ul>';
            foreach ( $categories as $category ) {
                foreach ($widget_instances as $cat_key) {
                    foreach ($cat_key['categories'] as $key) {
                        if($category->term_id == $key){
                            printf('<li class="cat-item cat-item-%3$s"><span style="color: red;">New!</span> <a href="%1$s"><strong>%2$s</strong></a></li>',
                                esc_url(get_category_link($category->term_id)),
                                esc_html($category->name),
                                esc_html($category->term_id)
                            );
                        }else{
                            printf('<li class="cat-item cat-item-%3$s"><a href="%1$s">%2$s</a></li>',
                                esc_url(get_category_link($category->term_id)),
                                esc_html($category->name),
                                esc_html($category->term_id)
                            );
                        }
                    }
                }
            }
            echo '<ul>';
    

    我正在使用上面代码的图片,但现在我有另一个问题,所有类别都打印了两次,一次是带有标签“New!”一次没有。

    与嵌套有关的内容 foreach 声明,但无法理解。

    请提供任何帮助或建议

    1 回复  |  直到 7 年前
        1
  •  1
  •   Samvel Aleqsanyan Tejas Shah    7 年前

    如果您的阵列 $widget_instances 每次返回按您提供的结构构造的数组:

    $cat_id = $widget_instances[2]['categories'];
    echo '<ul>';
    foreach ($categories as $category) {
        if (in_array($category->term_id, $cat_id)) {
            printf('<li class="cat-item cat-item-%3$s"><span style="color: red;">New!</span> <a href="%1$s"><strong>%2$s</strong></a></li>',
                esc_url(get_category_link($category->term_id)),
                esc_html($category->name),
                esc_html($category->term_id)
            );
        } else {
            printf('<li class="cat-item cat-item-%3$s"><a href="%1$s">%2$s</a></li>',
                esc_url(get_category_link($category->term_id)),
                esc_html($category->name),
                esc_html($category->term_id)
            );
        }
    }
    echo '<ul>';