Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using the following code to display a list of posts within a category, but I want to display the title in it's own h2 tag above the list.

<ul class="subcats-list">
<?php
$weightloss = new WP_Query();
$weightloss->query('showposts=5&include=4');

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

</ul><!-- subcat -->

EDIT:

So I tried using single_cat_title , however, the title is coming up for the parent category for all the sub category divs. Here's an example of a div that I am replicating multiple times, where the query is pulling posts from different sub categories. I would like the title for each sub category above the posts. I realize it's not working because I'm returning the parent category with that function, I'm just can't figure out the php...

        <ul class="subcats-list">
            <h2 class="subcats-title"><?php single_cat_title(); ?></h2>
                <?php
                $weightloss = new WP_Query();
                $weightloss->query('showposts=5&cat=4');

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

        </ul><!-- subcat -->
share|improve this question
1  
that doesn't make sense because in the loop above you're querying posts from all categories. And what's up with include? Is that a valid argument? –  onetrickpony Mar 30 '11 at 18:04
    
you're absolutely correct. I messed that up. I meant to use cat=4, not include. I am using this snippet multiple times on one page. So I wanted to display the category above the list. –  chris_s Mar 30 '11 at 23:19

2 Answers 2

up vote 1 down vote accepted

Since you already have the category ID (you use it in your call to WP_Query::query()), you can just use that to request the category name. get_cat_name( $cat_id ) will do exactly that. It uses get_category( $cat_id ) in the background, which returns the full category object, which can be useful if you want to show more information about the category.

share|improve this answer
    
this was exactly what I was looking for. Thanks Jan. –  chris_s Apr 6 '11 at 14:55

It's not entirely clear what you're referring to, you asked how to get the title of category inside a custom loop. Are you referring to particular category, or a category associated with a given post inside that loop?

In any case if you're referring to printing out the name of the first category for each post in your custom loop simply add the following code somewhere inside your loop.

<?php single_cat_title(); ?>

Documentation for the function can be found here:
http://codex.wordpress.org/Function_Reference/single_cat_title

Note the comment under your question though, that's a valid point, what were you intending with the include parameter, are you trying to filter that query to a particular set of posts?

Valid query parameters can be found on the WP_query codex page, if it's helpful. If you're unsure about how to set parameters, just let us know.. ;)

share|improve this answer
    
I'll give that a shot. He was correct about the include. That was my mistake. –  chris_s Mar 30 '11 at 23:20
    
So I tried it out, however, the title is coming up for the parent category for all the sub category divs. Here's an example of two divs, where the query is pulling posts from two different sub categories. I would like the title for each sub category above the posts. –  chris_s Mar 31 '11 at 15:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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