Using WordPress ‘get_links()’ PHP function

The get_links() WordPress PHP function retrieves the links associated with a category by its ID.

Usage

get_links($category, $before, $after, $between, $show_images, $orderby, $show_description, $show_rating, $limit, $show_updated, $display);

Example:

get_links(3, '<li>', '</li>', ' ', true, 'name', true, false, 10, 1, true);

This example will output a list of links associated with category ID 3, enclosed in <li> tags, showing images and descriptions, ordered by name, and limited to 10 links.

Parameters

  • $category (int) Optional: The category to use. If no category is supplied, it uses all. Default: 0.
  • $before (string) Optional: The HTML to output before the link. Default: ”.
  • $after (string) Optional: The HTML to output after the link. Default: <br />.
  • $between (string) Optional: The HTML to output between the link/image and its description. Not used if no image or $show_images is true. Default: ‘ ‘.
  • $show_images (bool) Optional: Whether to show images (if defined). Default: true.
  • $orderby (string) Optional: The order to output the links. E.g., ‘id’, ‘name’, ‘url’, ‘description’, ‘rating’, or ‘owner’. Default: ‘name’. If you start the name with an underscore, the order will be reversed. Specifying ‘rand’ as the order will return links in a random order. Default: ‘name’.
  • $show_description (bool) Optional: Whether to show the description if $show_images=false/not defined. Default: true.
  • $show_rating (bool) Optional: Show rating stars/chars. Default: false.
  • $limit (int) Optional: Limit to X entries. If not specified, all entries are shown. Default: -1.
  • $show_updated (int) Optional: Whether to show the last updated timestamp. Default: 1.
  • $display (bool) Optional: Whether to display the results or return them instead. Default: true.

More information

See WordPress Developer Resources: get_links()

Examples

Basic usage

Output all links in the default category with a line break after each link.

get_links();

Output all links in category ID 3 as a list.

echo '<ul>';
get_links(3, '<li>', '</li>');
echo '</ul>';

Output links with images and descriptions, ordered by URL.

get_links(0, '<p>', '</p>', ' ', true, 'url', true);

Output the top 5 highest-rated links in category ID 2.

get_links(2, '<p>', '</p>', ' ', true, '_rating', true, true, 5);

Get the links as an array instead of displaying them.

$links = get_links(0, '', '', '', false, 'name', false, false, -1, 0, false);