Using WordPress ‘get_linkobjects()’ PHP function

The get_linkobjects() WordPress PHP function retrieves an array of link objects associated with a specified category.

Usage

$links = get_linkobjects(1); if ($links) { foreach ($links as $link) { echo '<li>' . $link->link_name . '<br />' . $link->link_description . '</li>'; } }

Parameters

  • $category int (Optional): The category to use. If no category is supplied, all categories will be used. Default is 0.
  • $orderby string (Optional): The order to output the links (e.g., ‘id’, ‘name’, ‘url’, ‘description’, ‘rating’, or ‘owner’). Default is ‘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.
  • $limit int (Optional): Limit to a specific number of entries. If not specified, all entries are shown. Default is 0.

More information

See WordPress Developer Resources: get_linkobjects()

Examples

This example retrieves and displays links from category 3.

$links = get_linkobjects(3);
if ($links) {
    foreach ($links as $link) {
        echo '<li>' . $link->link_name . '<br />' . $link->link_description . '</li>';
    }
}

This example retrieves and displays links ordered by their URL.

$links = get_linkobjects(0, 'url');
if ($links) {
    foreach ($links as $link) {
        echo '<li>' . $link->link_name . '<br />' . $link->link_description . '</li>';
    }
}

This example retrieves and displays links in a random order.

$links = get_linkobjects(0, 'rand');
if ($links) {
    foreach ($links as $link) {
        echo '<li>' . $link->link_name . '<br />' . $link->link_description . '</li>';
    }
}

This example retrieves and displays the top 5 highest-rated links.

$links = get_linkobjects(0, '_rating', 5);
if ($links) {
    foreach ($links as $link) {
        echo '<li>' . $link->link_name . '<br />' . $link->link_description . '</li>';
    }
}

This example retrieves and displays all links with a target of _blank.

$links = get_linkobjects();
if ($links) {
    foreach ($links as $link) {
        if ($link->link_target == '_blank') {
            echo '<li>' . $link->link_name . '<br />' . $link->link_description . '</li>';
        }
    }
}