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.

Might be a really simple question... I am trying to pull out a category name in PHP and display it on my page. I have multiple category sections on this page, and each need to pull their respected name as an h2. Here is what I am currently doing:

<div class="category-section">
    <h2><?php the_category(); ?></h2>
    <div class="content">
        <?php query_posts('category_name"theCategoryName"');
        while (have_posts()) : the_post(); ?>
            #code...
        <?php endwhile; ?>
    </div>
</div>

When I query_posts for my theCategoryName I get the content needed for that section, however, a couple lines above where I have the_category() printing out, this stays the same name for all of my different sections. For example, if the cateogry name was "cats" then for all my sections that have an h2 show the text "cats".

Lets say this particular section is suppose to be "dogs" instead of "cats", how would I achieve this? Hope this makes sense..

*EDIT*

So here is an updated version of my code, but I am still having issues. the_category(); for each section still shows up as the same name. AND it seams as though the category name changes if I create a new post with a different category. For example, if I create "cars" then both of the below category names change to "cars".

<div class="category-1">
    <h2><?php the_category(); ?></h2>
    <div class="content">
        <?php 
        $q = new WP_Query('category_name="cats"');
        if($q->have_posts()) {
            while ($q->have_posts()) : $q->the_post(); ?>
                #code...
            <?php 
            endwhile; 
        }
        wp_reset_postdata();
        ?>
    </div>
</div>
<div class="category-2">
    <h2><?php the_category(); ?></h2>
    <div class="content">
        <?php 
        $q = new WP_Query('category_name="dogs"');
        if($q->have_posts()) {
            while ($q->have_posts()) : $q->the_post(); ?>
                #code...
            <?php 
            endwhile; 
        }
        wp_reset_postdata();
        ?>
    </div>
</div>

Is my code syntax correct? or am I just all over the place with this :(

share|improve this question

1 Answer 1

up vote 3 down vote accepted

I would advise against query_posts for readability purposes as its easier to make mistakes and you can end up muddled when you nest them. ( You also missed out an equals sign in your syntax.

Instead use WP_Query, and check for when no posts are found, e.g. this code:

<div class="category-section">
    <h2><?php the_category(); ?></h2>
    <div class="content">
        <?php
        $q = new WP_Query('category_name=theCategoryName');
        if($q->have_posts()){
            while ($q->have_posts()) : $q->the_post(); ?>
                #code...
            endwhile;
        } else {
            ?><p>No posts</p><?php
        }
 ?>
    </div>
</div>

Finally, the root cause of you're problem, you must add a call to this function after you're done with your loop to reset everything back to before the loop.

If you're using WP_Query or get_posts call wp_reset_postdata();

If you're using query_posts use wp_reset_query();

Remember to reset things after every post loop, always check if there are any posts before starting the loop, and always put the reset after the check, not inside after the loop ( else it won't reset if no posts are found).

share|improve this answer
    
I made some changes as per your suggestions, but continue to have some problems. Updated code is above. Am I reseting things right? –  Romes Apr 10 '12 at 20:41
    
Okay your updated code makes your issue clearer. You called the_category and got a result, why do you think calling it again would give you something different? Are you listing all categories? If so you need a loop surrounding everything.. –  Tom J Nowell Apr 10 '12 at 21:35
    
I'll update my code in the morning after I've slept to demonstrate and explain –  Tom J Nowell Apr 10 '12 at 21:36
    
I see what you mean. Noob mistake, thanks for the clarification. –  Romes Apr 10 '12 at 21:58

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.