How does this differ from get_links_list()? That will list links by category, and does not display empty link categories.
Here’s the original source of the code I posted. Now that I look at it, I guess they aren’t much different. *blush*
Sorry for the dumb question.
Edit: Actually, I just tried it… get_links_list() automatically puts <h2> tags around the category titles. I’d much prefer a different markup for them.
Wasn’t a dumb question. I just want to make sure you weren’t doing things in ways harder than they need to be…
Note that you have category titles in <h2> elements in the code above. But if you really need to avoid the markup from get_links_list(), this mod to the wiki code should do what you want:
<ul>
<?php
$link_cats = $wpdb->get_results("SELECT cat_id, cat_name FROM $wpdb->linkcategories");
foreach ($link_cats as $link_cat) {
$linklist = get_links($link_cat->cat_id, '<li>', '</li>', '', false, '', false, false, -1, false, false);
if($linklist) {
?>
<li id="linkcat-<?php echo $link_cat->cat_id; ?>"><?php echo $link_cat->cat_name; ?>
<ul>
<?php echo $linklist; ?>
</ul>
</li>
<?php } } ?>
</ul>
Hey, thanks! 🙂 I’d been researching in the meantime (doing a little reading up on the table structure and sql and stuff) and came up with the following:
<ul>
<?php
$link_cats = $wpdb->get_results("SELECT cat_id, cat_name FROM $wpdb->linkcategories");
foreach ($link_cats as $link_cat) {
$num_links = $wpdb->get_var("SELECT count(*) FROM $wpdb->links WHERE link_category = $link_cat->cat_id");
if ($num_links > 0):
?>
<li id="linkcat-<?php echo $link_cat->cat_id; ?>"><h3><?php echo $link_cat->cat_name; ?></h3>
<ul>
<?php wp_get_links($link_cat->cat_id); ?>
</ul>
</li>
<?php endif; } ?>
</ul>
Your solution is better though, since it doesn’t require the extra db hit. Thanks! 🙂
Oh, regarding the <h2> in the original, I had them changed to <h3> in my own version; what I posted was a direct copy-paste from the wiki. Just in case you were wondering. 😉