Link to home
Start Free TrialLog in
Avatar of Adam
AdamFlag for Canada

asked on

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

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.
Avatar of Jerry Miller
Jerry Miller
Flag of United States of America image

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
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.
ASKER CERTIFIED SOLUTION
Avatar of Jason C. Levine
Jason C. Levine
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
And I have no idea why the snippet doubled up, but enjoy it.
Avatar of Adam

ASKER

Worked like a charm. Thanks!