What a catchy blog post title
Anyways, been working on a wordpress cms site and have found a few bits and bobs useful, so thought I’d blog about it. Mostly for my own benefit for future reference, but if someone can make use of it, then cool.
useful plugins
Found the following plugins very handy when using wordpress as a cms…
cms – The hint is in the title, but it’s really excellent. It basically removes all the confusing cruft that wordpress has in order to focus more on it’s CMS bits. Simplifies the admin massively and allows you to turn things off in the admin such as posts/comments etc.
cms page tree view – Another must for a cms wordpress. Allows you to view the pages in a tree like structure. It does it really nicely too, with nice little hovers and a clean, simple interface.
more fields – The site I was working on required a sidebar which contained lots of different bits of functionality. Images, content, lists etc. This plugin allows you to specify different fields to use for different sections. It is based around custom fields, but makes them so much easier to use and manage and gives you things like a content editor for them.
wp list tweets – Like every site in the world right about now, I needed to list the latest tweets. There are hundreds of plugins to list tweets, but I found this one was the smallest, simplest and did exactly what I wanted to with minimum fuss. Basically you chuck it a twitter username and it displays the latest X tweets, simples.
cleaner gallery – A gallery page was also required for the site and I found this gallery plugin excellent. The lightbox integration is particularly useful.
wp contact form – Needed a small and simple contact form, again there are lots of contact form plugins, but they all seemed to complicated for my liking. This one is dead simple, the only drawback is that it has no anti-span functionality, so if your site has a fair bit of traffic and you don’t want spam, maybe this isn’t for you. However, there do seem to be re-hashed versions of this which have anti-spam features such as captcha included.
tubepress – There were a ruck of videos on a youtube channel, which needed to be brought into the website and displayed in a gallery. This plugin does exactly that. Nice.
exclude pages – Dead simple, if you want to exclude a page from the navigation, this allows you to do so.
setting it up
Did the following things in the admin in order to make it work. The site I was working on was to be used primarily as a cms with a single page being used for it’s blog.
settings > reading
Front displays as a static page with my ‘home’ page set as the front page and my ‘blog’ page set up as the posts page. Home and blog are just the names of the pages, they could be named whatever.
cms plugin settings
Only turn on the posts component. Only because I needed to have a blog page, otherwise this would have been turned off also.
more fields plugin settings
- New input box ‘Sidebar’.
- Used only for pages (not posts)
- Added sidebar-image field as a file-list. A file list is a dropdown of media items.
- Added sidebar-title field as a text box. This allows the administrator to add a title in the sidebar (duh!)
- Added sidebar-content field as a content editor. This allows the administrator to put whatever the hell content they want in their sidebar (to appear under the sidebar image and title)
custom theme
I created a custom theme, just to keep all the stuff I was doing neatly contained within its own theme. For this I used Elliot Jay Stocks Starkers Theme. I can’t recommend this highly enough. If you are creating a theme from scratch, then use this theme as a base, done.
codes fiddlings
Mostly amends to the sidebar. I was never going to use the widgets on the sidebar, so if you are, then my changes may not be that advisable.
So here is the code I added to my sidebar.php. You can read the code comments to see what’s going on. Also, note that the mismatch of code and html is not something I’d encourage, but this is wordpress remember.
<?php
global $wp_query;
/**
* If we are on a single blog page or a blog category page, we won't
* have the blog listing page within wp_query. So use the get_option()
* function to get the page id of the page we specified to list posts.
* Otherwise we can get the page_id from the wp_query.
*/
if (is_single() || is_category()) {
$page_id = get_option('page_for_posts');
} else {
$page_id = $wp_query->queried_object->ID;
}
/**
* Get our custom field sidebar-image and display that shit
*/
if ($sidebarImage = get_post_meta($page_id, 'sidebar-image', true)) {
?>
<div class="sidebar-image">
<img src="<?php echo $sidebarImage; ?>" alt="Alt text here" />
</div>
<?php
}
?>
<?php
/**
* Sidebar content we want to display latest tweets if on the home page,
* otherwise show the sidebar title, sidebar content and any children pages
* we may have
*/
?>
<div class="sidebar-content">
<?php
/**
* If we're on the homepage, then we want to show the latest tweets.
*/
if (is_front_page()) {
?>
<div id="footersm" style="padding-top: 10px">
<a href="http://twitter.com/yourtwitteraccount" target="_blank"><img src="<?php bloginfo('template_directory'); ?>/images/twitter-20x20.png" alt="Follow us on Twitter" title="Follow us on Twitter"></a>
</div>
<h2>@yourtwitteraccount feed</h2>
<?php
wp_list_tweets();
} else {
?>
<h2><?php echo get_post_meta($page_id, 'sidebar-title', true); ?></h2>
<?php echo get_post_meta($page_id, 'sidebar-content', true); ?>
<?php
/**
* Wanted to list children pages in sidebar, so check if the current
* page has a parent and if so, get the root page id.
* Otherwise just use the current page_id as the parent
*/
if ($wp_query->queried_object->post_parent) {
$ancestors = get_post_ancestors($page_id);
$root = count($ancestors)-1;
$parent = $ancestors[$root];
} else {
$parent = $page_id;
}
/**
* wp_list_pages() will get us list of pages under a certain page_id
*/
wp_list_pages( array( 'child_of'=>$parent,
'title_li'=>'',
'depth'=>1 ) );
}
?>
</div>
Needed to bring out the latest 2 posts on the homepage as well, wanted to keep code to a minimum and not introduce any functions/classes, but if you have a better set up than me then put this in your functions file or better still a nice little helper class or something, but here is what I used to pull out latest 2 posts on the home page…
<?php if ( is_front_page() ) { ?>
<div id="latest-posts">
<?php
/** Set up our options, i.e. 2 latest posts */
$options = array('numberposts' => 2,
'orderby' => 'post_date',
'order' => 'desc');
$i = 1;
foreach (get_posts($options) as $post) {
?>
<div class="post num-<?php echo $i; ?>">
<h3><?php echo $post->post_title; ?></h3>
<span class="date">Posted on <?php echo date("D, j M Y", strtotime($post->post_date)); ?></span>
<div class="content">
<?php echo (strlen($post->post_content) > 200) ?
substr($post->post_content,0,200).' <a href="'.get_permalink($post->ID).'">Read more...</a>' : $post->post_content?>
</div>
</div>
<?php
++$i;
}
?>
</div>
<?php } ?>
And, that’s pretty much it! Nothing too advanced or crazy, but just some useful things for me to remember for next time. I’m also not quite finished, so I may add to this list.