How to put Discourse comments somewhere other than the default comment location?

I’m customizing a WordPress site and I want to put a link to the Discourse thread for a given post in a custom location in the HTML.

(Details: I’m customizing a LearnDash course site and the LearnDash “focus mode” gets rid of most of the standard WordPress layouts stuff, including comments. I’d like to use one of the LearnDash hooks to insert a link to the forum for a given lesson.)

Can anyone point me in the right direction for some wp-discourse code I can invoke to emit “link to thread” markup?

Thanks!

2 Likes

Check out the heading, “Displaying the comments template with the Timber theme” at WP Discourse plugin tips and tricks.

3 Likes

Thanks… but that appears to be a bit out of date. The path referenced:

 WPDISCOURSE_PATH . 'templates/comments.php'

…doesn’t seem to correspond to any paths in my install of wp-discourse.

1 Like

I don’t even need to load the list of discourse comments; I’d be content with a link to the discourse thread. But I can’t figure out how to use the wp-discourse code to get at that link :-/

I wound up doing this, just referencing the postmeta without using any of the WP-Discourse code.

I was never able to get anything to work involving listing comments… all the functions I could find seem to want to return HTML with a bunch of {placeholders} in it :confused:

    $discuss_url = get_post_meta($post_id, 'discourse_permalink', true);
    $comment_count = get_post_meta($post_id, 'discourse_comments_count', true);
    if($discuss_url) {
        ?>
            <div class="lac-lesson-discuss">
                <a href="<?php echo $discuss_url ?>" target="lac_discuss">
                    💬 Discuss this! (<?php echo $comment_count ?> comments)
                </a>
            </div>
        <?php
    }
4 Likes

Wow, 7 years since your last post. Welcome back @avdi! Perhaps @simon can advise tomorrow.

3 Likes

You are right. That approach won’t work anymore. I think I can add a helper function to the plugin to make it easier to display the comments outside of the WordPress comments area. I need to make a small update to the plugin within the next couple of days, I’ll try to get a display_discourse_comments function added to the update. I’ll let you know when it’s available.

3 Likes

I’ve added a static helper function to the plugin that can be used to display comments without loading the Discourse comments template. The function is here: get_discourse_comments. It requires you to supply the post_id of the WordPress post that you want to display the comments for. Here’s a simple example of its use:

<?php
use WPDiscourse\Utilities\Utilities as DiscourseUtilities;
$discourse_comments = DiscourseUtilities::get_discourse_comments( 859 );
echo $discourse_comments;

It displays the comments in the same way as they are displayed if you select the
Enable Discourse Comments/Display Comments option (found on the plugin’s Comment Settings tab.) When this function is used, the value of that setting is ignored, but all other settings on that page are respected.

2 Likes