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
Solved

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

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Suggested Solutions

Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
An enjoyable and seamless user experience can go a long way on an eCommerce site. While a cohesive layout and engaging copy play roles in creating a positive user experience, some sites neglect aspects that seem marginal but in actuality prove very …
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…
The viewer will learn how to count occurrences of each item in an array.

809 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