如何修改清单类别编码?

时间:2016-09-04 作者:jrcollins

我正在使用以下内容wp_list_categories 类别页面上的代码。如果当前页面是父类别,则显示该类别的所有子类别;如果当前页面是子类别,则显示除当前类别外的父类别的所有子类别。

<?php if (is_category( )) {

    $cat = get_query_var(\'cat\');
    $thiscat = get_category ($cat);
    $parent = $thiscat->parent;
    $catname = get_cat_name( $cat );

    if ($parent != \'\') {
    echo \'<h2 class="widget-title"><span>Related Products</span></h2>\';
    echo \'<ul>\';
    wp_list_categories( array(
        \'title_li\' => \'\',
        \'child_of\' => $parent,
        \'exclude\'  => $cat,
        \'use_desc_for_title\' => 0
    ) );
    echo \'</ul>\';
    }
    else {
    echo \'<h2 class="widget-title"><span>\' . $catname . \'&nbsp;Products</span></h2>\';
    echo \'<ul>\';
    wp_list_categories( array(
        \'title_li\' => \'\',
        \'child_of\' => $cat,
        \'use_desc_for_title\' => 0
        ) );
        echo \'</ul>\';
    }
} ?>
然而,我现在意识到我需要将列出的每个类别包装在<h3> 标题元素。我认为做这件事的唯一方法是使用get_categories 然后使用foreach

1 个回复
SO网友:jrcollins

我现在使用以下代码,可以根据需要格式化列表:

<?php   if (is_category( )) {

        $thiscat = get_category( get_query_var( \'cat\' ) );
        $catid = $thiscat->cat_ID;
        $parent = $thiscat->category_parent;

        if (!empty ($parent) ) {
        //child category pages

        $catlist = get_categories(
        array(
        \'child_of\' => $parent,
        \'orderby\' => \'id\',
        \'order\' => \'DESC\',
        \'exclude\' => $catid,
        \'hide_empty\' => \'0\'
        ) );

        //widget title
        echo \'<h2 class="widget-title"><span>Related Products</span></h2>\';

        //categories list
        foreach ( $catlist as $category ) {
        echo \'<ul>\';
        echo \'<li><h3 class="entry-title"><a href="\' . get_category_link( $category->cat_ID ) . \'" rel="bookmark">\' . $category->cat_name . \'</a></h3></li>\';
        echo \'</ul>\';
        }
        }

        else {
        //parent category pages

        $catname = get_cat_name( $catid );

        $catlist = get_categories(
        array(
        \'child_of\' => $catid,
        \'orderby\' => \'id\',
        \'order\' => \'DESC\',
        \'hide_empty\' => \'0\'
        ) );

        //widget title
        echo \'<h2 class="widget-title"><span>\' . $catname . \'&nbsp;Products</span></h2>\';

        foreach ( $catlist as $category ) {
        echo \'<ul>\';
        echo \'<li><h3 class="entry-title"><a href="\' . get_category_link( $category->cat_ID ) . \'" rel="bookmark">\' . $category->cat_name . \'</a></h3></li>\';
        echo \'</ul>\';
        }

        }

} ?>