A monthly archives page can be very useful for blog visitors. In this post i’ll show you how to create monthly archives page in WordPress.
First of all, do you know what is monthly archives? Archives are the records of all your blog posts, so monthly archives is basically a record of all your blog posts which are sorted according to the month in which they are posted.
Creating monthly archives includes steps like creating a page template for WordPress, adding some PHP code in this template, uploading this template to the server and finally setting up the page in WordPress Admin Dashboard. So lets see how to do that –
Note – I am going to perform all these steps on Twenty Eleven WordPress Theme. If you have any problem with any step then feel free to comment on this thread, I will try to help you.
#1 – Find the Text Area Class or Id of your Theme
Th first step is all about finding the id or class of your blog post text area. In this case, we found class=entry-content and we will use it in the next step.
#2 – A Bit Of Coding
Given below is the code to display monthly archives of all blog posts. You can see i am using class=entry-content in this code. So if your text area class is different, you have to replace class=entry-content with your theme’s respective class.
Also you can change header from H2 to H1 or H3 according to your wish.
<?php /* Template Name: Archives - Page */ ?> <?php get_header(); ?> <div class="entry-content"> <div> <?php $numposts = -1; $previous_year = $year = 0; $previous_month = $month = 0; $ul_open = false; $myposts = get_posts(array( 'numberposts' => $numposts, 'orderby' => 'post_date', 'order' => 'DESC' )); ?> <?php foreach($myposts as $post) : ?> <?php setup_postdata($post); $year = mysql2date('Y', $post->post_date); $month = mysql2date('n', $post->post_date); $day = mysql2date('j', $post->post_date); ?> <?php if ($year != $previous_year || $month != $previous_month) : ?> <?php if ($ul_open == true) : ?> </ul> <?php endif; ?> <h2 style="margin-bottom: 0px;"><?php the_time('F Y'); ?></h2> <ul > <?php $ul_open = true; ?> <?php endif; ?> <?php $previous_year = $year; $previous_month = $month; ?> <li><strong><?php the_time('j'); ?></strong>: <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endforeach; ?> </ul> </div> <?php get_sidebar(); ?> <?php get_footer(); ?>
Just copy the code given above, paste it in a notepad file. Save the file as Archive-Page.php.
Then Copy this file or Upload this file inside the WordPress theme folder
In this case i am moving the file inside
and the final target of file will be
#3 – Setting up Archive Page using Admin Dashboard
- Login to your WordPress Blog.
- Go in WordPress Administrators Dashboard.
- Click on Pages > Add new
- Give Title as Archives
- And to the right side, Inside Page attributes, you will find –
- Select Archives – Page and Publish the page.
- Click on View Page Button.
The Archives Page is ready. Check the image below –
It might look different in your WordPress theme. But you can stylize and fix it using simple CSS. So i hope this post helps you. And for more awesome stuff, Subscribe to blog feed.