Saturday, 25 May 2013

Listing Subcategories with post titles underneath

Listing Subcategories with post titles underneath

When viewing a parent category page, I am trying to list its sub-categories with each post title (max 5) underneath. I have managed to put together a code that lists sub category titles and I have managed to put together a separate code that is listing the categories with the post titles underneath. The problem with the last code is that it is only listing level 1 categories.
I have tried manually adding "parent" arguments or a "category slug" argument but neither has worked. Here is that code, how can I alter it so that it only displays the sub-categories of the current page instead of level 1 categories. This is all sitting inside the category.php template.
<!-- Category Archive Start -->
<ul class="catArchive">
<?php
$catQuery = $wpdb->get_results("SELECT * FROM $wpdb->terms AS wterms INNER JOIN $wpdb->term_taxonomy AS wtaxonomy ON ( wterms.term_id = wtaxonomy.term_id ) WHERE wtaxonomy.taxonomy = 'category' AND wtaxonomy.parent = 0 AND wtaxonomy.count > 0");

$catCounter = 0;

foreach ($catQuery as $category) {

    $catCounter++;

    $catStyle = '';
    if (is_int($catCounter / 2)) $catStyle = ' class="catAlt"';

    $catLink = get_category_link($category->term_id);

    echo '<li'.$catStyle.'><h3><a href="'.$catLink.'" title="'.$category->name.'">'.$category->name.'</a></h3>';
        echo '<ul>';

        query_posts('cat='.$category->term_id.'&showposts=5');?>

        <?php while (have_posts()) : the_post(); ?>
            <li><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
        <?php endwhile; ?>

            <li><a href="<?php echo $catLink; ?>" title="<?php echo $category->name; ?>">More <strong><?php echo $category->name; ?></strong></a></li>
        </ul>
    </li>
    <?php } ?>
</ul>
<!-- Category Archive End -->
Here is the code I used that displays all sub-category names without any respective post titles underneath:
<?php if(is_category( array( 35,36 ) ) ) { ?>

<div class="subcategories">

<h2>Subcategories</h2>


    <?php
    if (is_category()) {
    $this_category = get_category($cat);
    }
    if($this_category->category_parent)
    $this_category = wp_list_categories('orderby=id&title_li=&child_of='.$this_category->category_parent."&echo=0"); else
    $this_category = wp_list_categories('orderby=id&title_li=&child_of='.$this_category->cat_ID."&echo=0");
    if ($this_category) { ?>

    <?php echo $this_category; ?>
    <?php } ?>

    </div>

    <?php } ?>
This is how both codes show up right now: http://getcrowdsuite.com/mvp2/category/markets/

No comments:

Post a Comment