Developing A Wordpress Magazine Theme – Part One
Posted: April 16th, 2009 | Author: Bryan | Filed under: Tutorial | 2 Comments »This tutorial is going to teach you how to build some of the functionalities that are commonly found in magazine style themes. I am going to stick with pretty generic concepts as the pertain to Wordpress only. And mind you, I will not be teaching you how to build an entire theme from scratch, we’ll just focus on building the homepage with the classic magazine theme functionality.
Recent Posts Boxes Image Preview
A common feature in a Magazine theme is those 1/3 columns with thumbnails of an image in the post. The CSS is actually pretty easy, we’ll just divide the width of the total area into three sections. Let’s make it easy and say its 960px wide, so we have 3 boxes with a width of 320px each. Let’s also make them square, and they need to float to the left to stack horizontally. This is the ideal HTML and CSS:
-
<div class="featuredbox">
-
<img src="/image1.jpg" alt="this is a thumbnail" width="128" height="128" />
-
<h3><a href="#">Heading 1</a></h3>
-
<p>Post excerpt here orem blah blah….</p>
-
<a href="#">Read more…</a></div>
-
<div class="featuredbox">
-
<img src="/image2.jpg" alt="this is a thumbnail" width="128" height="128" />
-
<h3><a href="#">Heading 2</a></h3>
-
<p>Post excerpt here orem blah blah….</p>
-
<a href="#">Read more…</a></div>
-
<div class="featuredbox">
-
<img src="/image3.jpg" alt="this is a thumbnail" width="128" height="128" />
-
<h3><a href="#">Heading 3</a></h3>
-
<p>Post excerpt here orem blah blah….</p>
-
<a href="#">Read more…</a></div>
-
.featured {
-
width:320px;
-
height:320px;
-
float:left;
-
}
-
.featured img {
-
float:left;
-
}
This should pretty much prep us for what we want to do, let’s make the WP_Query to grab this data. There is obviously no use in repeating ourselves like we did with the HTML with PHP at hand, in fact, lets just use a standard loop, like this:
-
<?php $featured = new WP_Query("showposts=3");
-
while($featured->have_posts()) : $featured->the_post();?>
-
<div class="featuredbox">
-
<img src="/imagenum.jpg" alt="this is a thumbnail" width="128" height="128" />
-
<h3><a href="#">Heading Num</a></h3>
-
<p>Post excerpt here orem blah blah….</p>
-
<a href="#">Read more…</a>
-
</div>
-
<?php endwhile; ?>
Well that’s close, but let’s get those standard Wordpress tags in there, otherwise they’ll be uselessly looping on a worthless box, with no appropriate content gracing our Wordpress magazine theme. Let’s try:
-
<?php $featured = new WP_Query("showposts=3");
-
while($featured->have_posts()) : $featured->the_post();?>
-
<div class="featuredbox">
-
<img src="/imagenum.jpg" alt="this is a thumbnail" width="128" height="128" />
-
<h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
-
<?php the_excerpt(); ?>
-
<a href="<?php the_permalink() ?>">Read more…</a>
-
</div>
-
<?php endwhile; ?>
That’s much closer, however, we’re missing our thumbnail. Let’s make this simple, we can get fancier but this technique works well for new blogs. If you’re moving your blog, this type of Wordpress magazine theme might not be right. You’ll want to go into your Wordpress Settings->Media section and set the thumbnail dimensions to 128×128 (or whatever you set it to) and check the crop checkbox.
-
<?php
-
-
function match($regex, $str, $i = 0) {
-
if(preg_match($regex, $str, $match) == 1)
-
return $match[$i];
-
else
-
return false;
-
}
-
-
$featured = new WP_Query("showposts=3");
-
while($featured->have_posts()) : $featured->the_post();
-
-
ob_start();
-
the_content();
-
$imgBeg = strpos(ob_get_contents(), '<img');
-
$imgStuff = substr(ob_get_contents(), $imgBeg);
-
ob_end_clean();
-
$imgEnd = strpos($imgStuff, '>');
-
$postImg = substr($imgStuff, 0, $imgEnd+1);
-
$postImg = match('/src="(.*?)" /msi', $postImg, 1);
-
$postImg = preg_replace('/.jpg/','-128×128.jpg', ob_get_contents());
-
$postImg = preg_replace('/.png/','-128×128.png', ob_get_contents());
-
$postImg = preg_replace('/.gif/','-128×128.gif', ob_get_contents());
-
-
?>
-
<div class="featuredbox">
-
<img src="<?php echo $postImg; ?>" alt="<?php the_title(); ?>" width="128" height="128" />
-
<h3><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h3>
-
<?php the_excerpt(); ?>
-
<a href="<?php the_permalink() ?>">Read more…</a>
-
</div>
-
<?php endwhile; ?>
This code grabs the first image URL, adds the -128×128.jpg to the end of the file name, calling the file created by Wordpress when you upload the image originally.
You can explore some more options for the WP_Query by browsing the official Wordpress site. You can select only certain categories, and go off of other more choosy criteria. Whatever you do, tune in for the next edition.
Congratulations on getting your design featured on Smashing Magazine.
http://www.smashingmagazine.com/2009/05/18/100-amazing-free-wordpress-themes-for-2009/
I am also based in Columbia, Missouri and your title, MidMoDesign.com caught my eye.
Great tutorial!
I was looking for a user friendly way to create thumbs for featured posts and this feels spot on. Darren Hoyt’s Timthumb-script is also excellent: http://www.darrenhoyt.com/2008/04/02/timthumb-php-script-released/
But this feels smarter since you use the built in code for Wordpress!