As I have recently mentioned, the ‘forces has just been at the sharp end of a code overhaul.
While appearances may be deceiving and the layout may look virtually the same it’s the changes under the hood that are interesting. This is why WordPress is not just semantic blogging software - it’s CMS to the core, without the bells-and-whistles.
So what triggered this code quest? Two reasons - I hated the complexity of the existing code and Chris got me thinking. :)
Firstly, gone is the complex, two page code and layout. It worked well, but was less than efficient handling posts. Two loops were required to generate what can more easily be done in one the_loop.
Secondly, the previous code base could not handle asides unless they originated outside the standard WP posts tables. For some that might be fine, indeed a number of ‘asides’ plugins exist, some of whom create their own tables - I wanted 1 source to rule them all, not 20..
I’ve been asked to indicate the code used, which I intend to do. I would challenge others who use customised code to share with others how you’ve achieved a particular goal - share your code..
So, to action my own challenge, this is basically how the ‘forces works. Quick, robin, to the code!
So, the_post() is called, generates the first full post (customisable to any number one desires) and a loop counter is used to keep track of progress.
``
``
``
So, if we have posts -> set a counter -> while have posts -> do something -> then increment $i by 1. When $i = $numcontents and thus is no longer less than, it will drop down to the next segment of code.
So what’s next? The next block is fairly simple - after the first n posts are displayed we drop into the following which basically will roll through whatever is in between the if and else. In my case I pull in asides using the awesome
mini post plugin and then the del.icio.us bookmarks:
Finally we drop down into what I class as remainders - older posts and content basically. Remember at this point we are still running within the original the_post() loop. Here’s my remainders code:
``
Obviously there will be more ’substance’ to each section - I’ve purposefully avoided showing the template code for posts, asides, del.icio.us and remainders to avoid confusion - hence the `` blocks to indicate where template code should be added.
One `the_post()` loop, one .php file (in my case index.php - I am considering using home.php however) and thus, one simpler block of code.
Here endith the lesson. :-)
≡ This is a journal entry relating to the topics of code, design, function.
Brendan Borlase is a Systems and Network Administrator living in Adelaide, Australia, having lived, worked and breathed Information Technology for over 12 years. Learn more.
Feedback is encouraged. If you would like to read more, consider subscribing to the regularly updated RSS Feed.


May 1st, 2005 at 4:08 am
Thanks very much Brendan. If other people join in and share some info on how they do some of their neat little things with WordPress, then those who are not as familiar with the_loop (like myself) and other WP functions can learn a lot from them. so I really appreciate what you have done here!
May 1st, 2005 at 8:42 am
Thanks very much Brendan.
You’re welcome. :)
I’m happy to share the wealth when it comes to WP. I have nothing to gain protecting code as though my life depended on it - life’s to short to be cryptic. :D
May 5th, 2005 at 4:46 pm
Hey, I shared my code in the first place!
Nicely done, I have been thinking of using a dedicated cat as an asides, and then calling that in an else block, if the_date for posts in the cat == the_date for the current main post…
Not sure yet.
May 5th, 2005 at 6:57 pm
Your code was my inspiration to play :)
It’s the easiest way - gives you a single cat that can be included, excluded or otherwise manipulated without impacting on anything else.
Check out the mini-post ‘asides’ plugin I mentioned though, it makes life easier - including the ability to have multiple different cat’s. It ‘marks’ the post differently to a normal post - thus it can then excluded from the normal ‘have the post’ loop via the options panel.
May 6th, 2005 at 5:28 am
I like to mark my asides with other relevant categories though. Is this maybe what is overcomplicating my set up?
May 6th, 2005 at 5:55 am
If your asides entry has a number of categories, then it gets progressively harder to filter them out I’ve found - in the end I started using mini-post as it makes the whole process simple.
Jun 22nd, 2005 at 3:34 pm
Thank you SOOOOOO much for the detail code, this rules. I run a community blog and I wanted to make a custom author page with links to the authors homepages, etc.
The codex documentation was lacking for learning how to create such a beast, I know nothing of basic php coding, and I don’t particulary want to go through a tutorial.
From your nicely laid out syntax I get the basics of php (its actualy similar to R, which I use all the time) and can tweak your loop to add a couple features. Now I can get on with my poject and focus on content instead of code.
A BIG thanks!
Jun 23rd, 2005 at 2:51 am
Wease,
You’re welcome. :)