Display WordPress Post from a Category with Excluding Current Post
Code for Display WordPress Post from a Category with Excluding Current Post. Change category slug ‘news’ to your own category slug.
<?php
/* main post's ID, the below line must be inside the main loop */
$exclude = get_the_ID();
/* alternatively to the above, this would work outside the main loop */
global $wp_query;
$exclude = $wp_query->post->ID;
/* secondary query using WP_Query */
$args = array(
'category_name' => 'news', // note: this is the slug, not name!
'posts_per_page' => -50 // note: showposts is deprecated!
);
$your_query = new WP_Query( $args );
/* loop */
while( $your_query->have_posts() ) : $your_query->the_post();
if( $exclude != get_the_ID() ) {
echo '<h3><a href="' . get_permalink() . '">' .
get_the_title() . '</a></h3>';
echo '<p>' .get_the_excerpt() . '</p>';
}
endwhile;
?>
