Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have stored an array of post IDs and I would like to list the posts as links, meaning I need to get the title and permalink for the post ID - $id. The list should be echoed out by the following if condition, which means I somehow have to replace $id with the permalink and title. At the moment the code merely lists the post ID numbers.

<?php
  if(count($related)){
    echo "<div>Read More<ul>";
    foreach($related as $id){
       echo "<li>$id</li>";
    }
    echo "</ul></div>";
  }
?>  
share|improve this question

2 Answers 2

up vote 7 down vote accepted
<?php
if(count($related)) {
    echo "<div>Read More<ul>";
    foreach($related as $id) {
        echo '<li><a href="'.get_permalink( $id ).'">'.get_the_title( $id ).'</a></li>';
    }
    echo "</ul></div>";
  }
?>  
share|improve this answer
    
Your answer...it's genius! :D –  m0r7if3r Feb 20 '12 at 1:39
    
That works brilliantly! Thank you very much! –  sarytash Feb 20 '12 at 1:44

You just need to use get_permalink( $id ) and get_the_title( $id ).

share|improve this answer
    
Thank you! This works! –  sarytash Feb 20 '12 at 1:47

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.