making my own "related pages" / "pages you might like" section

I'm trying to get the tags of a post on the single content page and displaying a list of other articles that share the tags with the post. For example, the user is reading an article with a tag like 'game'; I want to display the titles of other articles that share the tag 'game'. I'm basically trying to make my own "related pages" section.

How would I do this without using a plugin and which PHP file does the code go? I am using wordpress 3.2.1 and a child theme for the Twenty-Eleven theme.

Topic theme-twenty-eleven tags php Wordpress

Category Web


Function Reference/get the tags « WordPress Codex

Class Reference/WP Query « WordPress Codex#Tag_Parameters

add some code like this to content-single.php of your child theme of Twenty Eleven:

$tags = array();
$posttags = get_the_tags();
if( $posttags ) :
  foreach( $posttags as $tag ) { $tags[] = $tag->term_id; }

  $related_query = new WP_Query( array('tag__in' => $tags));
  if( $related_query->have_posts() ) : 
    while( $related_query->have_posts() ) : 
    $related_query->the_post();
      /*whatever you want to output; for instance:*/
      echo '<a href="'; the_permalink(); echo '">'; the_title(); echo '</a><br />';
    endwhile; 
  endif;
endif;

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.