$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>How to get permalink and title from post ID?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

How to get permalink and title from post ID?

matteradmin10PV0评论

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>";
  }
?>  

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 asked Feb 20, 2012 at 1:30 sarytash sarytash 1,7893 gold badges12 silver badges12 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 38
<?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>";
  }
?>  

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

If you dont have access you could try the wp api

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

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far