WordPress Link List Generation

I’m scratching my head here again. Tonight I was working on setting up the rest of my template for WordPress. I ran into a problem when I tried to check to see if it was XHTML compliant. It turns a method called get_links_list() was the culprit. This method is used to create my blogroll and other lists of links.

When designing a template, you want to make sure that you have total control over the layout and design of the page. The list of links are a little buggy in this area. First, the back-end interface allows the user to customize the text that appears before and after the link, as well as any text that appears between the link and the description. There are some list item tags suggested for the user by default.

Why is this a problem? The reason is that the template files that I am designing may one day appear on someone elses site. They may not be using list item tags. List item tags are required to be enclosed within list tags in order to be xhtml compliant. An additional problem is that only list item tags may be a direct child item of list elements.

Now let’s move onto the problem with get_links_list(). This method ignores the end-users preferences completely. That’s a good thing since I can now write a template knowing how the results will come back. The bad news is that I don’t have control over this in future upgrades, and I personally don’t want the list to be formatted the way that it comes back.

In the end, I made a modified function and placed it directly within my sidebar template. This is going a little overboard, but it does get the job done. I fixed it so that it does get the end-users information from the database about how individual links are formatted. I believe that template designers should have more control over this.

1 <?php
2 function get_links_list2($order = ‘name’, $hide_if_empty = ‘obsolete’, $cat_prefix = ‘<strong>’, $list_prefix = ‘</strong><ul>’, $list_suffix = ‘</ul>’) {
3 global $wpdb;
4
5 $order = strtolower($order);
6
7 // Handle link category sorting
8 if (substr($order,0,1) == ‘_’) {
9 $direction = ‘ DESC’;
10 $order = substr($order,1);
11 }
12
13 // if ‘name’ wasn’t specified, assume ‘id’:
14 $cat_order = (‘name’ == $order) ? ‘cat_name’ : ‘cat_id’;
15
16 if (!isset($direction)) $direction = ;
17 // Fetch the link category data as an array of hashesa
18 $cats = $wpdb->get_results(
19 SELECT DISTINCT link_category, cat_name, show_images,
20 show_description, show_rating, show_updated, sort_order,
21 sort_desc, list_limit, text_before_link, text_after_link, text_after_all
22 FROM `$wpdb->links`
23 LEFT JOIN `$wpdb->linkcategories` ON (link_category = cat_id)
24 WHERE link_visible = ‘Y’
25 AND list_limit <> 0
26 ORDER BY $cat_order $direction “, ARRAY_A);
27
28 // Display each category
29 if ($cats) {
30 foreach ($cats as $cat) {
31 // Handle each category.
32 // First, fix the sort_order info
33 $orderby = $cat[’sort_order’];
34 $orderby = (bool_from_yn($cat[’sort_desc’])?‘_’:) . $orderby;
35
36 // Display the category name
37 echo $cat_prefix. $cat[‘cat_name’] .$list_prefix;
38 // Call get_links() with all the appropriate params
39 get_links(
40 $cat[‘link_category’],
41 $cat[‘text_before_link’],
42 $cat[‘text_after_all’],
43 $cat[‘text_after_link’],
44 bool_from_yn($cat[’show_images’]),
45 $orderby,
46 bool_from_yn($cat[’show_description’]),
47 bool_from_yn($cat[’show_rating’]),
48 $cat[‘list_limit’],
49 bool_from_yn($cat[’show_updated’]));
50
51 // Close the last category
52 echo $list_suffix;
53 }
54 }
55 }
56 ?>

Leave a Reply