• Resolved devotee

    (@devotee)


    I´m using two loops on a page to display posts from two different categories. Above each is the title of the category, which I only want to display if there actually is posts in the category.

    Putting the title inside the loop just results in it getting repeated for each post (of course…).

    I need a simple if statement or something to only display the heading if there is posts in that category. Does anyone know what code I need?

Viewing 6 replies - 1 through 6 (of 6 total)
  • Most basic loop you can have.
    http://codex.wordpress.org/The_Loop#Using_The_Loop

    <?php
    $postargs=array(
    'showposts' => 4,
    'cat' => 3
    );
    
    $category_box = new WP_Query($postargs);
    if ($category_box->have_posts() ) : while ($category_box->have_posts()) :
    $category_box->the_post();
    ?>
    ... your code to display your posts ...
    
    <?php endwhile; else: ?>
    <p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
    <?php endif; ?>

    If you need to because of using two loops, you can reset the query to stop conflicts. This rarely happens, but just in case, call this after the first loop.

    wp_reset_query();

    Hope this points you in the right direction.

    Thread Starter devotee

    (@devotee)

    Hmm does this really lead me in the right direction? I have no problem displaying the posts I want to and from the category I want to.

    My page looks like this:

    Single post
    __________
    Category X title
    Thumbnails from category X
    ——
    Category Y title
    Thumbnails from category Y

    I cant have the title on Category X & Y in the loop because then it gets repeated for every post doesnt it?

    I need a simple if statement or something to only display the heading if there is posts in that category.

    most loops have this:

    if( have_posts() )

    if you paste the full code of your template into a http://pastebin.com/ and post the link to it here, then someone might be able to make a suggestion on how to include this conditional statement.

    Thread Starter devotee

    (@devotee)

    http://pastebin.com/xgRkdbML

    Here is the code. Its two near identical loops where only the category is different. You can see there is a div with the name on the category above each loop. I put it outside the loop because if I put it first in the loop, it would repeat the category title for each post.

    My problem is that I dont want the category name there if there are no posts in that category.

    Thanks for helping.

    these lines occur twice:

    if( $my_query->have_posts() ) {
          while ($my_query->have_posts()) : $my_query->the_post(); ?>

    and here is the spot where you could add the category name:

    if( $my_query->have_posts() ) {
    /*HERE CATEGORY TITLE CODE*/
          while ($my_query->have_posts()) : $my_query->the_post(); ?>

    make sure to close and open the php tags accordingly if you enter html code.

    Thread Starter devotee

    (@devotee)

    Thanks! I got it to work. It makes sense when I see it but I´m not familiar enough with wordpress and php to always figure it out by myself.

    Looks like this now:

    ...if( $my_query->have_posts() ) { ?>
    
        	<div class="grid_24">Portfolio</div>
    
        <?php  while ($my_query->have_posts()) : $my_query->the_post(); ?>

    Issue resolved! 🙂

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Category heading to display only if there are posts in the category’ is closed to new replies.