0

I'm trying to display the related posts using functions.php:

function posts_related($related){ if (is_single()) {
global $post;
// Build basic custom query arguments
$custom_query = new WP_Query( array( 
       'posts_per_page' => 8, // Number of related posts to display
       'post__not_in' => array($post->ID), // Ensure that the current post is not displayed
       'orderby' => 'rand', // Randomize the results
));

// Run the loop and output data for the results
if ( $custom_query->have_posts() ) : while ( $custom_query->have_posts() ) : $custom_query->the_post(); 
 if ( has_post_thumbnail()
  ) { 
                 $permalink = the_permalink();
                 $post_thumbnail = the_post_thumbnail('medium');
                 $title = the_title();                       
                 $related .= '<a href="' . $permalink .  '"><img src="' . $post_thumbnail . '/></a>';             
                 }
           $related .=  '<a href="' .  $permalink . '"><b>' . $title . '</b></a>';
     endwhile; 
 else : 
    $related .= '<p>Nothing to show.</p>';
endif;
// Reset postdata
}
         echo '<pre>'; var_dump( has_post_thumbnail() ); echo '</pre>'; 

    return $related;

}    //wp_reset_postdata();

add_filter( "the_content", "posts_related", 99 );
add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 100, 50, true );

But I'm not being able to handle the output properly. I need it to display below the post (single post).

1
  • use the functions that will return the results: get_the_title() and get_permalink() and get_the_post_thumbnail()...
    – Michael
    Commented May 3, 2021 at 3:49

1 Answer 1

2

I slightly modified your code and tested on my Wordpress installation.
If you using a filter, like 'the_content', which recieves a content as an argument,
you must also return this content back.
Here is working version of your function with comments.

 //this filter recieves $content, which you should always return, 
 //otherwise you will not see the content

 function posts_related($content){ 
    if (is_single()){

        $custom_query = new WP_Query( array( 
            'posts_per_page' => 8, 
            'post__not_in' => array(get_queried_object_id()), 
            'orderby' => 'rand', 
        ));

        //lets define $related variable outside of while loop
        // and if statement to make it visible 
        $related = '';

    if ( $custom_query->have_posts() ) : 
        while ( $custom_query->have_posts() ) : $custom_query->the_post();

            //use functions with "get_" to recieve a value
            // and not echo it
            $permalink = get_the_permalink();
            $title = get_the_title();
        
            if ( has_post_thumbnail() ) { 

                //here we receiving just url with
                //get_the_post_thumbnail_url()
                $related .= '<a href="' . $permalink .  '">
                                <img src="' . get_the_post_thumbnail_url() . '" />
                             </a>';   
      
            }else{    
                $related .=  '<a href="' .  $permalink . '"><b>' . $title . '</b></a>';
            }
        endwhile;
        wp_reset_postdata();

        //return content + related posts
        //if is_singular and we have related post
        return $content . $related;

    else : 

        //if is_singular but no posts were found
        $related .= '<p>Nothing to show.</p>';

    endif;

    //if is_singular and we have related posts
    //return content + related posts 
    //or content + "nothing to show"
    return $content . $related;
    
}

//if page is not a single,
//just return content
return $content;

}

P.S. Usually related posts are posts with the same category or tag, just orderby with rand will return random posts, but I guess you know this.

7
  • The only problem I'm having is that they're being displayed in the wrong place. I'm trying to display them below the post, not above. I was already returning the content back, maybe I posted the wrong modification.
    – 023023
    Commented May 3, 2021 at 16:20
  • How can it be possible if you return $content . $related; Concatination here returns content first. I checked this code and it returns random posts AFTER the content.
    – anton
    Commented May 3, 2021 at 16:28
  • it is really about using get_the_title() instead of the_title() etc in your code...
    – Michael
    Commented May 3, 2021 at 21:20
  • yes it is, and he got some syntax mistakes in a code as well
    – anton
    Commented May 4, 2021 at 7:22
  • The development software was making a mess with the file, your code is working. I implemented it, but now there's only one problem: the comment section is gone.
    – 023023
    Commented May 4, 2021 at 18:53

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.