• Resolved syllani

    (@syllani)


    Hi. I tried searching for this answer but I didn’t really find anything.

    I’m using the following code from the wiki to display links grouped by category in the sidebar:

    <ul>
    <?php
    $link_cats = $wpdb->get_results("SELECT cat_id, cat_name FROM $wpdb->linkcategories");
    foreach ($link_cats as $link_cat) {
    ?>
    <li id="linkcat-<?php echo $link_cat->cat_id; ?>"><h2><?php echo $link_cat->cat_name; ?></h2>
    <ul>
    <?php wp_get_links($link_cat->cat_id); ?>
    </ul>
    </li>
    <?php } ?>
    </ul>

    It works well, but I’d like to modify it so that any empty categories (those without any links) are not displayed. I’ve no idea how to do it myself; I’d greatly appreciate any assistance.

    Thanks!

Viewing 4 replies - 1 through 4 (of 4 total)
  • How does this differ from get_links_list()? That will list links by category, and does not display empty link categories.

    Thread Starter syllani

    (@syllani)

    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>

    Thread Starter syllani

    (@syllani)

    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. ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Hiding Empty Link Categories’ is closed to new replies.