Support » Developing with WordPress » Trying to show related posts without a plugin

  • I’m trying to display the related posts using functions.php with this code:

    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).

    Can anyone give me a hand?

Viewing 5 replies - 1 through 5 (of 5 total)
  • Joy

    (@joyously)

    It would be easier to just get a plugin and read the code (if you are trying to learn) or a theme that does this already.
    You’ve got several problems in your code.
    1. Your function is being set as a filter for the_content, and filters should not output anything. They only modify (or not) the input variable.
    The functions that start with the_ will echo their result, like the_post_thumbnail and the_title. (not to mention the debug echo you put in there)
    2. There is no need to use WP_Query, and it would be better to use get_posts since it returns an array of data.
    https://developer.wordpress.org/reference/functions/get_posts/
    3. Your query arguments are not sufficient to find “related” posts.
    4. The set_post_thumbnail_size and add_theme_support calls should be on the ‘after_setup_theme’ action, but yours are not in any function, so they will run when the theme is loaded. The size will be overwritten by the theme in its setup function. You don’t actually need the to set the size, since you can pass the dimensions to the function to get the thumbnail.
    https://developer.wordpress.org/reference/functions/set_post_thumbnail_size/
    https://developer.wordpress.org/reference/functions/get_the_post_thumbnail/

    Thread Starter spikespiegel

    (@spikespiegel)

    This code was modified from the same code presented in many known wordpress blogs, including wpbeginner blog. I haven’t found much resources online about this, and now I’m even more confused with your answer, because you made it look like everything is wrong with the code. If that code is so bad, why is the same code in so many tutorials?
    And I’m not going to use an entire plugin just for showing the related posts of a post, it’s too risky.

    Joy

    (@joyously)

    We have no control over external web sites and the examples they use. Just because something is prevalent doesn’t mean it’s good code.

    There is less risk in using a plugin from the WP repository than in writing it yourself with limited knowledge. The plugins in the repository are reviewed for security problems prior to acceptance. And the author usually tests the code and updates when problems are found. This is work you would have to do yourself if you don’t use a plugin.
    And, if you put the code in the theme, it disappears when you switch themes.

    Thread Starter spikespiegel

    (@spikespiegel)

    And who claimed you have control over other websites? Even so it doesn’t despite the fact that a lot of people are using the same code that uses the api.
    Also, how come using a plugin is much safer than writing a piece of code using Worpdress api? What you said doesn’t have logic at all.

    Well, you don’t seem to be willing to help at all, you look like you’re into something else. I’ll look for help somewhere else.

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Forum Moderator & Support Team Volunteer

    @spikespiegel Having your code criticized is painful; we’ve all been there. I suggest you try to put that aside and consider what Joy said regarding quality of code and depth of knowledge.

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.