Include the featured image in the post

I’m trying to include the featured image of my wordpress post to be displayed in the Discourse linked topic. Ideally it would show above all the other post content (regardless on whether or not “show full post”/truncation is turned on or not). Any suggestions would be greatly appreciated!

Thank you

There’s an example of how to do that here: WP Discourse template customization

The example is under the heading 'An example that adds the featured_image to the publish template.'

4 Likes

Awesome. Thank you simon. I did a search, but missed this example. I’ll see if I can’t get it working :slight_smile:

Alright, I tried to get that to work but I was thrown a PHP error. (unexpected < somewhere). I was able to add your other suggestion perfectly fine, Clearing the text from the shortcode, but not when including the featured image. I imagine I’m copy+pasting too many things or the wrong things into my function.php file. I’m honestly confused with the majority of the following paragraph:

This can be done in a custom template by copy and pasting the publish_format_html() function from it’s github, renaming the function by adding a namespace, adding the {featuredimage} template tag, removing the call to apply_filters from the function’s return statement, and then hooking into the new function. Note: you also need to remove the self::get_text_options() statement from the original function.

And then additionally confused by the code you’ve added following it.

My function.php file is super simple at the moment:

<?php
add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles', 15 );
function theme_enqueue_styles() {
    wp_enqueue_style( 'youplay-child', get_stylesheet_directory_uri() . '/style.css' );
}

add_filter( 'wp_discourse_excerpt', 'my_namespace_fix_shortcodes' );

function my_namespace_fix_shortcodes( $excerpt ) {
    $excerpt     = str_replace( '[nk_title]', '<h2>', $excerpt );
    $excerpt     = str_replace( '[/nk_title]', '</h2>', $excerpt ); 
    $excerpt     = str_replace( '[yp_text]', '<p>', $excerpt );
    $excerpt     = str_replace( '[/yp_text]', '</p>', $excerpt );

    $excerpt     = preg_replace('(\\[([^[]|)*])', '', $excerpt );

    return $excerpt;
}

Sorry for my confusion, but do you have an guidance on this?

I’m not sure, but it looks like there is a problem with the regular expression in the preg_replace statement. Try:
$excerpt = preg_replace( '/\[.*\]/', '', $excerpt );

The best way to work things like this out is to do your development in a local development environment and figure out how to log the output so you can see what’s going on. (You can still connect to a remote Discourse forum. Just publish your posts to a private category.)

Getting too involved with basic PHP programming is probably outside of the scope of this forum. If you’re really stuck, send me a message and I’ll see if I can help.