Categories
Plugins Uncategorized WordPress

Hierarchical Link Categories for WordPress

Download the plugin on WordPress.org

If you’ve ever tried to use WordPress’ built-in bookmarks / links manager, you know that its limits can be frustrating. One of the biggest issues I’ve encountered is that the link categories are not hierarchical.

Let’s say you have a tech blog and you link to a lot of pages about TV’s. You may want to categorize the links under TV > Plasma or TV > LCD. With the existing system, you cannot.

Enter the Hierarchical Link Categories plugin

Finally, with the new plugin by yours truly, you can create link categories that have parent categories. Blogroll categories can be nested, and the world breathes a sigh of relief.

What the plugin doesn’t do…yet:

  • When you’re in the “Add Link” page, you cannot add a child category
  • You tell me! Leave suggestions below.

By Zack Katz

Zack Katz is the founder of GravityKit and TrustedLogin. He lives in Leverett, Massachusetts with his wife Juniper.

21 replies on “Hierarchical Link Categories for WordPress”

I have WP 3.1 and installed this plugin this evening. I’ve gone to Link Categories and successfully created parent categories with my sub-categories, but they are not displaying as such on my website. I still have each sub-category listed in my Sidebar #2 under “Links”. I have the latest version of the Suffusion theme, and have customized it quite a bit, so I’ve checked pretty thoroughly where else I might be missing a setting to fix this problem and can’t find anything.

Now that I have my links in categories and subcategories, is there a way to display this to my users on my site.

I am confused. This helps on the back end of things, but it does nothing on the front end – is there some kind of code to list the links in a way that displays this hierarchy? Like the Links widget? I think this plugin should come with a widget that helps you display the intended results, otherwise it’s pretty silly.

I think I’ve worked out how to get the links of a children cat to display. The issue is that the parent-child relationship isn’t looked at by wp_list_bookmarks. Until the plugin has some functions of its own which you would use in the place of wp_list_bookmarks you’ll need to do something similar to:

http://pastebin.com/Z2Exng3q

I haven’t had time to turn this into a function, but will do soon. Basically you have to get all link cats, check if the link cat is related to your desired parent cat, and if it is, include it in a comma separated list which you then feed into the standard wp_list_bookmarks function.

I got round to writing a function today:

    /**
     * Prints bookmarks that are in link cat or child of link cat
     *
     * @param    string        $parent     ID of parent link cat
     * @return     echo                    Prints output via wp_list_bookmarks function
     */
    function pa_list_hierarchical_bookmarks($parent) {
        $linkCats = get_terms(‘link_category’);
        $catsToList = $parent;

        foreach ($linkCats as $linkCat) {
            if ($linkCat->parent == $parent) {
                $catsToList .= “, ” . $linkCat->term_id;
            }
        }
        $subCats = array(
            ‘category’ => $catsToList,
        );
        wp_list_bookmarks($subCats);
    }

Wow, I can’t even remember where I was using this.. Hmm.

Still, even though I can’t remember.. take the code above and insert it into your functions.php file.

Then within your template file run the function 

pa_list_hierarchical_bookmarks(5);

Assuming the parent link category id is 5 in the example above – but replace with whichever link category  id you need to display.

Thanks for the reply. 
I would like to output a complete list of links with all children 3 deep. Is that possible?

I’m sure it is possible, but I’d need some spare time to delve into the code and see what I was doing with it. Sadly I’m a little too busy at the moment. 

If I get a chance over the weekend I’ll look into it.

To get the link category id, you need to go into your link categories edit screen in the back end.  Click the Edit (not quick edit) option for the link category you want to get the id of. On the edit screen check the URL – where it says &tag_ID=x, x is the ID of your link category.

Hey, not sure if you’re still maintaining this project, but here’s a function that I cooked up that you can feel free to incorporate.  It returns the categories as a nested array (not as printable markup)

function get_link_category_tree($trunk=0) {
  $args = array(
    ‘parent’ => $trunk ,
    ‘hierarchical’ => true ,
    ‘hide_empty’ => false
  ) ;
  $categories = get_terms(‘link_category’,$args) ;
 
  $ret = array() ;
  if ($categories) : foreach ($categories as $category ) {
    $subcats = get_link_category_tree($category->term_id) ;
    $category->subcats = $subcats ;
    $ret[$category->term_id] = $category ;
  } endif ;
 
  return empty($ret) ? false : $ret ;
}
 

Comments are closed.