WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Link list, wp_get_links, want to reverse order (alter sample code in WP Codex) (2 posts)

  1. AuntiAlias
    Member
    Posted 9 years ago #

    After using the default template tag [get_links_list] to create a link list, and not liking the results (I put a break tag between the link[image] and description, but it doesn't take), I've discovered on the wordpress codex that there are multiple possible tags to generate a link list.

    wp_get_links() will respect the "before" and "after" settings that you can generate in the Edit Link Category part of the control panel.

    Example 2 comes with a code sample to make it all work just like the get_links_list() tag, only better:
    <?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 } ?>

    But when I use the get_links_list() the display order is reversed (which I like) from the way the results of above code sample provide.

    How do I alter the above code to reverse the order of the link categories?

  2. AuntiAlias
    Member
    Posted 9 years ago #

    Silly silly me. I'd looked at the php documentation for rsort, and the examples made my eyes swim until 30 seconds after I posted the above.

    Here is the solution (with new line of code marked in bold):
    <?php
    $link_cats = $wpdb->get_results("SELECT cat_id, cat_name FROM $wpdb->linkcategories");

    rsort($link_cats);
    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 } ?>

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags