Here is what I am trying to do…
I am trying to display the first post only in my header, sort of like a featured post. I have it displaying like I want to, but it’s looping through all of the posts, so instead of showing only one post it’s showing all of the posts in my header.
Here’s my code:
<?php $i = 0; ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php $i++ ?>
<div class="post<?php if ($i == 1) echo ' first'; ?>">
*content to display post here*
</div>
<?php endwhile; ?>
<?php endif; ?>
The easiest way to get the one latest post (in any category) is to use wp_get_archives() | Function | WordPress Developer Resources. Read the docs to change the category, etc.
But if you want to use a WP new query, try this:
Change category_name to your category or delete that variable to show the most recent one post from any category. get rid of the linked title, too, if you don’t want it. Change
to
to show an excerpt instead.
See Function Reference/WP Query « WordPress Codex
And a shortcode that shows the title and link to the most recent post is this; put in
functions.php
:And then use this shortcode in the post/page editor:
[recent-post]
If you know the category ID you can use this:
It will show the latest post in that ID.