WordPress
Adding pagination to your blog
Most WordPress themes come bundled up with “next page” and “previous page” links allowing you to navigate from older posts or newer posts. (The number of posts displayed on your blog overview and archives can be set in the Settings > Reading section of your admin.)
These links are great, but they can be improved by pagination which clearly indicates how many pages there are. As always there are many WordPress plugins for this, but there’s really no need. I personally prefer to keep plugins to a minimal and thus found the following tutorials very useful.
Kreisi’s tutorial provides you with all of the code that you need to add pagination to your site. Once you’ve read his detailed information take a look at Veron’s post at Sparklette Studio. She made a few modifications and enhancements to the code.
Removing ellipses from WordPress excerpts
WordPress allows you to display either the full content or excerpts of your posts.
When creating a template for search results, I do find that displaying excerpts rather than a full post improves usability. However if you simply use the excerpt function, you’ll notice that ellipses [...] are added at the end.
Personally I don’t find these useful at all and would prefer to have a link.
An easy way to fix this is by inserting the following code in your functions file:
function custom_excerpt_more($more) {
return '<br /><a href="'. get_permalink() .'">read more</a>';
}
add_filter('excerpt_more', 'custom_excerpt_more');
This will insert a break tag and add a link to the rest of the post.
You can also control the length of your excerpts if you wish. Simply add the following to your functions file.
function custom_excerpt_length($length) {
return 20;
}
add_filter('excerpt_length', 'custom_excerpt_length');
This function will limit your excerpts to the first 20 words.
Be careful though, if your post is less than 20 words, then the link won’t appear.
Hosting requirements for WordPress
The main advantage of using WordPress as a Content Management System (CMS) is the ability to make your own website edits. Gone are the days of finding a typo on your site and not being able to do anything about it. However a website powered by a CMS isn’t the same as a static website. In fact a site that is powered by a CMS is called dynamic, not static.
When making changes to a static site, you’re web developer modifies the code of each HTML page. Changes on a WordPress site are done by modifying the content which is stored in a database. The WordPress templates, coded using PHP, then pulls the content dynamically from the database and displays the webpage.
Thus when planning your WordPress, you’ll need to make sure that your hosting provider offers the following:
- PHP version 4.3 or greater
- MySQL version 5 or greater
Any server that runs PHP and MySQL will do, but an Apache server is the most robust and has the most features for running a WordPress site. Some hosting provider will tell you that Microsoft based servers are perfectly fine, but please don’t listen to them. The set up is quite difficult and no fun at all.
Having installed hundreds of WordPress sites, I am happy to recommend the following hosting providers.
These hosting providers all have very good WordPress support and great customer service.
Once you’ve set up your hosting, you’ll need to provide your web developer with the following:
- Access to your hosting provider control panel - This is needed to set up the database
- FTP access – This is needed to install the files on your server
Setting up a WordPress site is a bit more complicated than a static one, but with WordPress’s popularity, more and more hosting providers and making the necessary changes to offer full WordPress support.
Use jQuery to hide/reveal children of a WordPress page
I recently launched a beautiful website for Kurt Jurek, registered acupuncturist and herbalist. As some of you may know, Chinese medicine can be used to treat a huge amount of ailments. As a result when putting together content, the amount can be overwhelming and difficult to organize. Combine this with the need to use a CMS and you’re task is quite daunting. This is when adding a bit of JQuery to your WordPress theme helps.
My number one task when setting up WordPress as a CMS for a client is to make updates easy to understand. If clients need to use HTML in their admin, then I consider this a failure. WordPress in itself is quite easy for most people to grasp and understand and it should be the case at all times.
Continue reading »
Modifying the WordPress admin
I’ve been using WordPress to build 99.5% of my sites lately. Even if a particular client doesn’t need a blog or won’t be making frequent changes, I do like to provide the ability to update content if needs be.
As a result, the WordPress back end has too many features for such a client. After reading the tutorial from Six Revisions, I’ve been modifying the WordPress admin, but using the following:
/* Remove Posts and Tools from Admin menu items */
function remove_menu_items() {
global $menu;
$restricted = array(__('Posts'),__('Tools'),__('Comments'),__('Links'));
end ($menu);
while (prev($menu)){
$value = explode(' ',$menu[key($menu)][0]);
if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){
unset($menu[key($menu)]);}
}
}
add_action('admin_menu', 'remove_menu_items');
Once added to my functions.php file, the menu items posts, tools, comments and links disappear. How handy is that?
Updates to the WordPress Comment Form
Justin Tadlock recently wrote a great post and tutorial aimed at theme developers. His article pointed out that with the release of WordPress 3.0, a new function for handing comment forms: comment_form(), was introduced.
This new function offers much more flexibility for plugin authors and is really easy to use. All you have to do is open up your comments.php file and locate this bit of code:
<div id="respond">
Lots of HTML code to output form elements.
</div>
and replace it with:
<?php comment_form(); ?>
You may also need to tweak your CSS, but overall, that’s the only change needed.
I took Justin’s advice and updated my basic theme and noticed a few things.
- The default title is Leave a Reply
- The default submit value is Post a Comment
- The comment after the form ‘You may use these HTML tags…‘ was added
In order to change these easily, I updated my function file with the following code:
<?php
//Change comment form defaults
add_filter( 'comment_form_defaults', 'basic_comment_form_defaults' );
function basic_comment_form_defaults( $defaults ) {
$defaults['label_submit'] = __( 'Submit' );
$defaults['title_reply'] = __( 'Leave a Reply' );
unset($defaults['comment_notes_after']);
return $defaults;
}
?>
Displaying latest and most popular blog posts in your WordPress footer
Fat footers are very handy for displaying additional information on your site. On a previous version of this website I used to display the latest blog posts, while on this newer version I opted to display the popular posts. I thought I would share these code snippets here.
Display Recent Blog Post
In your footer.php file, simply use the following code:
<ul>
<? query_posts('showposts=10');?>
<?php while (have_posts()) : the_post(); ?>
<li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>
Simply change ‘showposts=10′ to whatever number you want to display more or less than 10 posts.
Display Most Popular Blog Posts
“Most popular” posts in this case is defined by the posts that have the most comments. In order to select these, the following pieces need to be used:
1. Add the following function to your function file:
function most_popular_posts($no_posts = 10, $before = '<li>', $after = '</li>', $show_pass_post = false, $duration='') {
global $wpdb;
$request = "SELECT ID, post_title, COUNT($wpdb->comments.comment_post_ID) AS 'comment_count' FROM $wpdb->posts, $wpdb->comments";
$request .= " WHERE comment_approved = '1' AND $wpdb->posts.ID=$wpdb->comments.comment_post_ID AND post_status = 'publish'";
if(!$show_pass_post) $request .= " AND post_password =''";
if($duration !="") { $request .= " AND DATE_SUB(CURDATE(),INTERVAL ".$duration." DAY) < post_date ";
}
$request .= " GROUP BY $wpdb->comments.comment_post_ID ORDER BY comment_count DESC LIMIT $no_posts";
$posts = $wpdb->get_results($request);
$output = '';
if ($posts) {
foreach ($posts as $post) {
$post_title = stripslashes($post->post_title);
$comment_count = $post->comment_count;
$permalink = get_permalink($post->ID);
$output .= $before . '» <a href="' . $permalink . '" title="' . $post_title.'">' . $post_title . '</a>' . $after;
}
} else {
$output .= $before . "None found" . $after;
}
echo $output;
In this function, I’ve set the number of posts to 10 – ‘$no_posts = 10‘. Just change this number to display more or less posts.
2. Insert the following in your footer.php
<ul>
<?php most_popular_posts(); ?>
</ul>
WordPress revision control
In an earlier post, I discussed the handy revision control plugin. Plugins are awesome and I truly have immense respect for plugin developers, but I do find them a bit tedious to maintain and sometimes a bit of an overkill.
I am constantly fiddling and updating over 30+ client sites at a time. If WordPress and plugin updates didn’t come out as frequently, I would be more than happy to load my sites with dozens of plugins, but I do find simpler non-plugin methods more handy.
I recently discovered that post revisions can be disabled or limited simply by adding the following bit of code to the wp_config.php file.
define('WP_POST_REVISIONS','5'); //Will limit revision to 5 revisions
define('WP_POST_REVISIONS','false'); //Disables revisions totally
Personally, I think that one line of code is better than a plugin.
Display your latest tweet(s) on your website
I recently worked with Magnify Digital on a website for a very interesting group of people in the Fraser Valley. These folks are concerned about Air Quality and thus have come up with a website and community to address these issues.
A big part of the website was the social media aspect. Thus facebook, twitter and youtube feature prominently on the website. There are plenty of WordPress plugins that allow you to display your latest tweets, but I wanted a simpler solution. The following piece of code proved to be just what I needed:
<?php
$username = "yourTwitterUsername";
$limit = "1"; // Number of tweets to pull in.
/* These prefixes and suffixes will display before and after the entire block of tweets. */
$prefix = ""; // Prefix - some text you want displayed before all your tweets.
$suffix = ""; // Suffix - some text you want displayed after all your tweets.
$tweetprefix = "<p>"; // Prefix - some text you want displayed before each tweet.
$tweetsuffix = "</p>"; // Suffix - some text you want displayed after each tweet.
$feed = "http://search.twitter.com/search.atom?q=from:" . $username . "&rpp=" . $limit;
function parse_feed($feed, $prefix, $tweetprefix, $tweetsuffix, $suffix) {
$feed = str_replace("<", "<", $feed);
$feed = str_replace(">", ">", $feed);
$feed = str_replace(""", "\"", $feed);
$feed = str_replace("'", "'", $feed);
$clean = explode("<content type=\"html\">", $feed);
$amount = count($clean) - 1;
echo $prefix;
for ($i = 1; $i <= $amount; $i++) {
$cleaner = explode("</content>", $clean[$i]);
echo $tweetprefix;
echo $cleaner[0];
echo $tweetsuffix;
}
echo $suffix;
}
$twitterFeed = file_get_contents($feed);
parse_feed($twitterFeed, $prefix, $tweetprefix, $tweetsuffix, $suffix);
?>
Basic blog theme updated
A few years ago, I was asked to teach how to use WordPress to students enrolled in the Electronic Media Design Program at Langara. The goal was to provide the students with enough knowledge and understanding of WordPress so that they can create an online portfolio.
Once the curriculum in place, my first decision to make was what theme to start with. At the time, WordPress version 2.6 was around and I didn’t particularly want to use the Kubrick theme. I always disliked it and found it too messy. I had built quite a few sites with a now defunct theme, and decided that if I removed all of the unnecessary code and style, this theme would be a great starting point for my class and all of my projects. Thus was born the Basic Blog theme.
This theme has all the functionality of a typical WordPress theme but very little styling. It’s a bare bones theme or a framework for you to work from and has the following following features:
- Two column website measuring 960px wide.
- Widget enabled.
- Sidebar can be positioned on the right or left simply by switching the float direction in the CSS.
- Can be used to create a website with blog and static pages or just static pages if no blog is required.
- Requires no plugin.
Having gone through 3 intakes of students, I’ve had many opportunities to refine the theme. With the release of WordPress 3.0, I’ve now had to make further updates and the theme now also includes the following:
- Custom post thumbnails
- Custom menus
- Easy jQuery integration
- Excess information from header file is removed
Here’s a copy of the Basic Theme for you to download and play with. If you have any suggestions, comments on how it could be improved, please let me know.
I would also like to say thanks to Tzaddi and Catherine for providing their input on this theme and of course to all my EMD students who have thought me so much.
