Home > Programming > Creating a directory system for Wordpress.

Creating a directory system for Wordpress.

December 20th, 2009 Jimmy Leave a comment Go to comments

Have you ever wanted to post some resources but didn’t necessarily want them to be on your blog’s front page? I post lots of resources but I don’t want to bog my readers down with tons of RSS feed updates and posts on my frontpage, so I created a few PHP scripts for Wordpress to help me do this. This is all done using native Wordpress API functions. In fact, you don’t even need to make or install any plugins to get this working. The following examples shows you how to make a directory for Wordpress just like I have here on my site.

Some prep-work to be done.
We’ll be working in your templates folder for the whole project. The first thing you need to do is create a new parent category for your directory root. For me I simply created a resources category. After creating a parent category you’ll need some sub (child) categories as well. I chose a few typical ones for my case such as Programming Tools, GIS Programming, and a few others. Now go add a few posts under those categories to get them rolling.


On to the directory stuff.
Lets open up your archive.php (not archives.php) file which is located in your template folder. Adding this code will display a category navigation breadcrumb at the top and also show sub categories and how many posts are in each category.

<?php
              if (is_category()) {
               echo '<div id="b-resources">';
               echo(get_category_parents($cat, TRUE, ' » '));
               echo '<br /><br />';
                 wp_list_categories( "child_of=$cat&hide_empty=1&orderby=name&show_count=1&title_li="  );
               echo '</div>';
              }

              ?>

<strong>Next</strong> we'll need to add some CSS code to your style.css file. Open up style.css and add the following code. This formats the categories to look more like a directory.

[css]

#b-resources li{
        display: block;
}
#b-resources{
 padding:6px;
 border: 1px solid #ccc;
 width:90%;
}
[/css]

Now when you goto a categtory you should see something similar to this page here.

Don’t show them on the homepage or in the RSS feed.
If you don’t want your directory posts to show up on your home page, add the following code to your index.php file in your template folder. Add the line of code above your have_posts() line. The code tells Wordpress to only get posts from categories 12, 13 and NOT 15. You can get category ID numbers in your dashboard. Highlight a category “edit” link and look at the URL in your browser’s status(normally at the bottom). Lets assume category 15 is our directory. Any posts in category 15 including child categories, won’t be displayed, same goes for the RSS part below.

<?php  query_posts($query_string . '&cat=12,13,-15');  ?>

If you don’t want the directory posts to show up in your RSS feed add the following code to your functions.php file, also located in your template folder. The following code tells Wordpress to only show posts in the RSS feed from categories 12 and 13.


add_filter('pre_get_posts', 'filterRSSQuery');
function filterRSSQuery($query) {
    if ( $query->is_feed ) {
        $query->set('cat', '12,13');
    }

    return $query;
}

Well that about sums it all up. If you need some help or have any ideas how to improve this, feel free to comment below.

Categories: Programming Tags:
Digg: DIGG ME
  1. No comments yet.
  1. No trackbacks yet.