views:

212

answers:

2

I'm building a small list of recent comments, and would like to make links to the actual posts that the comments were placed on. Unfortunately, there is no comment_permalink or post_permalink that I can find, so I thought maybe there would be a get_permalink() function, but again, none that I could find on http://codex.wordpress.org/Function_Reference/.

From the $post->ID alone, how can I find the permalink for that particular post? Not that it's completely necessary, but here is what I have so far:

<?php $comments = get_comments( array( 'status'=>'approve', 'number'=>5 ) ); ?>
<p class="recently-posted-comments">Recent Comments</p>
<ul>
<?php foreach ($comments as $comment): $parent = get_post($comment->comment_post_ID); ?>
  <li><?php print $comment->comment_author; ?> 
      on <?php print $parent->post_title; ?></li>
<?php endforeach; ?>
</ul>

My intent is to convert the $parent->post_title into a permalink.

+1  A: 

The confusion comes as a result of ambiguous function names. I was looking for something that suggested a link for a "post," yet found nothing. Out of curiousity, I came across and tested get_page_link(), only to find that it does exactly what I was looking for.

Unfortunately I assumed that "page" was an exclusive term reserved for pages in wordpress, rather than posts. It appears in this context it is representative of both.

Aristotle
+2  A: 

I thought maybe there would be a get_permalink() function, but again, none that I could find.

http://codex.wordpress.org/Function_Reference/get_permalink

I'd also recommend using that over get_page_link()

get_permalink() checks the post type and returns the result of the appropriate function;

  • Pages use get_page_link()
  • Attachments use get_attachment_link()
  • Custom post types use get_post_permalink()
TheDeadMedic
A bit frustrating that this function wasn't listed on the [Function Reference](http://codex.wordpress.org/Function_Reference/), but thank you for pointing out that it does indeed exist! Makes me wonder what else I may be missing.
Aristotle