Tumblelog by Soup.io
Newer posts are loading.
You are at the newest post.
Click here to check if anything new just came in.

September 23 2010

12:04

How to create a wordpress magazine theme using Twenty Ten – Part 3

This is part three of a short series outlining how to tweak a wordpress template to get some magazine style functionality. Part one and Part Two are available here.

In the previous part of this tutorial I looked at how we could display some custom content on our front page by adding another loop to the file. This gave us chance to experiment with template tags to customize what we say. But I also pointed out that we had a problem.

Our ‘new’ loop pulls out the latest story in our featured story category and puts the title and excerpt at the top of the page. But the original wordpress loop is still there. It pumps out the last 10 posts published on the site and theres a good chance that our featured post is one of them. That means we get the same story twice on the homepage.

Turning off the original loop

If you remember, the orginal loop is called using a get_template_part function in the Main Index Template file.

[php]
/* Run the loop to output the posts.
* If you want to overload this in a child theme then include a file
* called loop-index.php and that will be used instead.
*/
get_template_part( 'loop', 'index' );
?>
[/php]

We can turn the loop off by commenting it out.
[php]
/* Run the loop to output the posts.
* If you want to overload this in a child theme then include a file
* called loop-index.php and that will be used instead.
*/
// get_template_part( 'loop', 'index' );
?>
[/php]

Notice how the function call has turned green like the comments. By adding a // in front of the function we turn it in to a comment which the server will ignore. If you update your file and look at the front page, you’ll see you only have the featured post.

Commenting out is a common trick when developing code. It allows you to try a few different things without deleting anything. Just don’t do to much, things can get confusing.

You may also have noticed that other comments in the file are marked between /*…*/ rather than a //. They are both valid. The /*…*/ is usually reserved for multi-line or blocks of comments rather than single lines. It also makes for a handy way to differentiate between comments (description of the code) and commenting out.

Adding another loop

Getting rid of the loop altogether is a bit of severe way to solve the repeat post option. What we need to do now is add a loop that allows us to get the posts back in a more controlled way. Add the following code after the new loop we added and before the original loop.

[php]
….

/* This is the new loop to display a featured story.
* It creates a variable and then loads all the posts that match the query.
*/

$my_query = new WP_Query('category_name=Featured Story&showposts=1');

/* Now it loops through the results and displays the content.
*/

while ($my_query->have_posts()) : $my_query->the_post();
$do_not_duplicate = $post->ID;

/* We load the Page ID in to a variable to check for duplicates later on
* Then it displays the title as a working link with formatting to
* match the Twenty Ten template.
* Then we display the excerpt.
* Then we finish the loop with the endwhile statement
*/
?>

wpmodder.com, a great site.

Don't be the product, buy the product!

Schweinderl