Hie all, I am trying to list posts by year based on the provided url (for example: wordpressite/2004 - this should list all articles created in 2004), unfortunately all suggested methods from previous solutions solve the problem if the year to be queried is given.
I tried creating the year.php file suggested in the wordpress hierarchy as well as date.php, but the_loop() just gives everything, not listing articles from the specific year.
<?php
query_posts('posts_per_page=10');
if ( have_posts() ) : while ( have_posts() ) : the_post();
get_template_part( 'archive-content', get_post_format() );
endwhile; endif;
wp_reset_query();
?>
And this is the individual article I would like to have
<div class="row" style="margin-top: 15px;">
<a href="<?php the_permalink() ?>">
<div class="col col-sm-3 remove-left-margin">
<?php $url = wp_get_attachment_url( get_post_thumbnail_id(get_the_ID()), 'thumbnail' ); ?>
<img src="<?php echo $url ?>" class="archive-card-image" />
</div>
<div class="col col-sm-9">
<div><p class="archive-card-date"><?php the_date(); ?></p></div>
<?php
$categories = get_the_category();
if ( ! empty( $categories ) ) {
echo '<div class="card-article-tag"><a class="archive-card-topic" href="' . esc_url( get_category_link( $categories[0]->term_id ) ) . '">' . esc_html( $categories[0]->name ) . '</a></div>';
}
?>
<div><h4 class="archive-card-title"><b><?php the_title(); ?></b></h4></div>
</div>
</a>
</div>
I am still very new to wordpress, would appreciate any help.
wp_get_archives('type=yearly')
The documentation does not state how I can use my article structure mentioned above.
Hie all, I am trying to list posts by year based on the provided url (for example: wordpressite/2004 - this should list all articles created in 2004), unfortunately all suggested methods from previous solutions solve the problem if the year to be queried is given.
I tried creating the year.php file suggested in the wordpress hierarchy as well as date.php, but the_loop() just gives everything, not listing articles from the specific year.
<?php
query_posts('posts_per_page=10');
if ( have_posts() ) : while ( have_posts() ) : the_post();
get_template_part( 'archive-content', get_post_format() );
endwhile; endif;
wp_reset_query();
?>
And this is the individual article I would like to have
<div class="row" style="margin-top: 15px;">
<a href="<?php the_permalink() ?>">
<div class="col col-sm-3 remove-left-margin">
<?php $url = wp_get_attachment_url( get_post_thumbnail_id(get_the_ID()), 'thumbnail' ); ?>
<img src="<?php echo $url ?>" class="archive-card-image" />
</div>
<div class="col col-sm-9">
<div><p class="archive-card-date"><?php the_date(); ?></p></div>
<?php
$categories = get_the_category();
if ( ! empty( $categories ) ) {
echo '<div class="card-article-tag"><a class="archive-card-topic" href="' . esc_url( get_category_link( $categories[0]->term_id ) ) . '">' . esc_html( $categories[0]->name ) . '</a></div>';
}
?>
<div><h4 class="archive-card-title"><b><?php the_title(); ?></b></h4></div>
</div>
</a>
</div>
I am still very new to wordpress, would appreciate any help.
wp_get_archives('type=yearly')
The documentation does not state how I can use my article structure mentioned above.
query_posts()
Please don't use
query_posts('posts_per_page=10');
in your date.php
template file as it will override the main query instance and thus affect the main loop. This can truly ruin the day!
If you need to modify e.g. the posts_per_page
for the main date query, use the pre_get_posts
action to modify the query variables, before the database request is made.
There's a harsh warning on query_posts()
usage in the manual.
So remove that part toghether with wp_reset_query()
and it should work again!
Here's an example how to change the posts per page for the yearly date archive:
add_action( 'pre_get_posts', function( $query ) {
if ( is_admin() || ! $query->is_main_query() || ! $query->is_year() ) {
return;
}
$query->set( 'posts_per_page', 10 ); // Adjust the number to your needs!
} );
Here we use the is_year()
to target the yearly archive queries and the is_main_query()
to target only main queries.
date-year.php
template file.We can also add a support for the date-year.php
template file, by modifying the template filter example from the manual:
/**
* Add a support for date-year.php template files.
*/
add_filter( 'date_template', function( $templates ) {
if ( ! is_year() ) {
return $templates;
}
if ( ! is_array( $templates ) && ! empty( $templates ) ) {
$templates = locate_template( [ "date-year.php", $templates ], false );
} elseif ( empty( $templates ) ) {
$templates = locate_template( "date-year.php", false );
} else {
$new_template = locate_template( [ "date-year.php" ] );
if ( ! empty( $new_template ) ) {
array_unshift( $templates, $new_template );
}
}
return $templates;
} );
That way we are only working with the yearly date archive and not the others, like month and day if using the date.php
template file.
Add the code snippets into your functions.php
file in the current theme directory or create a plugin. Never copy/paste some code snippets from the internet directly to a live site. Always test first on dev/local installs.
query_posts
is no longer considered good practice. – birgire Commented Feb 14, 2019 at 8:59query_posts
and you mentioned it supports yearly archive out of the box, it doesn't seem to work in both my date.php and year.php both files use the code in the question. – Mark20 Commented Feb 14, 2019 at 9:02wordpressite/2019
to list articles from the year 2019? We can check the year archive on WordPress installs like blog.wordpress.tv/2019 and wptavern/2019. WordPress uses the main query to generate this and if we need to modify the main query we can use e.g.pre_get_posts
action, but I don't see a need for it here. If we need to change the corresponding theme template we can look at the template hierarchy, like you've done here. – birgire Commented Feb 14, 2019 at 9:20wordpressite/2019
shows results from 2018 and 17 as well. – Mark20 Commented Feb 14, 2019 at 9:30