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

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.

3 Likes

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)

<?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> 
5 Likes

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

3 Likes

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