Forums

PHP Code Issue (1 post)

  1. rzamith
    Member
    Posted 3 years ago #

    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) :
    ?>

  2. <h2>Random Articles</h2>
    <ul class="bullets">
    <?php
    $posts = get_posts('numberposts=5&category=3,4,16');
    foreach($posts as $post) :
    ?>
  3. "><?php the_title(); ?>
  4. <?php endforeach; ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags