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.
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:
$myposts = get_posts('numberposts=1&orderby=post_date&orderby=DESC');
foreach($myposts as $post) :
setup_postdata($post); ?>
<h2><a href="<?php the_permalink() ?>">< ?php the_title() ?></a></h2>
< ?php the_content(); ?>
<p>Posted In: < ?php the_category(', ') ?> | < ?php comments_number('No Comments', '1 Comment', '% Comments') ?></p>
< ?php endforeach; ?>
Put this in your markup where you want the post title, content and post meta displaying eg. within a div. You can use all of the standard Post tags that you usually use within the loop.
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:
<ul>
< ?php
query_posts('showposts=5&offset=1');
if (have_posts()) :
while(have_posts()) :
the_post();
?>
<li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to < ?php the_title_attribute(); ?>">< ?php the_title(); ?></a> < ?php comments_popup_link('', '(1)', '(%)'); ?></li>
< ?php endwhile ?>
< ?php endif; ?></ul>
This will then output a list of 5 recent posts, not including the most recent one.
There are many ideas of how you could have your layout, and the code in this post will give you a greater flexibility to control what goes where. You could have the most recent post on the left side of your page and then just have your 5 (or 10, or any number) posts in the sidebar, or after a piece of brief about/welcome content. The options are endless
Author comments are in a darker gray color for you to easily identify the posts author in the comments
[...] Create a Magazine Style Theme II [...]
Comments are closed since this post is older than 30 days. However, you can continue this discussion in our popular Blogging Forums
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
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.
I have written an ebook about magazine style themes for wordpress which you can find on my blog. Good post Sarah
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
query_posts($query_string.'&posts_per_page=3');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.