Display Top Posts from a FluentCommunity Space
Can't believe this hasn't been integrated into FC yet. Code snippet below displays the latest posts from any space on your inner site.
After you add snippet, display forum posts on a page using shortcode, for example, this will display 3 posts, from community space #1:
[fluent_space_posts space_id="1" limit="3"]
Blessings upon your family. π
// Shortcode: [fluent_space_posts space_id="1" limit="3"]
add_shortcode('fluent_space_posts', function ($atts) {
$atts = shortcode_atts([
'space_id' => 1,
'limit' => 3,
], $atts, 'fluent_space_posts');
$spaceId = (int) $atts['space_id'];
$limit = (int) $atts['limit'];
try {
// Query directly from the database using FluentCommunity models
$feeds = \FluentCommunity\App\Models\Feed::where('space_id', $spaceId)
->orderBy('id', 'desc')
->limit($limit)
->get();
if ($feeds->isEmpty()) {
return '<p>No posts found.</p>';
}
$html = '<div class="fluent-space-posts">';
$html .= '<ul>';
foreach ($feeds as $feed) {
// Build title from post content
$title = $feed->title ?? '';
if (!$title && !empty($feed->message)) {
$title = wp_trim_words(strip_tags($feed->message), 15, '...');
}
if (!$title) {
$title = '(Untitled)';
}
// Build URL to the feed/post
$url = home_url('/hub/feed/' . $feed->id . '/');
$html .= '<li class="space-post">';
$html .= '<a href="' . esc_url($url) . '">' . esc_html($title) . '</a>';
$html .= '</li>';
}
$html .= '</ul>';
$html .= '</div>';
return $html;
} catch (\Throwable $e) {
error_log('fluent_space_posts error: ' . $e->getMessage());
return '<p>Unable to load posts.</p>';
}
});
β¦ and Happy New Year!
Any screen how it works?
After you add snippet, just use shortcode, for example...
[fluent_space_posts space_id="1" limit="3"]
Lovely. Thank you so much!
I am not exactly sure what it does, or what I am looking for.
- You create a wordpress post, or page, on your website, that is OUTSIDE of the forums.
- You want to display forum feed on that page
- Add snippet and insert shortcode
- Now people can see posts from the forums and perhaps be encouraged to participate if they click.