1

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 :)

Lund
  • 41
  • 1
  • 6

6 Answers6

4

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.

Community
  • 1
  • 1
Duncan Lock
  • 12,351
  • 5
  • 40
  • 47
  • Thanks, but how do I make sure the latest posts loop always checks that $top_post_id always contains the ID of the latest post from a certain category? – Lund Oct 15 '13 at 22:02
  • I assumed that your latest posts loop already works - if that's the case, then you don't have to - just save the value returned by get_the_id() or $post->ID inside the featured posts loop where you're output the top post - that's the ID of the post you're outputting and the one you want to exclude later. Basically, when you output the post at the top, save it's ID, then exclude this ID later. – Duncan Lock Oct 15 '13 at 23:42
  • The problem is that the featured post is gotten from a different template using (get_template_part( 'includes/feat-slider' )) And therefor the featured post loop is not in the same file as the latest post loop. So I if there is a way to always get the ID of the latest post in a certain category. – Lund Oct 16 '13 at 10:33
  • I've updated my answer to show how to get the ID of the latest post in a given category directly, if you can't save it during a loop. – Duncan Lock Oct 16 '13 at 19:47
  • If the answer fixed your problem, upvoting it will help other people know that it worked, helping others who might have a similar issue in the future. – Duncan Lock Oct 18 '13 at 09:59
1

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.

Said Erraoudy
  • 1,490
  • 1
  • 13
  • 21
0

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
}
seoul
  • 864
  • 1
  • 12
  • 32
0

for that you can use

query_posts('offset=1');

for more info : blog

Bhumi Shah
  • 9,323
  • 7
  • 63
  • 104
0

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

Sumith Harshan
  • 6,325
  • 2
  • 36
  • 35
0

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
   )); 
?>
Billu
  • 2,733
  • 26
  • 47