Put the Categories, Archives and All Posts into Pages

By Zhiqiang Ma On Nov 27, 2012

I prefer putting pages that contains all the categories, archives and even all the posts in one page to putting these links in the side bar. Actually, most of time it needn’t to stay on every pages. And if it is in the side bar, the search engine will see these links in every page and think it is very important while it is not so important. So the better way is put them in one or several pages.

But unfortunately, WordPress doesn’t provide a simple way to put several pages and put some part like the widget into it. So I write some php script that will call WordPress’s API to put them input pages.

The page template is like this. This template enable the theme and load the common files that contains WordPress’s API functions. Then it generate the header, sidebar and the footer. What we need to do is add the content to replace “TODO: content here” now.

<?php
/**
* Loads the WordPress environment and template.
*/
define('WP_USE_THEMES', true);
if ( !isset($wp_did_header) ) {
  $wp_did_header = true;
  require_once( dirname(FILE) . '/../wp-load.php' );
}
?>

<?php getheader(); ?> <div id="container"> TODO: Content here. </div> <?php getsidebar(); ?> <?php get_footer(); ?> 1. Categories

The code for categories is:

<div>
<h1><?php e('Categories', 'default'); ?></h1>
<div>
<ul>
  <?php wplistcategories('titleli=&showcount=1'); ?>
</ul>
</div>
</div>
wplistcategories() can list all the categories as a list. “showcount=1″ means the post number under each category will be shown after the category.

2. Archives

The code is:

<div>
<h1><?php e('Archives by Month', 'default'); ?></h1>
<div>
<ul>
  <?php wpgetarchives('type=monthly&showpostcount=1'); ?>
</ul>
</div>
</div>
wpget_archives() will show the archives. the type can be changed to weekly, daily to show archives by week or day.

3. All posts

The code:

<div>
<h1><?php e('All posts', 'default'); ?></h1>
<div>
<ul>
<?php
  //The Query
  queryposts('postsperpage=1000');
  //The Loop
  if ( haveposts() )
  {
    while ( haveposts() )
    {
      thepost();
?>
<li>
<a href="<?php thepermalink() ?>" rel="bookmark"
title="Permanent Link to <?php thetitleattribute(); ?>">
<?php thetitle(); ?></a></li>
<?php
_e('</li>');
    }
  }
  //Reset Query
  wpreset_query();
?>
</ul>
</div>
</div>
At most 1000 posts’ titles will be listed.

Put these php files into the site, edit the theme files and then enjoy it! :)

By: Zhiqiang Ma Last updated: Nov 27, 2012 Views: 322
Tags: , ,

About Zhiqiang Ma

Zhiqiang Ma is a PhD candidate at Dep. of CSE, HKUST. He is interested in system software for cloud computing, virtualization of large-scale distributed system, etc. Find Zhiqiang on Facebook, Twitter, LinkedIn and Google+.

Like this post? Subscribe Fclose.com's RSS feed.
Have questions not covered here? Ask Fclose.
Add your comments, share your thoughts

Be nice. Keep it clean. Stay on topic. No spam.