1
votes

I administrate a website(www.teknologia.no) running Wordpress. As you can see on the front page I have a "main/featured" article on the top of the page showing the latest post from a specific category. And beneath it I have the main loop showing all the latest posts from all categories.

But as you can see and read from the title, when a posts is chosen to be places in the featured space on the top, it is also shown in the latest posts feed.

My question is as my title say: How can I exclude the newest/latest post in a certain category from appearing with the all the latests posts.

I know I can manually control this by changing the categories after a while and such, but I want it to be done automatically, and I don't know how.

Hope you can spare some time and help me :)

6

6 Answers

4
votes

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.

1
votes

here is a function that does just that:

function get_lastest_post_of_category($cat){
$args = array( 'posts_per_page' => 1, 'order'=> 'DESC', 'orderby' => 'date', 'category__in' => (array)$cat);
$post_is = get_posts( $args );
return $post_is[0]->ID;

}

Usage: say my category id is 22 then:

$last_post_ID = get_lastest_post_of_category(22);

you can also pass an array of categories to this function.

0
votes

Initiate a variable and check inside your loop. A simple way:

$i=0;

while(have_posts() == true)
{
 ++$i;
 if($i==1) //first post
  continue;

 // Rest of the code
}
0
votes

for that you can use

query_posts('offset=1');

for more info : blog

0
votes

Method - 1

$cat_posts = new WP_Query('posts_per_page=1&cat=2'); //first 1 posts
while($cat_posts->have_posts()) { 
   $cat_posts->the_post(); 
   $do_not_duplicate[] = $post->ID;
}

//Then check this if exist in an array before display the posts as following.
 if (have_posts()) {
    while (have_posts()) {

    if (in_array($post->ID, $do_not_duplicate)) continue; // check if exist first post

     the_post_thumbnail('medium-thumb'); 

         the_title();

    } // end while
}

Method - 2

query_posts('posts_per_page=6&offset=1');
if ( have_posts() ) : while ( have_posts() ) : the_post();

This query is telling the loop to only display 5 posts which follow the most recent first post. The important part in this code is “offset” and this magic word is doing the whole thing.

More details from Here

0
votes

Exclude first one From latest five posts

<?php 
   // the query
   $the_query = new WP_Query( array(
     'category_name' => 'Past_Category_Name',
      'posts_per_page' => 5,
              'offset' => 1
   )); 
?>