# Function to link to the Discourse topic from a WordPress template?

**URL:** https://meta.discourse.org/t/function-to-link-to-the-discourse-topic-from-a-wordpress-template/84790
**Category:** WordPress
**Created:** [4월 7, 2018, 9:40오후 UTC](https://meta.discourse.org/t/function-to-link-to-the-discourse-topic-from-a-wordpress-template/84790 "2018-04-07T21:40:39Z")
**Posts on this page:** 1
**Showing post:** 2

<div class="post-metadata">

### Author: ![simon](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/simon/32/339122_2.png) [@simon](https://meta.discourse.org/u/simon)
#### Post date: [4월 7, 2018, 11:11오후 UTC](https://meta.discourse.org/t/function-to-link-to-the-discourse-topic-from-a-wordpress-template/84790/2 "2018-04-07T23:11:47Z")

</div>

You can get the Discourse topic URL for a post with  
`get_post_meta( $post_id, 'discourse_permalink', true );`

Something like this will work inside the loop (for example, in `single.php`)

```plaintext
<?php
$topic_url = get_post_meta( get_the_ID(), 'discourse_permalink', true );
$comments_number = get_comments_number();

switch ( $comments_number ) {
    case 0:
        $comment_link_text = 'Start the Conversation';
        break;
    case 1:
        $comment_link_text = '1 Comment';
        break;
    default:
        $comment_link_text = "$comments_number Comments";
}
?>

<a href="<?php echo esc_url( $topic_url ); ?>"><?php echo esc_html( $comment_link_text ); ?></a>

```

---

_[View the full topic](https://meta.discourse.org/t/function-to-link-to-the-discourse-topic-from-a-wordpress-template/84790)._
