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

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

In part three of this short series we looked at adding a second loop to our edited index page to get round the problem of our Featured Post being repeated on the front page. In this final part I’ll look at adding a thumbnail and styling up the page.

Image thumbnails

Over the years, theme designers magazine themes have come up with many weird and wonderful ways of getting thumbnail images on front pages. But it’s only recently that WordPress developers added solid support.

One of the things I wanted to do with this series is to avoid too much tweaking of files. So I’m going to be relying on some of the core features for wordpress to get thumbnails on the page rather than fancy tricks. So beefore we get back in to editing the template code to display thumbnails there are few things we need to check.

Media settings

When you add an image to a blog post you are given the option to add it as a thumbnail, medium, large or original size. We are going to be using the standard function to to get a thumbnail (you may remember it from part 1) and it uses the same shorthand to get an image

<?php the_post_thumbnail('thumbnail'); ?>

The sizes for each these are set in the Media section of the settings tab.

The image size options

So our edited front-page is going to be based on these sizes. If you want any other sizes for your page you should set them here first. The downside of working this way is that this will impact on the sizes of images placed in your blog posts -that’s the trade off of keeping things simple.

Adding a featured image.

Version 2.9 of WordPress included a new post thumbnail option which allowed you to define an image to display “as the representative image for a Post or Page. The display of this images is up to the theme. This is especially useful for “magazine-style” themes where each post has an image.” The feature was renamed “featured image” in wordpress 3.0 – I’m guessing to avoid confusion with thumbnails. Whatever it’s called it’s ideal for our front page.

Adding a featured image

When you write a post you should see a panel called Featured Image. Clicking the Add featured image link opens up a standard image browser. You simply find the image you want to use and click the Use as Featured Image link and you’re done.

So before we go on, add a featured image to the post in your Featured Story category.

Adding the thumbnail to the template

Now that we have set up the Featured image we can edit our template file and get an image on our frontpage.

Open up the Main Index Template file and add the following and edit the first loop so it looks like this:

<h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf(esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php 	the_title(); ?></a></h2>

<?php the_post_thumbnail('medium'); ?>

<?php the_excerpt();?>

We’ve added the post_thumbnail function and called the medium sized image (300×300). The result should be something like:

Our medium thumbnail

Now do the same with the second loop.

<h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf(esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php 	the_title(); ?></a></h2>

<?php the_post_thumbnail('thumbnail'); ?>

<?php the_excerpt(); ?>

We’ve called the same function but specified the thumbnail image. The results should be something like this (don’t forget you need to add a featured image to your other posts)

Thumbnails added to each loop

And that’s it.

Adding some style.

Technically we are done. All the elements we want are on the page. But it’s not looking as good as it could be. We need to add some styling information and make some amendments to the stylesheet file.

I’m not going to go in to a big write up of CSS here (try the excellent W3Schools for a basic intro) but if you’re interested in tweaking wordpress templates it’s one of those areas you’ll be spending a lot of time with.

For now, its enough that when dealing with stylesheets, we need to keep our eye open for two things; divs and classes.

Divs

If you look at the Main Index template file, you’ll see the following lines

<div id="container">
			<div id="content" role="main">
......
		</div><!-- #content -->
		</div><!-- #container -->

The div tag is an html element that doesn’t actually display anything by default, it defines a section of the page. When it comes to look and feel, the key part is the id . This ‘connects’ the div to display instructions defined in the stylesheet. The style definition for container is:

#container {
	float: left;
	margin: 0 -240px 0 0;
	width: 100%;
}

Anything that sits between the div tags will be effected by this definition.

Classes

One restriction of ID’s is that you can only use them once on a page. So if you have a lot of elements on a page that you want to style you have to use a class. Remember the html we used for our post title:

<h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf( 		esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>

The first part of that is:

<h2 class="entry-title">

That means we take the standard H2 formatting and add some custom styling.

#content .entry-title {
	color: #000;
	font-size: 21px;
	font-weight: bold;
	line-height: 1.3em;
	margin-bottom: 0;
}

This says, any time the class entry-title (denoted by the full-stop) is referenced inside the content div (denoted by the # symbol) apply the following styling.

Image Alignment

The first thing to sort out is the alignment of the images. I’m going to cheat a little here and pick up the standard style call for images.

Change the post_thumbnail function call in the first loop to the following :

<?php the_post_thumbnail('medium',array("class" => "alignleft")); ?>

The post thumbnail function allows you to stack extra information in parameters that can be ‘added’ to the code as it’s generated. We have stuffed a reference to a style called alignleft. If you call up the Stylesheet file in the theme editor you can find the definiton of that style (you may have to search for while)

#content .alignleft,
#content img.alignleft {
	display: inline;
	float: left;
	margin-right: 24px;
	margin-top: 4px;
}

This is very similar to our post title example above but this time there is also a reference to the image tag (img).

To finish up we can add the same class to the thumbnail call in the second loop:
Change the post_thumbnail function call in the first loop to the following :

<?php the_post_thumbnail('thumbnail',array("class" => "alignleft")); ?>

Boxing in the featured story

To make my featured story stand out I’m going to wrap it in a grey box. To start with I’m going to use a DIV to define that extent of the box.

<div id="content" role="main">
			<div id="FeaturedStory">
...the rest of the loop....

<?php the_excerpt();?>
<?php endwhile; ?>
			
			</div><!-- #FeaturedStory -->

I’ve added a new DIV tag with an id called FeaturedStory and closed the div after the end of the loop.

If you update the file and looked at the page you should see nothing new. Remember DIV tags don’t show up till you style them.

Open the Stylesheet file in the editor window and scroll all the way down to the bottom. Add the following:

#FeaturedStory {
	background: #f7f7f7;
	color: #222;
	margin-bottom: 18px;
	padding: 1.5em;
	height: 350px;
}

This does the following:

  • Changes the background colour to grey
  • Changes the text colour to a dark grey
  • Pads the bottom of the box with 18 pixels of space
  • Pads the all the way round with 1.5 em of space
  • Sets the height of the box to 350pixels

Save the file and look at the results. You’ll see a box around the featured content.

Conclusion

That’s pretty much it. We’ve pulled in a featured post and thumbnail to go with it. Then we added a second loop to pull in the rest of the posts without duplicating our featured post on the page and added a thumbnail to them. Then we styled the results to align the thumbnail and wrap the featured post in a box to make it stand out.

Along the way we’ve touched on PHP, functions, variables and stylesheets. All of which are play a big part in theme development. But we have done it all with the minimum of alteration to the core theme files.

Some issues

This method is not without its issues. Editing the raw files like this is risky if you forget to back things up. There is also the risk that if the theme is updates by wordpress (as it is from time to time) then your customization will be deleted. But the exercise has been more about some of the basic concepts than a robust solution.

So I hope you found it useful and it made sense. Here’s the finished Main index template file:

<?php
/**
 * The main template file.
 *
 * This is the most generic template file in a WordPress theme
 * and one of the two required files for a theme (the other being style.css).
 * It is used to display a page when nothing more specific matches a query. 
 * E.g., it puts together the home page when no home.php file exists.
 * Learn more: http://codex.wordpress.org/Template_Hierarchy
 *
 * @package WordPress
 * @subpackage Twenty_Ten
 * @since Twenty Ten 1.0
 */

get_header(); ?>

		<div id="container">
			<div id="content" role="main">
			<div id="FeaturedStory">

<?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
			 */
?>

<h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf(esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php 	the_title(); ?></a></h2>
<?php the_post_thumbnail('medium',array("class" => "alignleft")); ?>
<?php the_excerpt();?>
<?php endwhile; ?>
			
			</div><!-- #FeaturedStory -->

 <?php 
 
			 /* This is the second loop that replaces the standard loop
			  * It uses the standard loop function calls
			 */
 
 
 if (have_posts()) : while (have_posts()) : the_post();


if( $post->ID == $do_not_duplicate ) continue; 
update_post_caches($posts); 

 			/* This line gets the post ID and checks it agains our duplicate variable
 			 * If it matches it does nothing. If it's different we display the content
			 */

?>

<h2 class="entry-title"><a href="<?php the_permalink(); ?>" title="<?php printf(esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php 	the_title(); ?></a></h2>
<?php the_post_thumbnail('thumbnail',array("class" => "alignleft")); ?>
<?php the_excerpt(); ?>
<?php endwhile; endif; ?>


			<?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' );
			?>
			</div><!-- #content -->
		</div><!-- #container -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>

Don’t forget, you need to update the Stylesheet file as well.

Questions, comments etc always welcome

8 thoughts on “How to create a wordpress magazine theme using Twenty Ten – Part 4”

  1. Thanks for posting this. I worked through all 4 lessons and learned a lot. I have been working with css and php for many, many years, but am relatively new to wordpress. This was very helpful. I even learned something new about css. However I did have trouble getting the loop in lesson 1 to work. I ended using the code posted here
    http://wordpress.org/support/topic/limit-posts-on-homepage

    Now I am off to see if I can take what I learned and have the second loop show one post from each category. That might make a great lesson 5.

  2. Thanks Tamara

    I was like you. I knew a lot about HTML, CSS etc and started to bash around in wordpress. It’s the best way to learn I think.

    I’ll check that loop. I do have a follow-up post planned so I’ll think about including that.

  3. Hi Andy,

    Brilliant set of posts, I did alter this a little further by using this post in the wp forums below and wanted to pass my edits on:
    http://wordpress.org/support/topic/twentyten-alternating-post-colour

    So that the following posts alternated their images/text from left to right.

    Your code:

    if( $post->ID == $do_not_duplicate ) continue;
    update_post_caches($posts);

    /* This line gets the post ID and checks it agains our duplicate variable
    * If it matches it does nothing. If it's different we display the content
    */
    ?>

    <a href="" title="" rel="bookmark">

    "alignleft")); ?>

    I changed to this:


    if( $post->ID == $do_not_duplicate ) continue;
    update_post_caches($posts);

    /* This line gets the post ID and checks it agains our duplicate variable
    * If it matches it does nothing. If it's different we display the content
    */

    $odd_or_even = ('odd'==$odd_or_even) ? 'even' : 'odd';
    ?>

    <a href="" title="" rel="bookmark">

    $odd_or_even)); ?>

    Added this to the style.css file:

    #content .odd,
    #content img.odd{
    display: inline;
    float: left;
    margin-right: 24px;
    margin-top: 4px;
    }
    #content .even,
    #content img.even{
    display: inline;
    float: right;
    margin-right: 24px;
    margin-top: 4px;
    }

    Also the excerpts were really short, so in theme functions ‘functions.php, alter the number of words returned by altering this codes return number:


    function twentyten_excerpt_length( $length ) {
    return 70;
    }

    I think it looks pretty neat http://lastdropofink.co.uk/

    Matthew

    1. Hi Matthew

      Thanks for the comment, I’m glad the posts made sense. Changing the function to lengthen the excerpt works well but if you update the template, that’s one of the things that can be lost.

      Some people prefer to do it in loop by using the the_content_RSS() function

      This has a variable called $cut which you can use to specify the number of words.

  4. Hi,

    I followed the instructions but my featured image shows up in full size although I set the size to small (120 x 120 px in my case). What am I doing wrong?

  5. Hi,

    great article Andy, i have one question.

    How can i get title next to rest of posts (not featured post)?
    I don’t want my title appears above thumbnail picture.

    Thnx

Leave a Reply