[update: this code for this page only applies to WordPress 1.2 - for more recent stuff check my WordPress section]
A couple of days ago I began giving this blog a refreshing cosmetic overhaul. Unfortunately, not being very good with cosmetics I soon realised that to maintain my interest in its redevelopment I would have to add a few crunchy extras. The result (besides the mind-blowing nav bar starfield) was the "news" section, and my personal favourite: the "random space".
MrSpeaker is running off the amazing WordPress bloggin' software. I'm going to refer to it as WP from now on, 'cause my fingers are sleepy. WP has been developed in PHP, of which I have a passable knowledge. I decided to use that knowledge for the forces of mediocrity - a Content Management System. Sort of.
After hunting around various files in the installation, I found a few useful function calls that made this job a piece of cake. The function
get_results() is what WP uses for fetching a bunch of posts and displaying them. I farted around with this for a couple of minutes, and when I didn't figure it out I went on to a less weird function
get_row(). This did the trick nicely.
First, I started a new blog post that I titled "randomspace". WP lets you mark posts as "published", "draft" and "private". As I don't know what the "private" posts are for, I decided I'd use them for my CMS. There now exists in my blog a private entry with the title "randomspace".
Then I wanted to wack the contents of that post in my index.php file (which WP uses for all page displays) Using the WP function, I grabbed the database row for this post like so:
$RandomSpace = $wpdb->get_row("SELECT ID, post_content FROM $tableposts WHERE post_title = 'randomspace' and post_status='private' LIMIT 1");
This returns an object containing each field the SQL query grabs (You can add whatever you want in the SELECT query - post_date, author etc. Have a look in the posts table in your mysql database for the field names)
You can then use the sql row like any PHP object. On MrSpeaker I grabbed the content of the post into a variable thusly:
$RandomSpaceContent = $RandomSpace->post_content;
Once you have it in a variable, you can print it out wherever you like! I put mine in a list element in the menu.
<li>Random Space: <?php echo $RandomSpaceContent ?></li>
Whatever you put in the randomspace post will appear in the menu. Pretty cool hey. I'm going put them on the other pages in my blog as page introductions. Later.
The next step once this Grade-A CMS was flying along was to add an "edit this" link next to the space that, owing to my powers of logged-in-ed-ness, only I can see. After some more hunting around the files here's what I came up with:
get_currentuserinfo(); // WP func to get user info.
if ( $user_level == 10 )
$location = get_settings('siteurl') . "/wp-admin/post.php?action=edit&post=$RandomSpace->ID";
$RandomSpaceContent .= " <a href="$location">*</a>";
Well thats it for the "Random Space" section. Although I used WordPress, the same general idea will work on any bloggin' software. Just have a sniff around the code 'till you figure it out. The "news" bit is a bit more tricky (just a bit). I'll do that one later - but here's a hint on how I did it: All news items are stored in a single post. Ooo, cryptic!