So we have our list of categories, in this part of the tutorial we are going to do two things:
- Add breadcrumbs (so you can navigate back “up” the forum)
- Display the list of topics within the current category
Looking to end up with the following:
You can see that within a given category, sub categories are listed (if any), followed by the topics within that category. Normally I would try and avoid listing both, but that totally depends on how you set up your forum – whether you allow topics in certain categories for example.
I’m using wps_forum_page_2.php and wps_forum_page.css files for this part – you’ll find them in the tutorials folder in the wp-symposium plugin, or download them from here.
You can see above the forum, I am displaying the “breadcrumb trail” of where you currently are in the forum structure – it allows you to move back up the hierarchy. This is part of the WPS UI class, so we need to include the class:
$wps_ui = new wps_ui();
Then to insert the breadcrumbs (line 61), we simply add:
echo $wps_ui->get_breadcrumbs($catid, 'catid');
There are a number of parameters we can pass to this method, namely:
- The category id of the current level of the breadcrumbs [mandatory].
- The parameter to be added to the breadcrumb links that you use in your code (catid in my case – and this is the default). Optional, but recommend you include for clarity in your code.
- Show link to the top level or not (true/false), defaults to true.
- The separator character, defaults to right arrow.
What is $catid I hear you ask? Don’t worry for now, this will make sense in a short while and I’ll remind you about the breadcrumbs.
Note that the current category is not included as a hyperlink.
For sub categories, I want to show the sub categories and the lasted topic in that sub category, along with some extra information like when and the author.
So lines 66-99 get and loop through any sub categories. For each sub category, the latest topic is retrieved as before (line 84).
What’s been added here this time? Well links so you can move “down” into your forum. Remember the breadcrumbs provide you with a way of moving back “up”.
First take a look at line 81, where a hyperlink is added to the current page with catid as a parameter for the category to show.
See where catid comes in now with the breadcrumbs?
To react to the passed category ID (catid), that is line 58:
$catid = isset($_GET['catid']) ? $_GET['catid'] : 0;
So $catid is set to the passed parameter, or 0 if nothing is passed.
Remember the breadcrumbs? This is the value that is passed (told you I’d remind you, lol).
Listing the topics
Having listed the categories we are now going to flesh out the topics list, providing information on the last reply. This code segment is line 101-152.
I get the list of topics for this category with:
$topics = $wps_forum->get_topics($catid);
If there are any, the table header is shown, and the all topics are looped through, displaying initially the topic subject. I limit the maximum length to 60 characters, feel free to change!
Showing the last reply
The last reply is retrieved on line 125:
$last_reply = $wps_forum->get_replies($topic->tid, 0, 1);
The topic ID is passed, starting at 0 (the first one) and retrieving only one. If there was one (there may not have been a reply yet), it is displayed along with information on the topic author, their avatar and so on.
We’ve covered this before, but briefly lines 134-136 gets and displays the avatar and link to the author’s profile page. Lines 139-144 show the reply. I’ve previously restricted the length and sorted out the formatting at lines 128-131.
Showing the number of replies
It can be useful to know how many replies have been posted to a topic, and this can be easily shown, line 148:
The next tutorial will allow us to click on a topic title and display it, along with all the replies to that topic. Yummy!
For reference… fields returned by get_replies()
- tid – topic ID, integer
- topic_group – 0 for none (ie. main forum), or group ID if a group forum, integer
- topic_category – which category the topic belongs to, integer
- topic_subject – if the initial topic post, the subject of the topic, text
- topic_post – the body of the topic or reply, text
- topic_owner – the user ID of the topic creator, integer
- display_name – WordPress display name of topic_owner
- topic_date – date created, or last replied to, datetime
- topic_parent – if a reply, the ID of the topic, or 0, integer
- topic_approved – defaults to ‘on’, or ” if awaiting moderation (or use true/false)
- topic_answer – is this the answer to a topic? ‘on’ if yes, otherwise ” (or use true/false)