• 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 the_title(); ?>
    • <?php endforeach; ?>

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