WordPress Link List Generation
I’m scratching my head here again. Tonight I was working on setting up the rest of my template for
When designing a
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
Now let’s move onto the problem with get_links_list(). This method ignores the
In the end, I made a modified function and placed it directly within my
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 ?>