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 24 2010

14:19

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]

[/php]

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:

[php]

Our medium thumbnail

Now do the same with the second loop.

[php]

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
[html]

……


[/html]

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:

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

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:

[php]

[/html]

That means we take the standard H2 formatting and add some custom styling.
[css]
#content .entry-title {
color: #000;
font-size: 21px;
font-weight: bold;
line-height: 1.3em;
margin-bottom: 0;
}
[/css]

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]
“alignleft”)); ?>
[/php]

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)

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

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]
“alignleft”)); ?>
[/php]

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.

[php]

…the rest of the loop….



[/php]

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:

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

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(); ?>

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

“alignleft”)); ?>

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

?>

“alignleft”)); ?>

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

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

Questions, comments etc always welcome

September 21 2010

12:47

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

This is part one of a short series outlining how to tweak a wordpress template to get some magazine style functionality.

I’m in the process of updating installations of wordpress for our students to use. In one sense it’s a stop gap measure as we are in the process of commissioning a more “industrial strength” system for them. But even with a new system in place I think we will still leave some courses the option of going the wordpress route. The magazine students for example, love the flexibility (and low level of tech) that design templates offer. It doesn’t seem to have done them any harm in terms of nominations.

When it comes to design, finding a wordpress template you like is half the battle, there are thousands out there. As more people use wordpress to get publications online, magazine style templates have become a popular search and a big growth area for premium template developers.

It’s tempting to pay for a template you like – nothing wrong with that. But it’s not as complicated as you think to get something up and running, out of the box, with very little tinkering. Especially if you build on existing templates. So I thought it would be useful to look at how easy it would be modify the standard Twentyten theme in to something with some magazine functionality.

Hacking around like this is how I learnt a lot of stuff about wordpress and it’s also a way to get your feet wet with a programming language. In this case PHP

To play along with this you’ll need:

  • Your own installation of the latest version of the wordpress.org software (as I write this it’s 3.0.1). Sorry wordpress.com won’t do.If you have webspace and your thinking of adding wordpress you could do worse than check out the wordpress codex entry on installing wordpress. Some hosts will offer automated installation of wordpress – very useful.
  • A text editor. Even word will do.

That’s it.

The design

The adapted Twenty Ten Theme

This is a screenshot of what we are going to end up with. It has a front page that has a featured post at the top and sections underneath for each category. You’ll also notice that I’ve tinkered around with the header to remove the big image. OK, it’s not going to win any design awards but this is more about exploring the concepts.

Normally you’d plan this kind of thing on paper first. You’d also work on the code in a development environment. An installation of WordPress that only runs on your machine, not the web.

If you’re feeling really brave you can set one up. Here are few resources

I’m going to assume that we dive straight in and edit the template live! I know, bad, bad, bad. All I’m going to say is do so at your own risk.

A word on programming and PHP

WordPress is written in a programming language called PHP.  This isn’t a programming tutorial (I’m not going to explain the basics of programming), but there are a couple of important things to know.

Spotting PHP

You may already be familiar with HTML. You can spot it in the raw code for a webpage because it is contained in pointy – brackets.

[html]

Anything here will appear as a heading two

[/html]

In a similar way, you can spot PHP in the raw code for a webpage because it is always between  <?php …. ?>. Here’s an example:

[php]

[/php]

But if you look at the source for this webpage in your browser you will only see HTML. Why don’t we see the PHP?

PHP is a server side language. That means the webserver looks at the page and processes any PHP it finds before it sends you the page. When we use PHP as part of wordpress themes we are using it to generate HTML.

Functions

When programmers write code they will always look for ways to avoid repetitive jobs. Rather than write the same code every time, they write a function. This is a set of instructions that can be called when needed.  The example of PHP above is a function:

[php]

[/php]

Whenever we want to show the title of post we call the function the_title() and the server runs the code needed to get all the right information. The semi-colon is also important. Here’s another example:

[php]

[/php]

This time it’s a function to show a thumbnail for a post. But there is also some content in the brackets. This is a parameter or extra information that the function might need. When the server runs the function to get the post thumbnail it tells the function it wants the thumbnail sized thumbnail. I know, sounds like repetition. We could also say:

[php]

[/php]

That says ‘get the thumbnail but make it medium sized’. In case you were interested, the thumbnail and medium sizes are defined in the media settings of your blog. But more on that later.

There are hundreds of these functions in wordpress. Some are specific to templates, like the examples above, but others do the heavy lifting of making the blog work. We’ll be scratching the surface of the template functions here but I thought it was worth a little intro.

So we are going to be looking at a little PHP to call some functions to help us modify the TwentyTen template. Hopefully, now, that might statement might make a bit more sense.

How wordpress themes work.

You can get a really good overview of the way themes work from the wordpress codex and plenty of other websites around. A google search for wordpress theme tutorials should give you plenty of options. But let’s break it down in to a few simple ideas.

A wordpress theme is split in to parts:

  • the content you want in a structured form
  • instructions on the way you want it to look.

This information is held in a number of different files.  These are stored in a folder, one for each theme, in the WP_content/themes folder of your wordpress installation. The more complex the theme, the more files there tend to be.

In a basic theme, for example, you will have a file called single.php. That’s the content and structure part. This is a mixture of HTML and PHP. But the way it looks, the colour and style of text, position on the page etc is controlled by a file called style.css. This is a cascading style sheet file.

The Twentyten theme we are going to edit, has 18 content and structure files and four style sheet files. We wont be using all of these for this tutorial. We are only interested in two.

  • Main Index Template (index.php)
  • Stylesheet (style.css)

Accessing template files

There are several ways we can get at these files:

Any of those will do. But I’m going to work through on the assumption you are using the built in editor.

First thing to do is check you have the TwentyTen theme activated by going to Appearance >Themes. It should show Twenty Ten as the current theme. Then click through to the editor panel (Appearance > Editor).

The Theme editor

You’ll see a list of the 18 template files down the right-hand side and an editor window. By default this displays the Visual Editor Stylesheet (editor-style.css). All  you need to do is find and click Main Index Template or Stylesheet on the right to load up the files we will be working with.

Permission to edit.

When you look at the bottom of the editor window you may see a warning: – You need to make this file writeable before you can save your changes

You need to set the permissions on the theme folder!

This could be the biggest stumbling block of the process. But if you are serious about having a go at theme development, even tweaking like this, it’s worth getting your head round.

To remove the error message you need to set the permissions for the Twenty Ten folder to be 666.

Does that make no sense? You could try:

If you set the permissions correctly, the message should be replaced by a big Update File button.

You're ready to start editing!

Some final preparation

From this point I’m going to assume that you have a working installation of wordpress up and running. But before we experiment with the theme we need to have some content to work with. So if your blog doesn’t have posts yet you need to add a few posts to work with. You could do this manually using the lipsum.com, a lorem-ipsum generator and some liberal cut and paste for content. There are also a number of random content generator plugins available. For this exercise I used demo data creator.

We will also need to create some categories and assign the posts across the categories. I’ve used the following for this demo:

  • News
  • Sport
  • Featured Story

Once you have done that we are ready for Part 2 tomorrow, where we will start to edit the front page to get that magazine look.

As always, feedback and suggestions always welcome

Older posts are this way If this message doesn't go away, click anywhere on the page to continue loading posts.
Could not load more posts
Maybe Soup is currently being updated? I'll try again automatically in a few seconds...
Just a second, loading more posts...
You've reached the end.

Don't be the product, buy the product!

Schweinderl