I want to display posts links from a category group by year. Group by year becouse by default wordpress repeat the date for each post. I try use a code but I got all post in current year. How can I do it?
Example I want to do:
2010
- post link 20
- post link 19
- post link 18
- …
- post link 8
2009
- post link 7
- post link 6
- …
The code:
<?php
query_posts(array('nopaging' => 1, /* desabilitar a paginacao pata obter todos os pots. O padrao e ordenado pela data */));
$prev_year = null;
query_posts('cat=27');
if ( have_posts() ) {
while ( have_posts() ) {
$this_year = get_the_date('Y');
if ($prev_year != $this_year) {
// Year boundary
if (!is_null($prev_year)) {
// A list is already open, close it first
echo '</ul>';
}
echo '<h2 class="titulo-conteudo">'. $this_year . '</h2>';
echo '<div class="barra-amarela-4"></div>';
echo '<ul>';
}
echo '<li>';
// Imprimi o link do post.
the_post(); ?>
<div class="entry">
<h2 id="post-<?php the_ID(); ?>">
<a href="<?php the_permalink(the_title()) ?>"><?php the_title(); ?></a></h2>
</div>
<?php
echo '</li>';
$prev_year = $this_year;
}
echo '</ul>';
}
?>
I wrote that original code on Stack Overflow, but I didn’t see your further replies because you posted them as answers and not as comments to my answer. I have tested the code now with a specific category and it works for me, but it needs one crucial change: the call to
the_post()
(which I completely forgot) must come right at the beginning of thewhile ( have_posts() )
loop, otherwise the year will always lag one post behind. I have corrected that in the original answer.If you want to specify multiple criteria for your query, you must combine them in the same function call. So not
query_posts('cat=27'); query_posts('nopaging=1');
, butquery_posts('cat=27&nopaging=1')
. You can also use the array format (as in my original example), I prefer that for readability.A last change: if this is not the main loop of your page (and I suspect this code will end up in a sidebar, so not the main loop), [it is better not to use
query_posts()][2]
. Instead, tryget_posts()
and use the result of that. I did not know this when I wrote the original answer, but hanging around on this site learns you a lot!What you have looks to work well.. Since I was on here looking for solutions to another question I posted and came across this one, I thought I’d add how I accomplished this in one of my sites:
This eliminates multiple query_posts calls and is really easy to control styling, etc.. hope this helps someone who may be wanted to see various solutions to this 🙂
Great work again, on your orig. solution.
You need to run a posts query that gives both the category and year values (as well as your nopaging, which is best done with posts_per_page=-1)
See the codex article for query_posts() for full details about prarameters you can pass into new WP_Query().
Here’s the code for your question:
Your code got very broken when posted. From what I can see multiple
query_posts()
are bad idea in most cases. Please correct your code if you want better assessment.Personally I would use plugin for this, there are plenty of excellent archive plugins around. I currently play with Smart Archives Reloaded. It allows easily to get posts by month/year in specific category:
smart_archives( array(
'format' => 'list',
'include_cat' => 27
));