Support » Fixing WordPress » Hacking Category Permalinks…

  • Alright, I set up my Categories to be in different directories, with different indexes.
    What I needed was, for every link generated by the_category to link to the category directory I created. To make things simple, I named each category directory as WordPress would generate it (ex: “My Awesome Category” = “my-awesome-category”).
    (Keep reading to the bottom, I have a question at the end of this) 🙂
    So about a month ago I can up with a solution for this…
    Instead of a mod-rewrite, I dived into the code.
    It’s rather simple actually:
    Open up: templates-functions-category.php
    Around line 30, change:
    $link = $file.$querystring_start.'cat'.$querystring_equal.$cat_ID;
    to:
    $link = ‘http://www.site.com/wpblog/’ . $category_nicename . ‘/’;
    It will output all of your Category permalinks from:
    http://www.site.com/wpblog/index.php?cat=5
    to:
    http://www.site.com/wpblog/whatever-the-category-name-is/
    Now here’s my problem… that works fine and dandy for categories, but it doesn’t work for subcategories. Well, it works, but it doesn’t include the parent category directory infront of the subcategory directory name 🙁
    The above code needs to somehow work where it creates links as:
    http://www.site.com/wpblog/the-category-name/
    and with subcategories:
    http://www.site.com/wpblog/parent-category/subcategory/
    Any help appreciated 🙂

Viewing 5 replies - 1 through 5 (of 5 total)
  • “Alright, I set up my Categories to be in different directories, with different indexes.”
    How did you do this? I’ve been trying to find a tutorial or instrructions that explain how to pull this off and have had no luck.
    Does the code distinguish between categories and subcategories? You could change your hack to a conditional statment – i.e. if this is a subcategory, set $categoryparent to the nincename of the category’s parent, and then do this: $link = ‘http://www.site.com/wpblog/’ . $categoryparent . $category_nicename . ‘/’;, otherwise have it do your original hack.
    If you can’t dymanically figure out whether or not a given caetegory is a subcategory, just hard-code categories who are subcategories into an array:
    $subcats = array(0 => ‘category-x’, 1 => ‘category-x’, 2 => ‘category-x’, 3 => ‘category-x’);
    where ‘category-x’ is whatever-the-category-name-is
    then do a conditional to test and see if the category is one of those:
    if (in_array(category_nicename, $subcats)){
    $link = ‘http://www.site.com/wpblog/’ . $categoryparent . $category_nicename . ‘/’;
    else:
    $link = ‘http://www.site.com/wpblog/’ . $category_nicename . ‘/’;
    endif;
    }
    of course, you might do best to have multiple arrays, one for each parent category, continaing its subcategories, but then you’d have to test each array seperately. But this would give you the parent category name to drop into $categoryparent.
    HTH.

    There is a category_parent object and a get_category_parents method, but I’m not sure how they’re used. But you should be able to test to see if a given category has a parent using one or both.

    Hi, thanks for the reply! 🙂
    I’ll check up on your code later tonight and get back to you…
    To set up my categories to be on seperate indexes in seperate directories, just duplicate the index.php, put it in a different directory (and name it to your category name – spaces are “-“), and make sure the header tags it calls for are pointing to the right path. Then you have to add:
    `$cat = 1;
    to the top of your page after the opening “<?php” tag. The number in the number of the category you want showing on that page.
    Let me know it’s still unclear… I’ll get back to you in the morning… off to work 😉

    Hmmm, it doesn’t seem to work :/

    bump 🙂

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Hacking Category Permalinks…’ is closed to new replies.