Display Category Box with posts inside
-
I would appreciate any help with this. I’m teaching myself how to customize WordPress and I’ve run into a roadblock.
URL – http://www.journeysbysteve.com/sandnsun
On this theme, there are a series of boxes beneath the slider (under the heading Latest News). Currently, this theme is set up to display the most recent post from a category.
I would like to change this so that the box shown has a permanent category, and I can have some intro text with a list of that category’s last three posts linked within the box. (Lordy, I hope I explained that well enough..)
I’ve tried this and that and broken the theme so many times that I admit needing help!
Can anyone guide me here? Thanks!!
-
To clarify what you’re asking, let me see if i have this right..
Instead of each of those boxes pulling in the content/title, etc. from seperate posts, you’d like to change that to now pull in a category each, and list 3 posts from each of those categories.
Would that be correct?
8 boxes, currently 8 posts, to be switched for 8 categories showing the 3 most recent posts for each relevant category.
Yes, that’s correct!
If you paste the contents of whatever file renders those boxes into a pastebin, and report the link back here we can work from there… 🙂
Here you are – the portion of the code that creates the current view, and the stylesheet portion that relates to that code.
Great.. 🙂 Do you have 8 categories in mind you want to use, or do you have a particular number of categories in mind? And how do you want to determine which categories to use? By name, by ID, or by pulling the data from somewhere you’ve stored it in the database already?
What should happen if a chosen category doesn’t have posts? .. hide the box, or show it with a message?
Should be pretty simple, but i’d rather not have to make dozens of changes, so i just want to be clear about how you are expecting the code to work… 😉
I do prefer to use category IDs. I have set categories but want to be able to change them later if needed – cat id’s seem to be the best way to do that. Keeping the box even if there are no posts is kosher, too. Kind of need to do that to maintain integrity of the design, methinks.
I like how the design is now, with a picture for the box (optional) – not sure how to add that unless I do it “traditionally.”
I see an issue here in that in order to query for 3 posts from each category you’ll end up needing 8 queries to the database to archieve you want (one per category).
If that’s acceptable i can provide something no problem, but there may obviously be downsides to doing so if you’re loading this on the main page, since those 8 queries will run in additional to any other queries throughout the page.. (you’ll like have anything upwards of 15 queries on your main page(looking at what you have on there already)).
I’m going to have a little think about how i/we might be able to create a custom query to do it in one, rather than several.
I hadn’t thought about that – I don’t want to drive up the calls (I think there’s plenty already1) needed to load the page. We can eliminate the calls, and have a “read more” type of link that will link to the category page listing all the posts. If that’s done, I’d like to be able to custom edit text within the box as a teaser – doesn’t have to be something queried but can be hardcoded into the code itself.
I think writing a single query for this is beyond me (i’m not sure such a query could be written either), what do you want to do about displaying data in those boxes?..
Maybe a comprimise would be limit those posts to one particular category? (that’s dead easy to do – leaving it still pulling posts).
Or another idea could be.. to pull the category descriptions into those boxes(assuming you’ve given them descriptions), and have a link that points to that given category’s page? You’d then having something to fill the boxes with. Basically, the title would become the category name, the post content becomes the category description, and the readmore link becomes a link to that category’s archive page (or something like that).
I like the category description idea. I’ve rarely used that field in WP but I’ve seen themes that do use them. I’ll have to dig around and see how to put that together; not really sure how to do that AND keep an image in the box too, since the original theme pulls the image from a post custom field (I think).
Well i’m sure we could find a way to pull some images to go alongside the category data if you want to.
Something like, if an image exists with a name that matches the category, put it in, else use a default(or none)… Or if you’re using 8 set categories anyway, then we could pull the images and categories both in by ID/name (if they’re not changing, i think it would make sense to just pull them in via ID or name).
I’m here if you need to some help with implementation, or if you’d like an example to work from, then i’m sure i could provide one.. 🙂
I would appreciate an example – all my attempts at changing it screw things up horribly and don’t turn out how I thought they would.
Give this a try, taking care to insert it correctly (it’s just the only opening and closing PHP tags you need be aware of).
http://wordpress.pastebin.ca/1859071
Adjust the string at the top so it contains the IDs of the categories you wish to use..
NOTE: Unfortunately i couldn’t find an easy way to reference or lookup an attachment based on name, so the alternative(which may or may not be suited), was to opt for looking in the theme’s folder for images with a name matching the category. If you’re using 8 set categories i can’t see why this should be problematic.
A few things were assumed(which you can easily change), firstly that the files would be directly in the theme’s main folder, secondly that the image would have a PNG extension, and additionally that an image called defaultcat.png exists to sit in place of those that don’t exist. Each of these things can be changed wtih relative ease though.
Let me know how the code works out for you..
Sorry I haven’t gotten back before now. I’ve been out of town at a conference (yecch).
Thanks for the code – it worked! (Took me a few minutes to get it right along with the original code) I’m studying it to see what you did and why, and if I have any questions I will ask.
I appreciate it very much!
This is exactly what I need. Just a question, where exactly do I paste it in the code? Do I replace another part of the Main Index code?
Thanks!
The topic ‘Display Category Box with posts inside’ is closed to new replies.