11

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>";
  }
?>  
24
<?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>";
  }
?>  
  • Your answer...it's genius! :D – mor7ifer Feb 20 '12 at 1:39
7

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

0

If you dont have access you could try the wp api

https://content.wordpress.com.au/wp-json/wp/v2/posts/POST_ID

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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