Wordpress: Create dynamic URL of discourse page

Hi all,

I’'ve set up my community so that each new post on Wordpress auto-posts to Discourse. The plugin creates a link to the discourse discussion dynamically at the bottom of every wordpress post.

I’m wondering if there’s a way to generate this link on each post in other areas too?

For instance, I want wordpress to also automatically generate a hyperlink on some text near the top of the page that links back to the discourse page for that particular post, but I have no idea how I would go about doing that.

The

2 Likes

We are doing the same by using the Gutenberg editor to display the post meta data. Please install this plugin:

JSM Show Post Metadata – WordPress plugin | WordPress.org.

And you will be able to view the discourse available data linked to that wp CPT. Next, you can use a Gutenberg or other plugin to display the discourse link to frontend.

1 Like

Ah ok I’ll take a look at that.

What I’m trying to do though is actually hard code something into the WP theme’s files so that the Discourse link for any given post is generated.

Would something like this work? (i saw this on another older thread): … etc

Would something like this work if I hard coded it into the theme’s files?

<?php
$topic_url = get_post_meta( get_the_ID(), 'discourse_permalink', true );
?>
<a href="<?php echo esc_url( $topic_url ); ?>">LINK TO FORUM</a>

Ok, so I figured out how to get it to do what i want.

<?php
$topic_url = get_post_meta( get_the_ID(), 'discourse_permalink', true );
?>
<a href="<?php echo esc_url( $topic_url ); ?>" style="color:#0271e1; font-size: 0.75em;" target="_blank">Join The Discussion</a>

This appears near the top of my articles. Is there a way to provide an atlernative link if the post in question does not have a discourse page (.e.g. older posts before Discourse was installed)

3 Likes

Yes, but it has nothing to do with Discourse, really.

Decide what link you would like to show instead, and then do a quick search on how to use “if” statements in PHP with posts that have empty or missing Wordpress fields.

2 Likes