0

I'm using the following wp_list_categories code on category pages. If the current page is a parent category it displays all child categories of that category and if the current page is a child category it displays all the child categories of the parent category except the current category.

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

However, I now realize that I need to wrap each of the listed categories in a <h3> header element. Am I right in assuming that the only way to do this is to use get_categories and then output the required HTML using a foreach loop?

0

I'm now using the following code which allows me to format the list as required:

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

        }

} ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.