Wordpress: Sorting and Displaying Links
I wanted to have a certain number of links pulled out from the Wordpress Link Manager at random, and I didn't want to install any plugins to do it. The Link Manager is accessible from your WP dashboard under the name "Blogroll". Here you can add, remove and edit links and create categories and such for them. We can get these random links by using a WP template tag. I had to do some digging to find it and it's parameters, so I figured I'd post it here incase anyone else finds it useful.The Parameters
<?php get_links(category, 'before', 'after', 'between', show_images, 'order', show_description, show_rating, limit, show_updated, echo); ?>Here is an explanation of each parameter and how to use it.
- Category - integer - the numeric ID of the category of links you want to display. If you want to display more than one category, simply separate the IDs with commas (ie. 1,4,7). To display all categories, set to -1. This parameter defaults to -1.
- Before - string - what you want displayed before each link. Doesn't default to anything.
- After - string - what you want displayed after each link. Defaults to <br />
- Between - string - what to display between each link. Defaults to a space.
- Show Images - boolean - whether images for links are displayed or not: TRUE or FALSE. Defaults to TRUE.
- Order - string - how the links are sorted. See below for usage.
- Show Description - boolean - should the description for the link be displayed: TRUE or FALSE. Defaults to TRUE.
- Show Rating - boolean - should the rating of the link be displayed: TRUE or FALSE. Defaults to FALSE.
- Limit - integer - maximum number of links to display. Defaults to -1 (no limit).
- Show Updated - boolean - should the last updated timestamp be displayed with the link: TRUE or FALSE. Defaults to FALSE.
- Echo - boolean - whether to display the links: TRUE or FALSE. Defaults to TRUE.
Order: Sorting your links
There are a lot of variables for the "order" parameter, so I will list them here.- id
- url
- name
- target
- category
- description
- user - the person who added the link in the WP admin panel
- rating
- updated
- rel - link relationship (XFN)
- notes
- rss
- length - sorts by the link's name's length, shortest to longest.
- rand - displays links in random order.
The end result
So, we have our parameters and our variables. How can we use them?
<?php get_links('-1', '<li>', '</li>', '', FALSE, 'rand', FALSE, FALSE, 5, FALSE, TRUE); ?>
This is how I've set my links up. It displays all categories, with a maximum of five links displaying at any one time. It sorts the links randomly, and doesn't display any images, descriptions, timestamps or ratings. It displays each link as a list item.
<?php get_links('-1', '<li>', '</li>', '', FALSE, 'name', TRUE, FALSE, -1, FALSE, TRUE); ?>
This displays all links as list items, in name order, and with descriptions
<?php get_links('3,5,8', '', '<br />', '', FALSE, '_rating', TRUE, FALSE, 10, FALSE, TRUE); ?>
And finally, this displays links from categories with the IDs 3, 5 and 8, puts a line break after each link and displays a maximum of ten links in reverse rating order, with descriptions.
So there we have it. Grabbing links from the link manager. (: A much easier way to manage and display links on your site.
Comments
-
Rick
On September 14, 2007 at 7:56 am.Thanks! This was very helpful. I used it on a couple of my own blogs. And youre right, it’s better than using any plugin.
RSS feed for comments on this post. TrackBack URI