Quantcast
Channel: Exclude only first post from one category from the latest posts feed, wordpress - Stack Overflow
Viewing all articles
Browse latest Browse all 7

Answer by Duncan Lock for Exclude only first post from one category from the latest posts feed, wordpress

$
0
0

You would need to update the template's logic so that the main loop skips outputting the post that's output at the top.

Without seeing your template code, it's hard to be specific, but something like this would probably work:

In the top section, save the ID of the post that you're outputting:

$exclude_post_id = get_the_ID();

If you need to directly fetch the ID of the latest post in a given category, rather than saving it during the loop, you can do it like this instead, using WP_Query:

$my_query = new WP_Query('category_name=my_category_name&showposts=1');
while ($my_query->have_posts()):
    $my_query->next_post();
    $exclude_post_id = $my_query->post->ID;
endwhile;

Then, in the main loop, either alter the query to exclude that post:

query_posts(array('post__not_in'=>$exclude_post_id));

or manually exclude it inside the loop, something like this:

if (have_posts()): 
    while (have_posts()):
        the_post();
        if ($post->ID == $exclude_post_id) continue;
        the_content();
    endwhile;
 endif;

More information here, here and here.


Viewing all articles
Browse latest Browse all 7

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>