Home > Wordpress > How to Create Archives Page without Any Plugin in Wordpress

How to Create Archives Page without Any Plugin in Wordpress

The archives page can show how many articles you have been write, and when you wrote this articles. There are many wordpress plugin that can build your wordpress archives page, such as Clean Archives Reloaded, WP-EasyArchives and so on.

If you do not like to use so much wordpress plugin, you can build the archives page yourself. Do some copy-edit to your page.php file, you can have your own archives page.

WordPress_archives-1

1. Copy you page.php file of your wordpress theme and rename it as archives.php. Add this code to the header of the file before <?php get_header(); ?>.

<?php
/*
Template Name: Archives
*/
?>

2. Add this code below the function <?php the_content(); ?>, or replace the function as you like.

<?php
// echo archives start
$lastpost = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_date <'" . current_time('mysql') . "' AND post_status='publish' AND post_type='post' AND post_password='' ORDER BY post_date DESC LIMIT 1");
$output = get_option('hfy_archives_'.$lastpost);
 
if(empty($output)){
 $output = '';
 $wpdb->query("DELETE FROM $wpdb->options WHERE option_name LIKE 'hfy_archives_%'");
 // Get all of the months that have posts
 $monthquery = "SELECT DISTINCT YEAR(post_date) AS year, MONTH(post_date) AS month, count(ID) as posts FROM " . $wpdb->posts . " WHERE post_date <'" . current_time('mysql') . "' AND post_status='publish' AND post_type='post' AND post_password='' GROUP BY YEAR(post_date), MONTH(post_date) ORDER BY post_date DESC";
 $monthresults = $wpdb->get_results($monthquery);
 
 if ($monthresults) {
 // Loop through each month
 foreach ($monthresults as $monthresult) {
 $thismonth    = zeroise($monthresult->month, 2);
 $thisyear    = $monthresult->year;
 
 // Get all of the posts for the current month
 $postquery = "SELECT ID, post_date, post_title, comment_count FROM " . $wpdb->posts . " WHERE post_date LIKE '$thisyear-$thismonth-%' AND post_date AND post_status='publish' AND post_type='post' AND post_password='' ORDER BY post_date DESC";
 $postresults = $wpdb->get_results($postquery);
 
 if ($postresults) {
 // The month year title things
 $text = sprintf('%s %d', $month[zeroise($monthresult->month,2)], $monthresult->year);
 $postcount = count($postresults);
 $output .= '<p>' . $text . '&nbsp;(&nbsp;<span>' . count($postresults) . '</span>&nbsp;posts&nbsp;)</p>';
 $output .= "<ul class='archiveslist'>\n";
 
 foreach ($postresults as $postresult) {
 if ($postresult->post_date != '0000-00-00 00:00:00') {
 $url = get_permalink($postresult->ID);
 $arc_title    = $postresult->post_title;
 if ($arc_title)
 $text = wptexturize(strip_tags($arc_title));
 else
 $text = $postresult->ID;
 $title_text = 'read more &quot;' . wp_specialchars($text, 1) . '&quot;';
 $output .= '    <li>' . mysql2date('m-d', $postresult->post_date) . ':&nbsp;' . "<a href='$url' title='$title_text'>$text</a>";
 $output .= '&nbsp;(' . $postresult->comment_count . ')';
 $output .= "</li>\n";
 }
 }
 $output .= "</ul>\n\n";
 }
 }
 update_option('hfy_archives_'.$lastpost,$output);
 }
 else{
 $output = '<strong>'. __('ERROR:') .'</strong> '. __('No items were found to be displayed.') .'';
 }
}
 echo $output;
//echo archives end
?>

When you finish this coding, upload the archives.php file to your wordpress theme directory. At this point the archives functions have been created, and you can use it to show your archives posts.

archives-page

Archives Template

Go to the dashboard, create a new page name as Archives and select the Archives as the page template. Click to see your new page after you publish it. You can see the archives page show all your wrote post by post.

The archives page can list the articles monthly and show the comment number at the end of each post. The list will update when you post a new articles, so don’t be confused when the list show the wrong comment number.

Sep 2nd, 2009 | Posted in Wordpress
  1. Oct 23rd, 2009 at 04:23 | #1

    The fact is the only people who know are Wenger and the board. ,

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">
Loading Comment...