How do i limit the posts i am display to the ONE most recent

Basically on the (custom template) home page I have the main loop is fine.
I have added below it four more divs in each i want to display a post from a diff category.
The most recent one in that specific category, I went and looked at codex & code but can’t find the answer so far & i don’t think i want to limit post numbers in the main user ui

this is what i was trying to use for example,



<?php
			<div class="slot1">
		<?php
		
		query_posts('posts_per_page=1'&&'cat=education');
		while (have_posts()) : the_post();
		the_post_thumbnail();
		the_excerpt();
		endwhile;
		?>
		<?php wp_reset_postdata();?>
	</div>


thx
D

Use WP_Query(). Your code would be something like this -

$args = array(
			  'category_name' 	=> 'education',
			  'posts_per_page'	  => 1
		  );
$my_query = new WP_Query( $args );

if ($my_query->have_posts()) : while ($my_query->have_posts()) : $my_query->the_post(); 

	the_post_thumbnail();
        the_excerpt(); 

endwhile;
endif;

wp_reset_query(); 

By the way, the ‘category_name’ takes the category slug, not the actual name of the category. In otherwords, all lowercase and multiple word categories have hyphens between the words. For example, the category slug for ‘New Topic’ would be ‘new-topic’.

Thank you WebMachine! will go try it out.
have a stupid question or two i guess…
is

$args

actually part of the wp code? when i see it in the codex I am 100% sure if it is meant to be replaced w/my own variable.

When it comes to the slug, i can then give the same slug to various posts? I see that when it is checked in the screen options checkboxes it auto fills in w/that post’s or page’s title.

thank you
D

In my code, the variable $args is defined at the very beginning: as an array containing two key-value pairs: ‘category_name’ => ‘education’, and ‘posts_per_page’ => 1.
Then when I made the new WP=Query object, instead of listing the arguments inside the (), I used the variable $args. It just makes it easier to keep things organized if you
have more than one argument. You can name it anything you want, but I like to use $args because it is short for arguments.

Use the slug of whatever category you want to display the most recent post for. So in each of your four or five different divs, you would use a different category slug.

Thank you!
D

this is really great. I notice that it also seems to work w/categories & i don’t have to change the slug.
& i was noticing as well that when you change the slug it changes the hyperlink.
D

There is a lot you can do with WordPress. That’s why I like it. :slight_smile: The learning is endless (and fun). I’m glad this is working for you.

Yes thank you it is really quite impressive. Definitely not without challenges! :slight_smile:
D

Say WebMachine have a quick question for you,

I have noticed that sometimes the code you helped w/will just not bring up a category. It’s not your code as in some case it was bringing it up just fine the day before.
so i have added a second category tag, to call up a second post if the first is not there.


<div id="slot12" class="slot">
		<?php
			$args = array(
			[COLOR="#FF0000"]'category_name' => 'factoids',[/COLOR] (this quit showing up)
			[COLOR="#008000"]'category_name' => 'health',[/COLOR]       (this shows)
			'posts_per_page' => 1
					);
			$my_query = new WP_Query( $args );
			if ($my_query->have_posts()) : while ($my_query->have_posts()) : $my_query->the_post(); ?>
	<div class="slotHead"> <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></div>
	<div class="slotCat">  <?php the_category(); ?> </div>
         	<?php
			the_post_thumbnail();
			the_excerpt();
       			 endwhile;
       			 endif;
			wp_reset_query();
		?>
	</div>


so this works but i was wondering if this would be the best way to do it?
Thx
D

When you tested the category ‘factoids’, was there actually at least one post using this category? Usually I use a conditional clause to test whether the category is empty, and if so, I have a plan B.

In key-value pairs (in arrays), you can only have one value for each key.

'[COLOR="#FF0000"]category_name[/COLOR]' => 'factoids', (this quit showing up)
			'[COLOR="#FF0000"]category_name[/COLOR]' => 'health',       (this shows)

So listing category_name twice like you have in your posted code above will not work. I believe the second one will override the first one, and this query will only look for posts that have category_name of ‘health’.

I have added below it four more divs in each i want to display a post from a diff category.

In each section where you want to display the most recent post, you have to have a separate post query.

yep. i have a post w/category of “factoids” before that it was “did-you-know” which worked for a while on my sandbox. then quit.
i also figured that it might be an override scenario. Will try a conditional clause then.
Thx for the advice
D