如何从帖子ID获取永久链接和标题?


11

我存储了一个帖子ID数组,我想将这些帖子列为链接,这意味着我需要获取帖子ID-$ id的标题和永久链接。以下if条件将回显该列表,这意味着我必须以某种方式将$ id替换为永久链接和标题。目前,该代码仅列出了职位ID号。

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

Answers:


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

您的回答……真是天才!:D
mor7ifer 2012年

7

您只需要使用get_permalink( $id )和即可get_the_title( $id )


0

如果您没有访问权限,则可以尝试wp api

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

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.