Pages Menu
TwitterRssFacebook

Posted by on Dec 24, 2011 in Development

Coming Soon … is here!

This is a celebratory post, as I’ve managed to do some PHP editing, and didn’t break anything, or kill anyone.

I have a very bad relationship with PHP. I’m pretty sure it’s nothing to do with language itself (I don’t know enough about it to form an opinion), but to do with the environments in which I use.

Whenever I use PHP, it’s always on live systems, so it’s always high pressure. It’s never in an IDE, always Notepad. There’s never any source control, just a folder called ‘backup’. To make things worse I’m always altering or adding to a larger system I don’t fully understand, such as WordPress.

Consequently, it’s probably no surprise that things don’t work first time, and when they don’t work, something usually breaks, in a fairly terrible way. I don’t understand the errors, unless they’re really obvious. Basically I’m out of my comfort zone.

But, this blog is PHP, it’s WordPress. And, there was something I’ve been wanted to do for a little while. I often write posts in advance of when they’re published, and schedule them. Mostly this is because I tend to write 2 or 3 posts at a time, and I’d like to give the impression that I post more regularly than I do.

I found a plug-in, called the Future Posts Widget. This did almost exactly what I was looking for, except that I didn’t like the format of the output. For instance, the widget just output the title of the post. I wanted to keep the title a secret, but I did want the publish date, and the categories where the post would go to. So, I decided to try for some PHP editing.

Actually, it wasn’t quite as bad as I thought, mostly because WordPress seem to have provided lots of very useful functions for getting to data such as post name, date, category easily. Using the WordPress Dashboard I was able to edit the source code of the Future Post Widget, and modified it to:

while( $the_query->have_posts() ) :
$the_query->the_post();
echo "<li>";
echo 'on ' ;
echo the_time('D d');
echo ' at ';
echo the_time('H:i');
echo ': a post about ';
echo the_category(', ');
echo "</li>";
endwhile;

There’s a couple of things I really like about this. Firstly, you can specify a query, and then step through the results. Nothing so amazing about that, except that when you do, WordPress knows you are stepping through posts, so allows you to call the various functions, such as the_time, the_category etc. In WordPress Speak this is, I think, called The Loop and there’s plenty of other, more interesting things you can do with it.

My only slight annoyance is that, because the_category outputs the standard list of categories, they are output with the same CSS classes as everywhere else, so they get styled the same. Ideally, I’d create my own version of the_category which didn’t mark up the HTML the same way, so I’d get “normal” hyperlinks for categories. I’m not that worried though.

Anyway, you can see the results down the right-hand sidebar.

Written by Tom Morgan

Tom is a Microsoft Teams Platform developer and Microsoft MVP who has been blogging for over a decade. Find out more.
Buy the book: Building and Developing Apps & Bots for Microsoft Teams. Now available to purchase online with free updates.

Post a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.