Title: PHP Code Issue
Last modified: August 19, 2016

---

# PHP Code Issue

 *  [rzamith](https://wordpress.org/support/users/rzamith/)
 * (@rzamith)
 * [17 years, 8 months ago](https://wordpress.org/support/topic/php-code-issue/)
 * Hi there,
 * The thing is: when you open a post on my website, on the sidebar are shown some
   5 links to other posts of the same category. The problem is: when I have that
   post in more than one category, 10 posts are printed (sometimes, repeated posts).
   I would like PHP codex to only print posts from 1 of the categories (doesn’t 
   matter from which one). I am not very much familiar with PHP, but I can see the
   it is the “foreach” loop that is creating this problem. Can someone offer me 
   some suggestions?
 * Please check the code:
 * <?php
    if ( is_single() ) : global $post; $categories = get_the_category(); foreach(
   $categories as $category) : ?>
    - <h2>Random Articles</h2>
       <ul class=”bullets”> <?php $posts = get_posts(‘numberposts
      =5&category=3,4,16’); foreach($posts as $post) : ?>
    - 
    - <?php endforeach; ?>

The topic ‘PHP Code Issue’ is closed to new replies.

## Tags

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

 * 0 replies
 * 1 participant
 * Last reply from: [rzamith](https://wordpress.org/support/users/rzamith/)
 * Last activity: [17 years, 8 months ago](https://wordpress.org/support/topic/php-code-issue/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
