[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!
8 Comments
Nice one, WordPress is good in its super upgradability. Might be a little tidier and manageable in future if you added your extra bits to the wp-content/plugins folder. Then you can share it all with the rest of wordPressers out there nicely.
Pfft… “upgradability”… “manageable”… “share”… What are you thinking?!
I haven’t looked in to the plugin development for WordPress, but I guarantee that someone has already made this module anyway – most likely with “useful” features. If I dig that up I’ll whack a link here.
Nice background ;)What are you… some sort of super-designer?
Hey, I’m sure it says “background concept by AntonGraphics” somewhere around here. Its in a small font. Next to “Manton runs like a girl”
Great webpage dude!
Heres a plugin Get Custom Field. kind of fiddly, some people have troublees
I’ve been thinking of making the leap from Blogger to my own domain/WordPress. Do you think it’s worth making the leap?
Hey BHR, I don’t know if it is a good idea or not – I don’t know much about the community side of blogging, but I think the Blogger blogs would generate a lot more “accidental” traffic than if you do start up your own domain. Infact I have seen your PhotoSyndey site before when clicking around randomly in the top bar bit from other Blogger sites.
You might be best off having a look at your referrer logs to see how people are getting to your site. I don’t know if you get access to this kind of info on Blogger – maybe that is one of the plus sides of getting your own/domain – you get complete control.
Personally, I like hacking around with web programming languages and stuff more than writing a blog so its an easy choice for me :)