views:

105

answers:

2

I'm trying to add two loops to a theme on two separate pages: home and blog.

Blog is basically an index of the posts. It's what most Wordpress pages default to as a home page. To accomplish this I went to "reading settings" and set "front page displays" as 'static' with "front page" set to a Home page I set up in Wordpress pages and "posts page" set to a Blog page.

Now the problem is that when I add the loop to the Home page, it doesn't work, presumably because I have posts page set to a different page.

So how do I get the loop to work on the Home page as well as the blog page? Btw, the Home page loop is just post title + date + maybe excerpts. Do I need to completely rework the theme or is this is just not a possibility under Wordpress?

Oh and the loop I'm using is:

<?php if(have_posts()) : ?>
        <?php while(have_posts()) : the_post() ?>
A: 

It is possible that WordPress does not start a loop for you because you use a static page. But if this static page is defined in your theme (since you include the PHP code to display the loop, I assume it is), you can always start a new loop there yourself. Just call query_posts yourself, and your code should start working.

Jan Fabry
Hi, thanks for replying. I ended up using "new WP_query" since it was a nested a loop and using the default template of front-page.php.
Ben Moseley
+1  A: 

There are at least three wayst to run custom queries in WordPress.

Query_posts() can define the query string of your second loop. It is easy and very common to do. This code is a basic structure I copied from the codex page for query_posts():

//The Query
query_posts('posts_per_page=5');

//The Loop
if ( have_posts() ) : while ( have_posts() ) : the_post();
 ..
endwhile; else:
 ..
endif;

//Reset Query
wp_reset_query();

You can also use get_posts() which is similar.

<ul>
 <?php
 global $post;
 $myposts = get_posts('numberposts=5&offset=1&category=1');
 foreach($myposts as $post) :
   setup_postdata($post);
 ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
 <?php endforeach; ?>
 </ul> 

Both functions accept a number of arguments that are explained on the query_posts function reference page. The arguments shown above are only examples. The list of available args is long.

A third method available to you is to instantiate another instance of the WordPress Query object (WP's main query method). Query_posts and get_posts both run a second call to the database after WordPress runs the default wp_query. If you are super concerned about performance or reducing db hits, I suggest learning how you can interact with wp_query to modify the default query before it is run. The wp_query class provides a number of simple methods for you to modify the query.

Good Luck!

kevtrout
Thank you for response. I'm new to Wordpress and I have no idea where I can put that code though, because right now the index.php file is now linked to the "Blog" page through the reading settings. And correct me if I'm wrong, but you can't just plop the php code for a second loop in the middle of a static WP page?
Ben Moseley
There should be a file in your theme called 'page.php'. Unless you copy and rename that, and change the template name in the file where it says /*template name: xxxxxxx/*, it is the static page template. You put my code above in that file, or the one you create and use as the home page template. If you do create an alternate page template file, make sure to set it as the home page template by editing your page in the WP admin area, and select your file in the page-template-dropdown usually in the right column of the edit page screen.
kevtrout
Won't that make it show up on every static page though instead of just the home page?
Ben Moseley
If you have other WordPress pages that use the page.php template, make a copy of that file, and rename it. Also in the file, you'll see "/*Template Name: page template*/"near the top. "page template" may be something else. Change the name to a unique name. Then, edit the WordPress page you want to show the custom loop on in the admin area by changing the page template to the one you created. In the right column, you'll see a dropdown select box to choose the page template. You'll see your custom template listed.Select it and save. Now the second loop will only appear on that page
kevtrout
Okay, thank you. Wasn't even aware of templates. I ended up using front-page.php because it's actually the template that my home page defaults to given my readings settings.As for the loop, I used "new WP_query()" and it's now working perfectly. Thanks for pointing me in the right direction.
Ben Moseley