Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

My <?php else: ?> isn't showing up

Featured Replies

I've just installed the YARPP plugin on my blog to display related posts in my single.php - i.e. on the actual posts. I'm using one of the templates provided that utilises the wordpress 'post thumbnails' functionality which I use.

 

The code within this template is:


<?php /*
Example template for use with post thumbnails
Requires WordPress 2.9 and a theme which supports post thumbnails
Author: mitcho (Michael Yo****aka Erlewine)
*/ ?>
<h3>Related Posts</h3>
<?php if ($related_query->have_posts()):?>
<ol class="related-posts">
<?php while ($related_query->have_posts()) : $related_query->the_post(); ?>
	<?php if (function_exists('has_post_thumbnail')): if (has_post_thumbnail()):?>
	<li><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail(); ?><br /><?php the_title_attribute(); ?></a></li>
	<?php endif; endif; ?>
<?php endwhile; ?>
</ol>

<?php else: ?>
<p>No related posts.</p>
<?php endif; ?>

 

My problem is that if there are no related posts the text 'No related posts.' does not display. Anyone know why? Here's an example post with no related posts:

 

http://www.milomade.co.uk/blog/2010/04/april-showers/

 

I've messed about with the PHP and whatever I do breaks the site - I'm crap at PHP, so would appreciate it if one of you experts out there could let me know if there is anything wrong with the above code and how to fix it.

 

Thanks

E

What is

have_posts()

returning? Try putting in something simple and see if it it the argument that is causing the problem.

 

Try:

 

<?php /*
Example template for use with post thumbnails
Requires WordPress 2.9 and a theme which supports post thumbnails
Author: mitcho (Michael Yo****aka Erlewine)
*/ ?>
<h3>Related Posts</h3>
<?php if (!$related_query->have_posts()):?>
<ol class="related-posts">
       <?php while ($related_query->have_posts()) : $related_query->the_post(); ?>
               <?php if (function_exists('has_post_thumbnail')): if (has_post_thumbnail()):?>
               <li><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail(); ?><br /><?php the_title_attribute(); ?></a></li>
               <?php endif; endif; ?>
       <?php endwhile; ?>
</ol>

<?php else: ?>
<p>No related posts.</p>
<?php endif; ?>

 

If that shows 'No related posts' then it is your 'have_posts()' method that is causing you trouble.

 

:)

  • Author

okay - by adding your version of the code I get the text 'No related posts' for every post I visit. Now what?

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.