Title: Adding Code for Pages
Last modified: August 30, 2016

---

# Adding Code for Pages

 *  Resolved [digitalcam](https://wordpress.org/support/users/digitalcam/)
 * (@digitalcam)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/adding-code-for-pages/)
 * The following code is in my content.php (child theme) and it returns 5 related
   posts based on tags.
 *     ```
       <?php
       //for use in the loop, list 5 post titles related to first tag on current post
       $tags = wp_get_post_tags($post->ID);
       if ($tags) {
   
       $first_tag = $tags[0]->term_id;
       $args=array(
       'tag__in' => array($first_tag),
       'post__not_in' => array($post->ID),
       'posts_per_page'=>5,
       'caller_get_posts'=>1
       );
       $my_query = new WP_Query($args);
       if( $my_query->have_posts() ) {
       ?>
       <related class="related">
       <?php
       echo '<h3>You May Also Enjoy...</h3>';
       while ($my_query->have_posts()) : $my_query->the_post(); ?>
   
       <ul><li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li></ul>
       <?php
       endwhile;
       ?>
       </related>
       <?php
       }
       wp_reset_query();
       }
       ?>
       ```
   
 * I need to add Pages to the code so on my Pages it returns the 5 related based
   on tag.
 * Suggestions?

Viewing 1 replies (of 1 total)

 *  Thread Starter [digitalcam](https://wordpress.org/support/users/digitalcam/)
 * (@digitalcam)
 * [10 years, 11 months ago](https://wordpress.org/support/topic/adding-code-for-pages/#post-6226319)
 * Resolved

Viewing 1 replies (of 1 total)

The topic ‘Adding Code for Pages’ is closed to new replies.

 * ![](https://i0.wp.com/themes.svn.wordpress.org/twentyfourteen/4.5/screenshot.
   png)
 * Twenty Fourteen
 * [Support Threads](https://wordpress.org/support/theme/twentyfourteen/)
 * [Active Topics](https://wordpress.org/support/theme/twentyfourteen/active/)
 * [Unresolved Topics](https://wordpress.org/support/theme/twentyfourteen/unresolved/)
 * [Reviews](https://wordpress.org/support/theme/twentyfourteen/reviews/)

## Tags

 * [content php](https://wordpress.org/support/topic-tag/content-php/)

 * 1 reply
 * 1 participant
 * Last reply from: [digitalcam](https://wordpress.org/support/users/digitalcam/)
 * Last activity: [10 years, 11 months ago](https://wordpress.org/support/topic/adding-code-for-pages/#post-6226319)
 * Status: resolved