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

(Quim Gil) #1

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.

(Simon Cossar) #2

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)

$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';
    case 1:
        $comment_link_text = '1 Comment';
        $comment_link_text = "$comments_number Comments";

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

Is there a way to show the number of ❤️ in a WordPress template?
(Quim Gil) #3

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

(Joshua Rosenfeld) #4

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