Skip to content

Instantly share code, notes, and snippets.

@surrealroad
Last active March 6, 2016 17:12
Show Gist options
  • Save surrealroad/6990268 to your computer and use it in GitHub Desktop.
Save surrealroad/6990268 to your computer and use it in GitHub Desktop.
jQuery snippet to grab latest discourse forum posts and display them inside a container uses TBS classes
// usage
var forumURL = 'http://forums.com';
$('#discourse-latest-container').getDiscourseTopics(forumURL, 'latest');
// fetch discourse items
(function($) {
jQuery.fn.getDiscourseTopics = function (url, page) {
// load forum topics
if($(this).exists()) {
$this = $(this);
$.getJSON(url + "/" + page + ".json", function (data) {
var topics = data.topic_list.topics;
//console.log(topics); //uncomment this for debug
var limit = 6; // topics.length;
var result = '<nav class="list-group list-group-flush">';
for (var i = 0; i < limit; i++) {
result += '<a class="list-group-item" href="' + url + '/t/' + (topics[i].slug) + '/' + (topics[i].id) + '"><span class="badge">' + (topics[i].posts_count) + '</span> <h5>' + (topics[i].fancy_title) + '</h5></a>';
}
result += '</nav>';
$this.html(result);
});
}
};
})(jQuery);
@Qasem-h
Copy link

Qasem-h commented Mar 21, 2015

how to use it ?

@mrgithub
Copy link

mrgithub commented Mar 6, 2016

Any tutorial or example on how to use it as it's not working for me :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment