WordPress plugins, themes, tips and hacks

Archive for the ‘Code Snippets’ Category

Creating different Page templates

Monday, October 29th, 2007

To create a new Page template, add the following code to the top of the page:

<?php
/*
Template Name: Home
*/
?>

The Template Name can be changed, i.e. Home can be About Us, or anything else. When you create a Page in WordPress, on the right-hand side there is a drop down box for selecting a template. The name that you enter in the above code is what will appear there.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Display a selected number of posts from one category in the sidebar

Monday, October 29th, 2007

<?php$postlist = get_posts('category=6&numberposts=5');
foreach ($postlist as $post) :?><li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li><?php endforeach; ?>

Use this in the sidebar to display a defined number of posts from a specified category. Where it says “category=6″ replace the 6 with the relevant category ID number, and where it says “numberposts=5″ replace the 5 with the number of posts that you want to appear.

Update Nov. 7, 2007:

The above code snippet can also be used to display a page in the sidebar. This is is handy if you want to create an editable chunk of text in the sidebar without using widgets. So, for example, if you want to show an excerpt of your About page in your sidebar, use the following code:

<?php$postlist = get_posts('id=54');
foreach ($postlist as $post) :?><?php the_excerpt(); ?>
<a href="<?php the_permalink(); ?>">read more</a>

Where it says id=54, replace 54 with the id number of the page you want to display.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Code snippets on WordPressGarage

Monday, October 29th, 2007

I am going to start posting code snippets on WordPressGarage that are used in WordPress template files for different purposes. I will display the code snippet with an explanation of how it’s used, what it does, and why you would want to use it.

The goal of WordPressGarage is to be an online WordPress manual for me and my workers, and to give all of you the opportunity to benefit from this too. So while these may not be the prettiest or most fascinating posts, they serve my original goal of making it easy to find information for repeat use.

So here they come…

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Showing different sidebar on different pages

Wednesday, February 21st, 2007

To show a different sidebar on different pages, replace the standard call for the sidebar:

<?php get_sidebar(); ?>

with the following code:

<?php include ('sidebar2.php'); ?>

The same can be done to call different headers, i.e. instead of <?php get_header(); ?> use <?php include ('header2.php'); ?>For more information, see Different Sidebars Anyone? on the WordPress codex.

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]
Premium News Themes