0

Currently I have the following code to show related posts:

<?php

$cat_ids = array();
$postID = get_the_ID();
$categories = get_the_terms($postID, 'article-category'); //this will give all the terms

foreach($categories as $cat) {
    $cat_ids[] = $cat->term_id; //you may put some condition and consider only some specific terms as per your requirement
}

$args = array(
        'post_type' => 'article', 
        'posts_per_page' => 5,
        'tax_query' => array(array( 
        'taxonomy' => 'article-category',
        'field' => 'id',
        'terms' => $cat_ids //ids of specified terms
        )
      )
   );

$exclude = get_the_ID();
$your_query = new WP_Query( $args );

echo '<div class="posts-group">';
echo '<h2>More blog posts</h2>';
while( $your_query->have_posts() ) : $your_query->the_post();
    if( $exclude != get_the_ID() ) {
        echo '
        <article class="post home-post">
            <a href="' . get_permalink() . '">
                <div class="post-thumbnail-img">
                    ' . get_the_post_thumbnail($_post->ID, "small-thumbnail") . '
                </div>
                <h2>' . get_the_title() . '</h2>
            </a>
        </article>';
    }
endwhile;
echo '</div><br>';

?>

Now the echo '<h2>More blog posts</h2>'; should only be displayed when we have related articles, now it's displayed every time.

Thanks in advance.

1 Answer 1

1

You can do a conditional check before your loop to see if your query has any posts. And only display the title and the div if there is any posts to display.

$your_query = new WP_Query( $args );
if( $your_query->have_posts() ) : // Add this line here to check if the query has posts.
    echo '<div class="posts-group">';
    echo '<h2>More blog posts</h2>';
    while( $your_query->have_posts() ) : $your_query->the_post();

Also make sure to close the new IF statement after the end of your loop:

    endwhile;
    echo '</div><br>';
endif; // Add this line to close the IF-statement.

Update:

Exclude your current post in the Query to make this work. Otherwise the Query still contains the current post. Here is the code:

$args = array(
    'post__not_in' => array( get_the_ID() ), //Add this line to exclude the current post.
    'post_type' => 'blog', 
    'posts_per_page' => 5,
    'tax_query' => array(
        array( 
            'taxonomy' => 'blog-athlete',
            'field' => 'id',
            'terms' => $cat_ids 
        )
    )
);

$relatedquery = new WP_Query( $args );

if( $relatedquery->have_posts() ) : 
    echo '<div class="posts-group">';
    echo '<h2>More blog posts:</h2>';
    while( $relatedquery->have_posts() ) : $relatedquery->the_post();
        //Removed the IF here
        echo '
            <article class="post home-post">
                <a href="' . get_permalink() . '">
                    <div class="post-thumbnail-img">
                        ' . get_the_post_thumbnail($_post->ID, "small-thumbnail") . '
                    </div>
                    <h2>' . get_the_title() . '</h2>
                </a>
        </article>';
        // Removed the closing of the IF here
    endwhile;
    echo '</div><br>';
endif;
4
  • I tried your code, but it still displays the title, did I do something wrong? pastebin.com/HYaknYSu
    – vlovsky
    Commented Aug 11, 2015 at 12:40
  • So I found out that it works for articles, but not for blog posts.. weird.
    – vlovsky
    Commented Aug 11, 2015 at 12:47
  • Updated the answer with new info.
    – jzatt
    Commented Aug 11, 2015 at 12:52
  • Awesome, this works, cheers!
    – vlovsky
    Commented Aug 11, 2015 at 13:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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