Adding posts from one category to a static front page
-
Actually, this code is to add the posts from two different categories into two different columns. If you would just like one category, delete the second half of the code after the end of the first loop.
After a lot of searching trying to find an answer in layman’s terms on how to have two columns featuring the posts of two different categories on my static front page, I have finally worked out a code that has worked for me.
This is not the Main Index Template – this is a static front page. This code has been placed directly in the static page file, not in a template.
Just to clarify – my static front page is called index.php and it sits just outside of the blog folder. It is separate to wordpress.
The code –
<div id="column_01"> <?php if(is_home()) {query_posts('cat=73&showposts=3'); } ?> <?php while (have_posts()) : the_post (); ?> <a href="<?php the_permalink() ?>"> <?php the_title(); ?></a> <p> <?php the_excerpt_rss(); ?> <?php endwhile;?> </div> <div id="column_wrap"> <div id="column_02"> <?php query_posts('cat=52&showposts=3'); ?> <?php while (have_posts()) : the_post (); ?> <a href="<?php the_permalink() ?>"> <?php the_title(); ?></a> <p> <?php the_excerpt_rss(); ?> <?php endwhile;?>To explain the code –
<div id="column_01">This is to call information from the stylesheet. Please see further stylesheet details below.
<?php if(is_home()) {query_posts('cat=27&showposts=3'); } ?>if(is_home()) is needed to make the posts show up on a static front page. In the query_posts section, you note the category number of the category you would like to show, i.e. cat=27 and you put the number of posts you would like to show after ‘showposts’.
<?php while (have_posts()) : the_post (); ?>This is the start of the loop.
<a href="<?php the_permalink() ?>"> <?php the_title(); ?></a> <p>This turns the title of the post into a link to the post. The <p> is to just put a space between the title and the excerpt of the post.
<?php the_excerpt_rss(); ?>This grabs an excerpt from the post. Now, you have a choice here. You can either just let the programming select an excerpt or you can put your own excerpt in the ‘excerpt’ box on your post edit page. I chose the latter. If you choose the latter, you will need to make adjustments to make everything line up over the two columns (that’s if you want it to line up 🙂
Alternatively, if you would like to put the entire contents of each post on your page instead of an excerpt, replace ‘the_excerpt_rss’ with ‘the_content’.
In this case, if you didn’t require a link to your post, you could replace ‘“><?php the_title(); ?>‘ with just ‘<? php the_title(); ?>
<?php endwhile;?>This is the end of the loop.
</div> <div id="column_wrap"> <div id="column_02">Stylesheet reference info.
The remaining code is a replica of the above code, the only exception being that you don’t need the ‘if(is_home())’ info again plus you need to change the category number to whatever other category you wish to use.
Regarding the stylesheet info – add this code to your style sheet where you can make adjustments to suit. The ‘div#column_wrap’ code may take a little getting use to. You can always add something like border: thin solid blue; to each parameter to help guide you while making any changes –
div#column_01 { float: left; text-align: justify; clear: none; width: 45%; } div#column_wrap { float: right; clear: none; width: 50%; } div#column_02 { float: right; text-align: justify; clear: none; width: 95%; }From what I can gather, the column_01 & column_wrap percentages should equal the column_02 percentages. You may need to adjust these for your own requirements.
I hope this code works for you as well as it has for me. You can see it in action on my website http://www.travelcaper.com The sections are for ‘Travel Must Haves’ and ‘Attractions’.
The topic ‘Adding posts from one category to a static front page’ is closed to new replies.