January 24, 2006

WordPress Archive Lists

WordPress — Sarah King

One thing I haven't become involved with is writing plugins for WordPress but I have been looking at ways to make my blog more efficient.

I tried out a tag system back in October but found that it generated too many tags and often irrelevant tags so finally scrapped it in favour of the plugins from Ultimate Tag Warrior produced by fellow kiwi Christine Davis (who I don't actually know ;)). Her system allowed me to produce a "Tag Cloud" which is pretty cool.

I haven't totally got my head around the custom tags page with url rewriting but for now I'm happy with urls like /index.php?tag=kiwi and I've learnt a little about templates.

So when I posted some info about the UTW plugin at DigitalPoint one of the members asked about Category Archives and gave a new plugin to use. I took a look and couldn't quite work out how to use it, so took the lessons from UTW and applied them!

SKArchive

I didn't want to replace the default pages when I click on the category name, or change the core template so I'm calling this "SKArchive". First, check out my wordpress archive page.

  1. Create a "skarchive.php" in your template folder (contents below) and upload
  2. In the Admin control panel create a new page and change the template to SKArchive. Save.
  3. Now, view your blog and it should all be there. Tweak as necessary.

The Code

PHP:
  1. <?php
  2. /*
  3. Template Name: SKArchive
  4. */
  5. ?>
  6. <?php get_header(); ?>
  7. <?php
  8.  
  9. /**
  10. * @return string
  11. * @param int $catID
  12. * @desc returns a list of posts for a category
  13. */
  14. function wp_cat_posts( $catID )
  15. {
  16.     global $wpdb;
  17.     $output = '';
  18.    
  19.     $get_posts_in_cat = "SELECT {$wpdb->posts}.ID, {$wpdb->posts}.post_title,
  20.         {$wpdb->post2cat}.post_id, {$wpdb->post2cat}.category_id
  21.         FROM `{$wpdb->posts}`, `{$wpdb->post2cat}`
  22.         WHERE {$wpdb->posts}.ID = {$wpdb->post2cat}.post_ID
  23.         AND {$wpdb->post2cat}.category_id = '$catID'
  24.         AND {$wpdb->posts}.post_status = 'publish'
  25.         ORDER BY {$wpdb->posts}.post_date desc ";
  26.            
  27.     $get_posts_in_cat_result = mysql_query($get_posts_in_cat);
  28.  
  29.     if (mysql_num_rows($get_posts_in_cat_result)> 0)
  30.     {
  31.         $output = '<ul>';
  32.         while ($posts_in_cat_row = mysql_fetch_assoc($get_posts_in_cat_result))
  33.         {   
  34.             $post_title = $posts_in_cat_row['post_title'];
  35.             $postID = $posts_in_cat_row['ID']
  36.                    
  37.             $output .= "<li><a href='" . get_permalink($postID) . "'>{$post_title}</a></li>\n";  
  38.         }
  39.         $output .= '</ul>';
  40.     }      
  41.     return $output;
  42. }//wp_cat_posts
  43.  
  44. global $wpdb;
  45.  
  46. $get_cat = "SELECT *
  47.     FROM `{$wpdb->categories}`
  48.     order by cat_name";
  49.            
  50. $get_cat_result = mysql_query($get_cat);
  51.  
  52. while ($cat_row = mysql_fetch_object($get_cat_result))
  53. {   
  54.     $link = '<a href="'.get_category_link($cat_row->cat_ID).'">';
  55.     $link .= apply_filters('list_cats', $cat_row->cat_name, $cat_row).'</a>';
  56.            
  57.     echo "<p><b>{$link}</b></p>" . wp_cat_posts($cat_row->cat_ID);   
  58. }      
  59. ?>
  60. <?php get_footer();
  61.  
  62. // Credits
  63. //========
  64. //Brian Groce
  65. //http://briangroce.com/
  66. ?>

Also on this site

2 Comments »

  1. Sarhak, do u plz tell How i add wordpress theme as well as admin panel in my “.com” site. ???

    Comment by sagbee — January 26, 2006 @ 10:45 pm

  2. Hi Sagbee

    At present you can’t change the templates over at WordPress.com - one of the limitations of taking a hosted package. However many will find this a small price to pay for the convenience.

    I see they’ve dropped your blog though! Better luck next time!

    Sarah

    Comment by sarahk — January 27, 2006 @ 5:25 am

Leave a comment

RSS feed for comments on this post. TrackBack URI

Search

Pages

Categories:

Recent Comments

Other Resources

Subscribe in NewsGator Online