PureNews

PureNews is an amazingly sleek and powerful news theme with unlimited color variations.

View full feature list Check out the live demo Buy this theme today

Create a Magazine Style Theme II

Posted by on 21st Sep 2008 WordPress Coding & Design 5 comments

Last week I gave an example of how to create a magazine style theme. This method allowed you to have a main post followed by a list of titles. This week I’ll explain how to separate these on the page so that you can have a far greater control over your layout.

The Latest Post

Rather than using the WordPress loop to display the latest post in full, we can use the get_posts() function to grab the latest post and display it in full. To do this we can use the following code:

[sourcecode language="php"]$myposts = get_posts(‘numberposts=1&orderby=post_date&orderby=DESC’);
foreach($myposts as $post) :
setup_postdata($post); ?>

Post tags that you usually use within the loop.

The Remaining Posts

Then we need to pull out the remaining posts in a list using the WordPress loop, however we don’t want the first post as we’ve already displayed this. To prevent the first post being displayed we use the query_posts() function, which allows us to add in additional parameters and manipulate the output in the loop. The code we use for this is as follows:

query_posts('showposts=5&offset=1');

This will tell the loop to retrieve 5 posts after the first post. We then use the loop to display the posts in a list with the above code just before the loop starts:

[sourcecode language="php"]

    < ?php
    query_posts('showposts=5&offset=1');
    if (have_posts()) :
    while(have_posts()) :
    the_post();
    ?>

A PHP Developer using WordPress to power both blogging and commercial CMS sites. I've written and released a couple of plugins for WordPress and am currently writing plugins for use on commercial websites.

5 comments - Leave a reply
  • Posted by Ajith Edassery on 21st Sep 2008

    Hi,

    Thanks for this tip… I had another related query. See, I want to show the in archives the actual number of posts as per the wordpress reading settings (If I say, 5 there, I want 5 posts per page in the archives). However, on the homepage I want to show only 3. The problem for me is that the 'while (have_posts())…loop still fetches 5. If I put a break logic after three, when I browse the previous pages, I loose 2 articles…

    Pls help,

    Ajith

  • Posted by marianmystery on 22nd Sep 2008

    I hope you can post more tips and techniques like these. They really interest people like me who are new to all these blogging business.

  • Posted by Syed Balkhi on 22nd Sep 2008

    I have written an ebook about magazine style themes for wordpress which you can find on my blog. Good post Sarah :)

  • Posted by Sarah on 22nd Sep 2008

    Ajith – Using the query_posts() function theoretically should work but it can have a tendency to break the next/previous navigation links. However it's worth trying first

    <code>query_posts($query_string.'&posts_per_page=3');</code>

    Put the above before your loop. If that doesn't work then post up in the Blogging Tips forums and we can take it further in there, as you may need to use a plugin instead.

    Marian and Syed, thanks for the comments.