[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Wordpress: Hide title if there is less then one post...??

Posted on 2011-09-25
5
Medium Priority
?
320 Views
Last Modified: 2012-06-27
This seems like a dumb question but I can't seem to figure it out.

I have created a query post that will list the last 5 posts based on some custom fields data. However, its a new site so it might be a little bit before any posts show up in the list. Above the query post, I have added a title (i.e. "Latest Posts") but want the title to not appear if there are no posts in the list. Obviously, if I put it within the while loop that generates the list, it will get repeated each time.

So how do I remove my title from outside the loop if there is less then one post in the list? Again, it seems like a real simple concept but I think my brain is a little fried at the moment and I can't quite figure it out.
0
Comment
Question by:Adam
  • 2
  • 2
5 Comments
 
LVL 18

Expert Comment

by:Jerry Miller
ID: 36596572
You could put in a variable that changes within the the loop based on whether your query returns rows. Then outside the loop, do the comparison. I am not sure which database type you are using, but in MS SQL it would be something along this line:

declare @count int
set @count = 0

While <condition exists>
begin
<your code here>
set @count = @count + @@ROWCOUNT
end

If @count < 1
begin
<hide title>
else
<display title>

I am not as familiar with MySQL, but here is a reference for the functions.

http://dev.mysql.com/doc/refman/5.0/en/information-functions.html
0
 
LVL 18

Expert Comment

by:Jerry Miller
ID: 36596576
Make sure that you set the @count variable directly after the select statement that returns your rows. The @@ROWCOUNT counts the number of rows in the previous SQL statement and if it isn't a select statement, it will not work as expected.
0
 
LVL 70

Accepted Solution

by:
Jason C. Levine earned 2000 total points
ID: 36597049
This is covered in "Other Loop Tricks" in the Codex:

http://codex.wordpress.org/The_Loop_in_Action#Other_Loop_Tricks

<?php if (have_posts()) : ?>

  <?php if (($wp_query->post_count) > 1) : ?>
     <?php while (have_posts()) : the_post(); ?>
       <!-- Do your post header stuff here for excerpts-->
          <?php the_excerpt() ?>
       <!-- Do your post footer stuff here for excerpts-->
     <?php endwhile; ?>

  <?php else : ?>

     <?php while (have_posts()) : the_post(); ?>
       <!-- Do your post header stuff here for single post-->
          <?php the_content() ?>
       <!-- Do your post footer stuff here for single post-->
     <?php endwhile; ?>

  <?php endif; ?>

<?php else : ?>
     <!-- Stuff to do if there are no posts-->

<?php endif; ?>

Open in new window


So you can see from the above that first you check for if have_posts() and then open the loop.  Put the text outside of the while statement and it will only repeat once.  If you don't have any posts, that's where the else comes in.  In your case, do nothing.



<?php if (have_posts()) : ?>

  <?php if (($wp_query->post_count) > 1) : ?>
     <?php while (have_posts()) : the_post(); ?>
       <!-- Do your post header stuff here for excerpts-->
          <?php the_excerpt() ?>
       <!-- Do your post footer stuff here for excerpts-->
     <?php endwhile; ?>

  <?php else : ?>

     <?php while (have_posts()) : the_post(); ?>
       <!-- Do your post header stuff here for single post-->
          <?php the_content() ?>
       <!-- Do your post footer stuff here for single post-->
     <?php endwhile; ?>

  <?php endif; ?>

<?php else : ?>
     <!-- Stuff to do if there are no posts-->

<?php endif; ?>

Open in new window

0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 36597080
And I have no idea why the snippet doubled up, but enjoy it.
0
 
LVL 1

Author Closing Comment

by:Adam
ID: 36814590
Worked like a charm. Thanks!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The first step to building an amazing About page is to figure out what you want the page to say about your company. You then must grab the attention of the reader, boast a bit, tell a story and let others brag about you. With a little bit of thought…
The purpose of this video is to demonstrate how to add AdSense Ads to a WordPress Website, and how to set up WordPress to automatically place Ads in Sidebars. This will be demonstrated using a Windows 8 PC. Log into your AdSense account. : Cli…
The purpose of this video is to demonstrate how to prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…
Suggested Courses
Course of the Month17 days, 16 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question