www.muttznutz.net

Underwater photography by Andy Kirkland

Link organisation with the Category Overload plugin

I’m also looking after my Club’s site, and have loaded Alex King’s CategoryOverload plugin.
This is cool, as it lets you build a hierarchy of link categories.

I’ve written a custom SQL routine - which I’ve included in one of our pages - to use this for output, by cycling through the different sub-categories of a parent (other links aren’t output).
This works with Wordpress 2.1.3.
Feel free to adapt it.

Here’s the skeleton code, for a “parent” category with an ID of 12.
Note that Php commands have an extra space inserted after the “< " to prevent accidental execution.

This first bit builds a array of sub-categories :-

<?php
$querystr =
SELECT cat_id, cat_name
FROM `wp_categories`
WHERE category_parent =12
order by cat_id
;
$linkcats = $wpdb->get_results($querystr, OBJECT); ?>

… Then this part loops through each subcategory and outputs it.
(you may want to put some formatting in …)

<?php if ($linkcats): ?>
<?php foreach ($linkcats as $linkcat): ?>
<?php echo $linkcat->cat_name ; ?>
<?php get_links( $linkcat->cat_id, , , , TRUE, ‘category’, TRUE, FALSE, -1, FALSE, TRUE); ?>
<?php endforeach; ?>
<?php endif; ?>

6 Comments ....

  1. I had a few problems getting the code to appear properly. My apologies to anyone affected.
    So I’ve installed the codesnippet plugin - which “colourises” the code as well. Thanks to those involved.
    I’ve also - hopefully - got the Comments function working as I’d like (if you can see this, then I have).
    This is mostly so I can give updates on topics.
    I’m afraid I’m not still opening them up in general, however, as I don’t have time to moderate all the spam entries.

    Posted by muttznutz on 20 May, 2007

  2. I’ve also used the Get Comments Count plugin from Mark Jaquith’s Tempus Fugit site.
    It should make posts summaries cleaner, by only highlighting posts where there is a comment.
    Here’s how I use it in my Category pages :

    <?php if (get_comments_count() > 0) : ?>
        This post has <?php comments_number(); ?>
    <?php endif; ?>

    Posted by muttznutz on 21 May, 2007

  3. The above code segments still seem to work with WP 2.2

    Posted by muttznutz on 25 May, 2007

  4. I’m just trying to upgrade to Wordpress 2.3.1
    Not everything’s working too well, but I’m trying to get through the new database structure.

    Key point - I’ve disabled CategoryOverload.
    I’ve hopefully made the code above a bit more compliant (using the $wpdb-> prefix).
    Some of the underlying tables have changed, so here’s a replacement for the SQL in the 13th May post (which seems to work for me).

    SELECT term_id, name
    FROM $wpdb->terms
    WHERE term_id IN
    (
       SELECT term_id
       FROM $wpdb->term_taxonomy
       WHERE parent=6
    )
    ORDER BY term_id

    As the ‘get_links’ tag - used above - is deprecated in WordPress 2.3, I suppose it’s probably best if you switch to the wp_list_bookmarks tag instead.

    Posted by muttznutz on 12 November, 2007

  5. I’m getting a bit better with this php stuff (as you may see elsewhere) and I’ve just got around to reworking this code.
    I’m up to v2.6.2. of WP now.
    This looks up the category based on the “slug”, soit’s a bit more portable.

    $querystr =
            SELECT term_id, name
            FROM $wpdb->terms
            WHERE term_id IN
            (
            SELECT tt.term_id
            FROM $wpdb->term_taxonomy tt
           left join $wpdb->terms t
           on tt.parent = t.term_id
            WHERE t.slug = ‘links-thanks-etc-etc’
            )
            ORDER BY term_id
      “
    ;
     $linkcats = $wpdb->get_results($querystr, OBJECT);

    As an alternative, there’s a WordPress function (since 2.3.0) called “get_category_by_slug()”. It’s not part of the canonical API - so there’s no guarantee it’ll be there in future releases - but this means you achieve the same results with something like this :

    $parentCat = get_category_by_slug( ‘links-thanks-etc-etc’ ) ;
     $querystr =
            SELECT term_id, name
            FROM $wpdb->terms
            WHERE term_id IN
            (
            SELECT term_id
            FROM $wpdb->term_taxonomy
           where parent = ‘{$parentCat->term_id}’ and taxonomy = ‘link_category’
            )
            ORDER BY term_id
      “
    ;
     $linkcats = $wpdb->get_results($querystr, OBJECT);

    You can then do something on the lines of this next snippet to process each link category selected

    if ($linkcats) {
      foreach ($linkcats as $linkcat) {
        wp_list_bookmarks(“category={$linkcat->term_id}”);
        };       
      };

    While this works fine, the main problem is still that - since the taxonomy change in 2.3 - there’s no way of updating the parent category for link categories without delving in at database level.

    Posted by muttznutz on 4 October, 2008

  6. Back to basics

    Rather worryingly, this is getting quite high on the Google list for “Category Overload”. That’s very flattering, but I wasn’t expecting so much interest …
    What I was trying to do was to treat different categories of bookmarks / links / blogroll entries in different ways.
    Certain categories (Photo Albums, Camera buddies etc.) are shown in the sidebar, while others would appear on the “Thanks” page.
    I wanted these to be driven by data so far as possible, rather than being hard-coded into the page template. So - if I add a new category into links I want to “thank”, I’d rather just set a flag.
    The “CategoryOverload” plugin sorted this up to WP 2.3, when the new database structure was introduced.
    At this point, link categories were given there own slot in the taxonomy (see the 12th Nov 2007 comment).
    Many of the functions which apply to post / page categories were no longer available for link categories - previously, this seems to have been constrained by screens, but now many functions “cut out” any entry which isn’t a “category”.

    So how do you use this code, then eh ?

    One of the comments said the documentation was “a bit vague” (sorry about that).
    Well, this does involve messing around a bit with your theme’s template pages, and it does involve php and SQL code, so you should understand what you’re doing.
    I’ve not got into the WordPress development process enough yet to wrap this up as a Plugin, and all of the above isn’t anything like “official” WP code.
    But what I’ve done is to create a template called “thankslinks.php”, and plumbed in the above code.
    That code selects a set of data which can be iterated and used for standard WP template tags such as wp_list_bookmarks().
    My “Thanks” page has that template assigned (in the “Page Template” tab when you edit the page) and then the code just kinda drops things out …

    Posted by muttznutz on 4 October, 2008

The URI to TrackBack this entry is: http://muttznutz.net/muttzblog/site-news/link-organisation-with-the-categoryoverload-plugin/2007/05/13/trackback

RSS feed for comments on this post.


Valid XHTML 1.0 Strict