Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 998443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:11:44+00:00 2026-05-16T07:11:44+00:00

i have 4 posts belonging to news category. I’ve added a this code in

  • 0

i have 4 posts belonging to “news” category. I’ve added a this code in single.php to show the title of other posts in the same category in the sidebar.

<?php
    $query = "showposts=10&orderby=rand&cat=";      
    foreach((get_the_category()) as $category) { 
    $query .= $category->cat_ID .","; 
    }       
    query_posts($query);
?>
<ul class="related grid_5">
    <?php while (have_posts()) : the_post(); ?>
        <li><?php the_title() ?></li>
     <?php endwhile; ?>      
</ul>

using this i can retrieve all posts in the same category and display them and all 4 titles belonging to “news” are displayed in the widget. but I’m on post#3 how can show only #1, #2, and #4 titles in the widget?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T07:11:44+00:00Added an answer on May 16, 2026 at 7:11 am

    If you captured the ID of your currently displayed post, you could add a conditional check to avoid displaying it in the while loop.

    1. Get ID of current post

    I’m assuming you’re outside the loop as you’re in your sidebar, so you could get the current post ID like so:

    global $wp_query;
    $currentPost = $wp_query->post->ID;
    

    However, if you are in the loop, the following will work:

    global $post;
    $currentPost = $post->ID;
    

    2. Skip printing of current post’s title

    Once you’ve got the current post’s ID, skipping the print is pretty straightforward:

    <ul class="related grid_5">
        <?php while (have_posts()) : the_post(); 
            if( $post->ID != $currentPost ) ?>
                <li><?php the_title() ?></li>
        <?php endwhile; ?>      
    </ul>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have seen posts that show three ways to reset the DMV views: Reset
I have 2 tables: posts tags Tags table is structured like this: post_id tag
Say I have a post and category model, with each post belonging to a
I have a Category and a Post model, with each Post belonging to a
What's the best way to show just 2 posts in the loop? <?php $i
Say I have a blogging app in Django. How can i re-order the posts
I have read posts like these: What is a metaclass in Python? What are
I've encountered posts which have stated, I'd love to use Extension A but it
I have read in some of the ClickOnce posts that ClickOnce does not allow
I have a database table called Posts which stores all the information regarding an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.