Manually link to existing content or a custom url through a meta box on the writing page.
Related Links gives you the possibility to manually link other posts to your current post. But you can also link pages, media or any custom post-type. And in addition you can use custom urls to link to external files. The plugin adds a Metabox to the writing page with a list of all available content.
Features:
related_links()
related-links
folder to the /wp-content/plugins/
directory.Plugins
menu in WordPress.Related Links
under the Settings
menu in WordPress.<?php related_links(); ?>
in your templates.Use the Related Links
widget to show a list of the related links.
Use the related_links()
function directly in your template files. This will return an unordered list with an <ul>
wrapper. Use this code for example in your `content.php’ template:
<?php related_links(); ?>
You need to use the get_related_links()
function. A simple example that shows a list with all link names and the type of link:
<?php $related_links = get_related_links(); ?> <ul> <?php foreach ($related_links as $link): ?> <li><a href="<?php echo $link['url']; ?>"><?php echo $link['type']; ?>: <?php echo $link['title']; ?></a></li> <?php endforeach; ?> </ul>
The get_related_links()
returns an array containing every related link. when you loop through this array every link consists of another array with the following keys:
id
: equals to $post->ID
or null
for custom linksurl
: equals to get_permalink()
or the manually entered url of a custom linktitle
: equals to $post->post_title
or the manually entered title of a custom linktype
: the $post->post_type
or null
for custom linksSet the $post_type
in get_related_links($post_type)
to 'post'
, 'page'
or any custom post-type. A simple example that show a list of links:
<?php $related_links = get_related_links('page'); ?> <ul> <?php foreach ($related_links as $link): ?> <li><a href="<?php echo $link['url']; ?>"><?php echo $link['type']; ?>: <?php echo $link['title']; ?></a></li> <?php endforeach; ?> </ul>
Set the $post_id
in get_related_links(null, $post_id)
to the id of the post. A simple example that show a list of links:
<?php $related_links = get_related_links(null, 1); ?> <ul> <?php foreach ($related_links as $link): ?> <li><a href="<?php echo $link['url']; ?>"><?php echo $link['type']; ?>: <?php echo $link['title']; ?></a></li> <?php endforeach; ?> </ul>
You need to check the 'type'
and then get with wp_get_attachment_url()
the attachment url from the 'id'
.
<?php $related_links = get_related_links(null, 1); ?> <ul> <?php foreach ($related_links as $link): ?> <?php if ($link['type'] == 'attachment') : $url = wp_get_attachment_url($link['id']); else : $url = $link['url']; endif; ?> <li><a href="<?php echo $url; ?>"><?php echo $link['title']; ?></a></li> <?php endforeach; ?> </ul>
You need to check the 'type'
and then get with wp_get_attachment_url()
the attachment url from the 'id'
.
<?php $related_links = get_related_links(null, 1); ?> <ul> <?php foreach ($related_links as $link): ?> <?php if ($link['type'] == 'attachment') : $url = wp_get_attachment_url($link['id']); $mime = explode('/', get_post_mime_type($link['id'])); $mime = $mime[sizeof($mime) - 1]; else : $url = $link['url']; $mime = null; endif; ?> <li><a href="<?php echo $url; ?>"><?php echo $link['title']; ?><?php echo isset($mime) ? ' (' . $mime . ')' : ''; ?></a></li> <?php endforeach; ?> </ul>
Put the get_related_links()
function or your whole code into an if
clause. Like this your theme will still work even if the plugin is deactivated.
<?php if(function_exists('get_related_links')) : ?> <?php $related_links = get_related_links(); ?> <ul> <?php foreach ($related_links as $link): ?> <li><a href="<?php echo $link['url']; ?>"><?php echo $link['type']; ?>: <?php echo $link['title']; ?></a></li> <?php endforeach; ?> </ul> <?php endif; ?>
id
property to the get_related_links() functionInitial release