I’m trying to get a list of posts from one category, echo the title and the permalink of all posts on that category but exclude the permalink and the post name IF the current post is on that list.
It will be displayed on single.php after or inside the loop
Anyone knows if it is possible, if so how?
Thanks in advance
<?php query_posts('category_name=MyCatName&showposts=-1'); ?>
<?php while (have_posts()) : the_post(); ?>
<a href="https://wordpress.stackexchange.com/questions/53027/<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
<?php endwhile; ?>
First off, note that, since your custom loop is a secondary loop/query, you should use the WP_Query
class instead of query_posts()
. Read why.
That being said,
/* 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' => 'MyCatName', // note: this is the slug, not name!
'posts_per_page' => -1 // note: showposts is deprecated!
);
$your_query = new WP_Query( $args );
/* loop */
echo '<ul>';
while( $your_query->have_posts() ) : $your_query->the_post();
if( $exclude != get_the_ID() ) {
echo '<li><a href="' . get_permalink() . '">' .
get_the_title() . '</a></li>';
}
endwhile;
echo '</ul>';
that’ll do.