Ask your WordPress questions! Pay money and get answers fast! (more info)

Related Links plugin WordPress

  • SOLVED

I'm currently using the "Related Links" plugin on a site I'm working on:
[[LINK href="http://wordpress.org/extend/plugins/related-links/faq/"]]http://wordpress.org/extend/plugins/related-links/faq/[[/LINK]]

The plugin allows you to show links from a certain "post_type" in your theme by using the following code:
<?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>


In this instance, it's only showing "Pages".

The plugin allows you to add "Custom" links via the UI - these are often external URLs... I'd like to tweak the above code to include "Pages" and "Custom" links... Not all instances will have "Custom" links.

Thanks.

Answers (2)

2013-02-19

Francisco Javier Carazo Gil answers:

Hi Michael,

You can do two petitions:
$related_links_page = get_related_links('page');
$related_links_custom = get_related_links('custom');

And then merge it into one array:
$related_links = array_merge($related_links_page, $related_links_custom);


Michael Brumm comments:

Thanks for the quick replay Francisco... That worked... However, how would I go about displaying the outputted list in alphabetical order by title?

I'm playing around with sort() right now and can't seem to get it to sort by title.


Michael Brumm comments:

Scratch the alphabetical request

The client would like to retain the order defined with the UI of the plugin... Hmmm...

Is the best approach to use the general <strong>get_related_links()</strong> and only display options if the type equals pages or custom?


Francisco Javier Carazo Gil comments:

Sorry i am out and from mobile I cannot see. Doc. Try a var_dump with resulting array and use sort or ksort to sort it.

2013-02-19

Arnav Joy answers:

try this

<?php

$related_links = get_related_links('page');

$related_links_custom = get_related_links('custom');

if( !empty($related_links_custom))
$related_links = array_merge( $related_links , $related_links_custom );

?>

<ul>

<?php foreach ($related_links as $link): ?>

<li><a href="<?php echo $link['url']; ?>">
<?php if( $link['type'] != '' ) echo $link['type'] .':'; ?>
<?php echo $link['title']; ?>
</a></li>

<?php endforeach; ?>


Michael Brumm comments:

I seem to have accomoplished what I was trying to achieve by the following:

<?php if(function_exists('get_related_links')) : ?>
<?php $related_pages = get_related_links(); ?>
<ul class="related-links-list">
<?php foreach ($related_pages as $rpage): ?>
<?php if( $rpage['type'] == 'page' || $rpage['type'] == '') { ?>
<li><a href="<?php echo $rpage['url']; ?>"><?php echo $rpage['title']; ?></a></li>
<?php } ?>
<?php endforeach; ?>
</ul>
<?php endif; ?>


Is there a downside to doing this compared to the other merged approach?


Arnav Joy comments:

no , it is also ok.


Michael Brumm comments:

One more question regarding implementing... I have the following code in place:


<?php if(function_exists('get_related_links')) : ?>
<?php
$related_pages = get_related_links();
if(count($related_pages) != 0){
?>
<h3 style="font-size: 18px; border-top: 1px solid #e5e5e5; padding-top: 30px; margin-top: 10px;">Related Information</h3>
<ul class="related-links-list">
<?php foreach ($related_pages as $rpage): ?>
<?php if( $rpage['type'] == 'page' || $rpage['type'] == '') { ?>
<li><a href="<?php echo $rpage['url']; ?>"><?php echo $rpage['title']; ?></a></li>
<?php } ?>
<?php endforeach; ?>
</ul>
<?php } ?>
<?php endif; ?>


How do I modify this code to see if the count for the "page" type and "custom" type equals zero? If these two are empty, do not show the "Related Information" section?

The code above breaks when the user selects other types of content from the related links UI (such as "Whitepapers")...


Arnav Joy comments:

try this


<?php if(function_exists('get_related_links')) : ?>

<?php
$count = 0;
$related_pages = get_related_links();

foreach ($related_pages as $rpage){
if( $rpage['type'] == 'page' || $rpage['type'] == '') {
$count = 1;break;
}
}


if($count == 1){

?>

<h3 style="font-size: 18px; border-top: 1px solid #e5e5e5; padding-top: 30px; margin-top: 10px;">Related Information</h3>

<ul class="related-links-list">

<?php foreach ($related_pages as $rpage): ?>

<?php if( $rpage['type'] == 'page' || $rpage['type'] == '') { ?>

<li><a href="<?php echo $rpage['url']; ?>"><?php echo $rpage['title']; ?></a></li>

<?php } ?>

<?php endforeach; ?>

</ul>

<?php } ?>

<?php endif; ?>


Michael Brumm comments:

Works perfectly... Thank you.