Fetching recent topics from same category?
-
How would I edit the following to pull recent topics from the same category? Currently it's set to all categories.
-
Try changing this line
socket.emit('topics.loadTopics', {start:0, end: numTopics - 1, set:'topics:tid'}, function(err, data) {
to
var cid = ajaxify.variables.get('category_id'); var set = cid ? 'categories:' + cid + ':tid' : 'topics:tid'; socket.emit('topics.loadTopics', {start:0, end: numTopics - 1, set: set}, function(err, data) {
-
@baris I would have thought that worked which is does, but only when called manually via console - weird stuff. It keeps reverting itself back to all categories. Any idea why it would do this?
-
I put the console log there, and when I refreshed it says undefined, and pointed this line;
var cid = ajaxify.variables.get('category_id');
-
Just to make sure the code should look like this
var cid = ajaxify.variables.get('category_id'); var set = cid ? 'categories:' + cid + ':tid' : 'topics:tid'; console.log(cid); socket.emit('topics.loadTopics', {start:0, end: numTopics - 1, set: set}, function(err, data) {
If that is showing undefined in the browser console when you browse to a category, there might be a bug in the code, you can try updating to https://github.com/NodeBB/NodeBB/commit/53d20cf058d70f58b8242346f7203a5f7ce9f9ae#diff-d41d8cd98f00b204e9800998ecf8427e and see if it helps.
-
I updated and its the same thing. This is within a topic to show the latest topics within that same category. For example, in an Art category, it should show all recent topics for Art, not the whole site. See SS below:
-
I tested it with a global sidebar widget and it worked, I think you are not using a widget but just placing that code in the same template as the topic? In that case you can try doing
var cid = '{category_id}'; var set = cid ? 'categories:' + cid + ':tid' : 'topics:tid'; console.log(cid); socket.emit('topics.loadTopics', {start:0, end: numTopics - 1, set: set}, function(err, data) {
Not sure if that will work though, pinging @psychobunny.
-
Yes, that's what I'm doing and I'll give that a shot too!
-
Well, no that doesn't work either as it just shows
'{category_id}'
in the console with no ID #. Rather interesting.