<script type="text/discourse-plugin" version="0.1">
api.onPageChange(() => {
// Check if an element with ID 'topic' exists
if ($('#topic').length > 0) {
// If 'topic' element exists, find 'data-user-id' attribute
var userId = $('#topic article').data('user-id');
// Check if 'data-user-id' attribute exists
if (userId !== undefined) {
// Make an API request to get user info
apiReq(`admin/users/${userId}.json`, 'GET')
.then(data => {
// Handle the response data
console.log('User post count:', data.post_count);
// Check if the user has a value in user_fields["1"]
if (data.user_fields && data.user_fields["1"]) {
// Display the user field inside the first 'names' div
displayUserField(data.user_fields["1"]);
}
// Display the post count inside the first 'names' div
displayPostCount(data.post_count);
})
.catch(error => {
// Handle errors
console.error('API request error:', error);
});
} else {
console.log('No data-user-id attribute found');
}
} else {
console.log('No element with ID "topic" found');
}
});
// Function to display the user field inside the first 'names' div
function displayUserField(userFieldValue) {
// Find the first 'names' div
var firstNamesDiv = $('.names:first');
// Create a span element with classes 'user-title'
var userFieldSpan = $('<span></span>').addClass('user-title');
userFieldSpan.text(userFieldValue);
// Append the span element to the first 'names' div
firstNamesDiv.append(userFieldSpan);
}
// Function to display the post count inside the first 'names' div
function displayPostCount(postCount) {
// Find the first 'names' div
var firstNamesDiv = $('.names:first');
// Create a span element with classes 'user-title'
var postCountSpan = $('<span></span>').addClass('user-title');
postCountSpan.text(postCount + ' posts');
// Append the post count span element to the first 'names' div
firstNamesDiv.append(postCountSpan);
}
</script>
In order to obtain the post_count value when viewing a post or a user’s card, it is necessary to serialize the data to include this field. This can only be achieved with the help of a plugin and relying on requests per user is not a realistic solution.
Give it a try. The JS part is in assets if you want to take a look.
One specific thing you may have missed in using the API is the ability to customize via outlets.
Let me know if you want more options.
I don’t know if I should release this plugin as it’s small, but here we go anyway:
Thank you so much for the effort. I’ll try it.
My actual plan was to achieve the following feature
Show post count of user in post and comments next to avatar.
Assign a default title to all the members of a group (ex: Admin)
Show trust level beside avatar
Show badge beside avatar.
Show a user_field value beside avatar.
And options are changeable by admin.
I also planned to hyperlink the post count so others can click and see the list of posts.
Although, it’s a small plugin, I believe it worth publishing. You may consider to add new features as suggested.
Thank you
I actually trying to gain something like postCount + TopicCount
simply want to count topics as post.
Also trying to show that after user title (if have any). I tried to modify the plugin code (after forking) but no luck so far.
I cant edit, so, I just installed and tested the plugin, and it’s working very well. Except on mobile, where the total number of posts is being displayed on the left side of the name.