WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] wp_list_bookmarks() - alphabetical random? (5 posts)

  1. aslee
    Member
    Posted 6 years ago #

    I have a fairly large list of links, and I'd like to call up (for example) five of them at random, then have them displayed alphabetically. Just for example we'll use some webcomics:

    Dork Tower, Goblins!, Penny and Aggie, Penny Arcade, PVP, Something Positive, xkcd

    Now if I use limit=5&orderby=rand, I get something like this:

    xkcd, Goblins!, PVP , Penny and Aggie, Something Positive

    which is fine for not having too many silly links, but rather chaotic-looking. With that selection I'd like it to display

    Goblins!, Penny and Aggie, PVP, Something Positive, xkcd

    rand,name; errors out, and using two orderby parameters just leaves me with the first five alphabetically. I don't see any suggestions in the Codex page.

    Does anyone out there have any ideas?

  2. MichaelH
    Member
    Posted 6 years ago #

    Try this:
    <?php
    $random_links = $wpdb->get_col("SELECT link_id FROM $wpdb->links ORDER BY RAND() LIMIT 5");
    $csv_links = implode(",", $random_links);
    $wlb_param_list='orderby=name&categorize=0&title_li=&include=' . $csv_links;
    wp_list_bookmarks($wlb_param_list);
    ?>

    Note: of course change the LIMIT to however many links you want.

  3. aslee
    Member
    Posted 6 years ago #

    That's very helpful, thank you!

    However, I can't figure out how to pull from one specific link category in that case. I tried changing the SQL query to "SELECT link_id FROM $wpdb->links where link_category=something ORDER BY RAND() LIMIT 5", which seems intuitive (looking at the SQL structure, but on further inspection all of my links have the link_category=0, even though they belong to separate categories in WordPress.

    Going into mySQL and doing "SELECT * FROM wp_links" I'm afraid I don't see any other easy way to distinguish them.

    Am I missing something obvious?
    Thanks!

  4. MichaelH
    Member
    Posted 6 years ago #

    Well your original 'spec' didn't say anything about specific categories ;)

    Using Otto42's magic try this (I didn't do much testing):

    <?php
    $random_links = $wpdb->get_col("SELECT link_id FROM $wpdb->links
    LEFT JOIN $wpdb->term_relationships ON
    ($wpdb->links.link_id = $wpdb->term_relationships.object_id)
    LEFT JOIN $wpdb->term_taxonomy ON
    ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
    WHERE $wpdb->term_taxonomy.taxonomy = 'link_category'
    AND $wpdb->term_taxonomy.term_id = 13
    ORDER BY RAND()
    LIMIT 5");
    $csv_links = implode(",", $random_links);
    $wlb_param_list='orderby=name&categorize=0&title_li=&include=' . $csv_links;
    wp_list_bookmarks($wlb_param_list);
    ?>

    That's pulling links in Category = 13.

  5. aslee
    Member
    Posted 6 years ago #

    This is very true. =D I thought I could work from a general solution to the specific one, but my SQL-fu is weak. I think having both in one place will be good for future reference, though.

    That works beautifully!
    Thanks again!

Topic Closed

This topic has been closed to new replies.

About this Topic