Create a page to display a random post
October 22, 2008
Your blog have probably a lot of posts that your readers haven’t read yet. Why not creating a page and display a random post on it? Here’s an easy way to do it. The first thing to do is, of course, to create a page template. Once done, paste the following code in your new page template: <?php query_posts(array('orderby' => 'rand', 'showposts' => 1)); if (have_posts()) : while (have_posts()) : the_post(); ?> // WordPress loop, your random post will appear here endwhile; endif; ?> That’s... [Read the full story]
Add a edit link on each post
October 21, 2008
Sometimes, you read one of your blog post and you see a typo or an error. Sure, you got to correct it. But who said you must open your WordPress dashboard, go to “Manage” and then edit the post? In this recipe, I’m going to show you how to add a button to allow the admin to directly edit the post. To achieve this recipe, we need the current_user_can() WordPress function. This function checks the given parameter, which is the level of the current user, and returns true if the level of the current user is superior or... [Read the full story]
Insert Adsense after the first post
October 21, 2008
This seems to be a common WordPress question, but implementing it on your theme isn’t hard at all. The only thing we need is a simple php variable (here named $count) which will count how many posts are listed. If we just listed the first post, we’ll display the Adsense code. Here’s the code. Paste it on your index.php file, instead of your current WP loop. <?php if (have_posts()) : ?> <?php $count = 0; ?> <?php while (have_posts()) : the_post(); ?> <?php $count++; ?> <?php if ($count <... [Read the full story]
Display the total number of posts on your WordPress
October 20, 2008
Wouldn’t it be nice to be able to display the total number of posts published on your WordPress blog? WordPress don’t have a function to do that by default, but happilly this hack is here to help. Here’s the code: We’re using the $wpdb object to make a custom query to WordPress database: $numposts = $wpdb->get_var("SELECT COUNT(*) FROM $wpdb->posts WHERE post_status = 'publish'"); if (0 < $numposts) $numposts = number_format($numposts); Right now, the $numposts variable contains the total number of posts.... [Read the full story]
Display one full post and three excerpts
October 20, 2008
To achieve this, we’ll use a simple WordPress loop. The only thing we have a to add is a variable (here named $count) which will count how many posts are listed. Here’s the code. Paste it instead your current WP loop and customize it a bit to make it fit your needs. <?php if (have_posts()) : ?> <?php $count = 0; ?> <?php while (have_posts()) : the_post(); ?> <?php $count++; ?> <?php if ($count < 2) : ?> <?php the_content() ?> <?php else : ?> <?php... [Read the full story]




















