admin管理员组文章数量:1130349
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
3 Answers
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
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
3 Answers
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
本文标签: How to get permalink and title from post ID
版权声明:本文标题:How to get permalink and title from post ID? 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749222363a2335144.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论