# 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:** [April 7, 2018, 9:40pm 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:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![icaria36](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/icaria36/32/426431_2.png) [@icaria36](https://meta.discourse.org/u/icaria36)
#### Post date: [April 7, 2018, 9:40pm UTC](https://meta.discourse.org/t/function-to-link-to-the-discourse-topic-from-a-wordpress-template/84790/1 "2018-04-07T21:40:39Z")

</div>

Hi, in order to show the number of Discourse comments in a WordPress article without showing the comments themselves, we have added this simple code to single.php in our child theme:

`<?php echo get_comments_number(); ?> comments`

This will show the number of comments just fine, but how to make that this string links to the corresponding Discourse topic? I could not find a function, and I could not make `{topic_url}` work there either.

---

<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: [April 7, 2018, 11:11pm 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>

```

---

<div class="post-metadata">

### Author: ![icaria36](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/icaria36/32/426431_2.png) [@icaria36](https://meta.discourse.org/u/icaria36)
#### Post date: [April 8, 2018, 8:50pm UTC](https://meta.discourse.org/t/function-to-link-to-the-discourse-topic-from-a-wordpress-template/84790/3 "2018-04-08T20:50:31Z")

</div>

It works! Flawless. @simon you rock, also on Saturday night. Thank you very much.

---

<div class="post-metadata">

### Author: ![jomaxro](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jomaxro/32/126216_2.png) [@jomaxro](https://meta.discourse.org/u/jomaxro)
#### Post date: [April 14, 2018, 10:00pm UTC](https://meta.discourse.org/t/function-to-link-to-the-discourse-topic-from-a-wordpress-template/84790/4 "2018-04-14T22:00:06Z")

</div>

This topic was automatically closed after 2 days. New replies are no longer allowed.
