?
Solved

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

Posted on 2011-09-25
5
Medium Priority
?
313 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Originally, this post was published on Monitis Blog, you can check it here . It goes without saying that technology has transformed society and the very nature of how we live, work, and communicate in ways that would’ve been incomprehensible 5 ye…
Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
The purpose of this video is to demonstrate how to Import and export files in WordPress. 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 : Click on Too…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

752 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