Solved

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

Posted on 2011-09-25
5
300 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 500 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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
In order to have all security and back ups taken care of, WordPress users can sign up for services with WP Engine.
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Go t…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now