WordPress.org

Ready to get started?Download WordPress

Forums

Top-level category page to show all categories (4 posts)

  1. BillyRayPreachersSon
    Member
    Posted 6 years ago #

    I'm developing a theme, which I'm pretty happy with so far. I've got all my categories displaying nicely on category.php when you drill down into them (e.g. "/category/cat1/", "/category/cat1/subcat3/", etc) using category.php.

    What I want is to be able to have a top-level "category overview" page at "/category/" which shows me all top-level categories.

    Coding the category list on this page is no problem - my problem is that if I visit "/category/", I get taken to the 404 page rather than to category.php.

    I can work around this by creating a page whose permalink I set to "/category/", but this smells to me.

    Is there a cleaner way of getting to a defined PHP file (whether category.php or not) when I visit "/category/", but without losing the existing functionality of using category.php when I visit a normal category URL ("/category/something")?

    Thanks,

    Dan

  2. jpadie
    Member
    Posted 6 years ago #

    i would plug the category rewrite rules something like this

    <?php
    /*
    Plugin Name: CategoryOverview
    Plugin URI: http://rathercurious.net
    Description: redirects a blank category name to an overview page
    Author: Justin Adie
    Version: 0.1
    Author URI: http://rathercurious.net
    */
    
    add_filter('category_rewrite_rules', array('categoryOverview', 'rewrite'));
    
    class categoryOverview{
    	public function rewrite($data){
    		$catBase = get_option('category_base');
    		$array[$catBase.'$'] = 'index.php?category_name=overview';
    		return array_merge($data, $array); //use array_merge to force this one on to the end so it gets matched last
    	}
    }
    ?>

    then you would create a category called overview, grab the id associated with it and create a template file called category_[id] and this would be used to display your overview.
    if you want, you could suppress the overview category from the various places that categories are displayed too, although in reality if you're the only person posting this isn't a real issue.

    of course, you can also redirect to any other page that you want too: just change the array element value. or use the template_redirect hook.

  3. BillyRayPreachersSon
    Member
    Posted 6 years ago #

    After trying a lot of things out, I've gone back to Plan A (having a page with permalink of "/category/").

    The various things tried can be found in this thread at the Tek-Tips forums:

    http://www.tek-tips.com/viewthread.cfm?qid=1497505

    Dan

  4. jpadie
    Member
    Posted 5 years ago #

    actually we ended up with a neater solution than a permalink so do check out BRPS' link above for the full discussion and solution.

Topic Closed

This topic has been closed to new replies.

About this Topic