Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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="<?php the_permalink(); ?>">
          <?php the_title(); ?>
          </a>
        <?php endwhile; ?>
share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

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.

share|improve this answer
    
Hi, Thanks but I got a blank page :( –  user983248 May 23 '12 at 12:44
    
You're inserting into single.php, right? Did you wrap the entire above snippet in <?php opening and closing (?>) tags? If implemented correctly, the above will only alter a tiny bit of your post view. It should not influence the whole page. Oh, and see the edit by @Geert - I had missed bracket ( } ). Thanks Geert. –  Johannes Pille May 23 '12 at 12:47
    
Yes, I'm cosing the tags, but I get nothing, in fact all after the code stop to work, like the footer and other stuff from the single.php file. You code meant to replace the loop on single.php ? Should I paste the code after or before the loop? –  user983248 May 23 '12 at 12:56
    
The last edited code didn't work, Still breaking things, like the footer (it will not load) –  user983248 May 23 '12 at 13:03
    
It's not meant to replace the main loop. You need to display the other posts in a secondary loop. –  Johannes Pille May 23 '12 at 13:12
show 2 more comments

Based on Johannes’ code, but using the post__not_in argument:

/* Secondary query using WP_Query */
$wpse63027_posts = new WP_Query( array(
    'category_name'  => 'MyCatName',
    'posts_per_page' => -1,
    'post__not_in'   => array( get_queried_object_id() ), // Exclude current post ID (works outside the loop)
) );

Then you can loop through the new posts:

if ( $wpse63027_posts->have_posts() )
{
    while( $wpse63027_posts->have_posts() )
    {
        $wpse63027_posts->the_post();

        // Now do everything you want with the default API calls
        // Example
        the_title( '<h2>', '</h2>', true );
        the_content();
    }
}

WP_Query » Post_*/Page_Parameters

share|improve this answer
2  
Why would anybody downvote this addition? It's a good suggestion, ain't it? –  Johannes Pille May 23 '12 at 13:12
1  
@JohannesPille That's what I just did. –  kaiser Oct 17 '12 at 20:02
1  
Now this had definitely become the most complete answer to this one. And, @kaiser, there's get_queried_object_id() showing up again! I've seen that one once already today, can't remember where. :) –  Johannes Pille Oct 17 '12 at 23:03
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.