Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have the following code to display tagged articles with the same name as the page title, but I only want the to appear if the tag exists, otherwise it just says 'Related Articles' with no content. I have tried using some conditionals with no luck. Is there a way to do this? I have found no examples or questions here that relate specifically to this.

<h1>Related Articles</h1>
<?php query_posts( 'tag=' . get_the_title() . '' );
while ( have_posts() ) : the_post(); ?>
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<?php endwhile;?>
<?php wp_reset_query();?>

Thank you for any help

share|improve this question

2 Answers 2

Try this

<?php $related = new WP_Query( 'tag=' . get_the_title() . '' );
if( $related->have_posts() ):
    <h1>Related Articles</h1>
    while ( $related->have_posts() ) : the_post(); ?>
        <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a> </li>

   <?php endwhile;

else:
//there are no related articled
endif;
wp_reset_postdata();?>

EDIT

You should not use query_posts for a secondary loop. The query_posts alters the 'main loop' (in this case the main loop initial contains just your page). See the Codex here:

query_posts() is meant for altering the main loop. Once you use query_posts(), your post-related global variables and template tags will be altered. Conditional tags that are called after you call query_posts() will also be altered - this may or may not be the intended result.

To create secondary listings (for example, a list of related posts at the bottom of the page, or a list of links in a sidebar widget), try making a new instance of WP_Query or use get_posts().

share|improve this answer
2  
Hope you don't mind the edit :) –  Stephen Harris Apr 12 '12 at 13:24
    
Nope you are totally right –  janw Apr 12 '12 at 13:26
    
@janw +1 for using if( $related->have_posts() ) to answer the actual question –  Michael Apr 12 '12 at 16:31

First, a caveat: I think that using get_the_title() as a tag filter is incredibly fragile. Unless all your post titles are single words, I doubt you'll ever get any matches. So, you really should be using some means of splitting up the words in the post title, if you're going to use them to query posts by matching post tag. Try exploding the results of get_the_title(), or, better yet: the Post permalink slug, e.g. via basename( get_permalink() ).

Second, why are you not simply tagging each article, and then querying by post tags? That would seem to be a heck of a lot easier, and effective, than querying by post tags that match the post title.

Third, do not use query_posts() for this purpose. The query_posts() function is intended only to modify the main loop query. If you need a secondary loop (and a list of related posts is certainly a secondary loop), then you need to use get_posts() or WP_Query().

Here's one way to build the secondary query/loop:

<?php
// Get the post slug
$related_post_slug = basename( get_permalink() );
// Explode the slug terms
// Since the post slug is constructed as
// "term-term-term-term", we simply
// Use the hyphen to explode the terms
$related_post_slug_terms = explode( '-', $related_post_slug );
// Implode the slug terms, using commas, for an OR query
// If you want an AND query, implode using "+"
$related_post_tags = implode( ',', $related_post_slug_terms ); 
// Query related posts
$related_posts = new WP_Query( array(
    'tag' => $related_post_tags
) );

// Now loop through the related posts query
if( $related_posts->have_posts() ):
    while ( $related_posts->have_posts() ) : $related_posts->the_post(); 
        ?>
        <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
        <?php
    endwhile;
else:
    ?>
    there are no related articled
    <?php
endif;

// Reset post data, for good measure
wp_reset_postdata();
?>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.