Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Quick Q about using a second wp loop by category on page WordPress

  • SOLVED

Hi Everyone,

http://www.wpquestions.com/question/show/id/398 describes showing only one category when running The Loop.

I'm making a band website with the loop on the homepage, and a second loop only using the "gigs" category showing on a different gigs page.

What function do I use instead of is_home() in Will Anderson's code to ask if the current page is the "gigs" page.

I plan to use a plug-in that allows PHP on pages/posts.

How do I call The Loop with this added filter?

To summarize, I want to call a new loop free and separate from the formatting of the main loop, and I want it only to display one category, and this needs to be on a normal wordpress page without affecting my homepage loop.

Many thanks,

Mike.

Will Anderson's code, this does not have to be used but I thought it was a good starting point:

function only_news_cat($query) {

if ( is_home() || is_archive() ) {

$query->set('category_name', 'News');

}

return $query;

}

add_filter('pre_get_posts', 'only_news_cat');

Answers (5)

2011-02-19

Pippin Williamson answers:

You need to go just a little further then the answers above.


if (is_page('gigs'))
{
query_posts('category_name=gigs');
if (have_posts()) :
while (have_posts()) : the_post();
. . . . post content . . . .
endwhile;
else :
echo '<h3>Oops, something went wrong.</h3>';
endif;
wp_reset_query();
}


You need to first check that you are on the correct page, then you need to set your query to only return the "gigs" category, and finally, you need to reset the WP query (wp_reset_query) so that it doesn't mess with any of the other queries being performed on that page.


team74 comments:

Hi, is this a copy and paste solution? It looks like it except for the

. . . . post content . . . .


Pippin Williamson comments:

No, the "wp_reset_query();" is very important.


Pippin Williamson comments:

Oh, sorry, yes, it is a copy and paste solution. Just modify the ....post content.... for what ever you need


team74 comments:

I copied the code, and got the error message. This means the page is parsing php but something went wrong. I checked the category and page names to make sure the names matched exactly lower/upper case.

<?php
if (is_page('gigs')) {

query_posts('category_name=Gigs');
if (have_posts()) :
while (have_posts()) : the_post();
//. . . . post content . . . .
endwhile;
else :
echo '<h3>Oops, something went wrong.</h3>';
endif;
wp_reset_query();
}
?>


This is what displays on page:

<blockquote>Oops, something went wrong.‘; endif; wp_reset_query(); } ?></blockquote>


Pippin Williamson comments:

You might use "in_category()" instead of is_category().

2011-02-20

Duncan O'Neill answers:

This might be easier if you were to make a new .php file. Name it page-pagename.php, or page-gig.php in this case.

Just copy the index.php, and rename it page-gig.php. Then you'll be able to write code which will apply only to the gig page, and avoid these pesky if loops.

Your first loop will be already in place. This will spit out the content you've entered on the gig page.

The first loop starts;

if (have_posts()) : // this will render whatever content you entered on the gigs page

while (have_posts()) : the_post(); ?>


and ends;

<?php endwhile;
endif; ?>


Then after that, you need to reset the query, and make a new one, to show posts from the gigs category, like so;

wp_reset_query(); // reset the query
$gigs = new WP_Query('category_name=gigs&posts_per_page=-1');

if ($gigs->have_posts()) :

while ($gigs->have_posts()) : $gigs->the_post(); ?>

// enter standard loop code, same as in the loop above, or copy it from index.php, or front.php

<?php endwhile; ?>


<?php else :?>

<h2 class="center">Not Found</h2>
<p class="center">Sorry, but you are looking for something that isn't here.</p>
<?php get_search_form(); ?>-->

<?php endif; ?>



Please message me if this doesn't make sense, and I can help you with the code.

regards,

Duncan



team74 comments:

That's a good idea, I'll try this now and then choose a best answer.


team74 comments:

Ok, I had a look and this is my index page:

<?php
/**
* Cue the star of the show...
*
* @package Thesis
*/
thesis_html_framework();
?>


Sorry, I probably should have said I was using Thesis 1.8.


Duncan O'Neill comments:

Hmm, ok, try this, your mileage may vary, because I don't actually have a copy of thesis to test it with. But you should be able to test it quickly....

1) Create a custom_functions.php file in your root directory.

Put the following in it ( add it to the bottom if you have other content )



add_action('my_gigs_posts','thesis_hook_after_content');

function my_gigs_posts(){// same name as the first argument for add_action above
if (is_page('gigs')){
thesis_widget_recent_posts('gigs', 'Your Heading'); // first argument, your posts category
}
}



Note that you can put that gigs list elsewhere by varying the thesis hook you use;

[[LINK href="http://diythemes.com/thesis/rtfm/hooks/"]]Thesis Hooks[[/LINK]]


Duncan O'Neill comments:

Apologies, the custom_functions.php suggested above should of course go in "your thesis directory" rather than "your root directory".

2011-02-19

Jens Filipsson answers:

if (in_category('category-name'))

2011-02-19

juan manuel incaurgarat answers:

you might use is_page();

is_page(42);
// When Page 42 (ID) is being displayed.

is_page('Contact');
// When the Page with a post_title of "Contact" is being displayed.

http://codex.wordpress.org/Function_Reference/is_page

or is_category()

is_category('9')
//When the archive page for Category 9 is being displayed.

is_category('Stinky Cheeses')
//When the archive page for the Category with Name "Stinky Cheeses" is being displayed.

http://codex.wordpress.org/Conditional_Tags

=)

2011-02-19

rilwis answers:

I've made a change to the function you gave, and here is the code. Just add it in functions.php file:

function only_news_cat($query) {
if ( is_home() || is_archive() ) {
$query->set('category_name', 'News');
}
if ( is_page('gigs') || is_page(9) ) { // use page slug or ID here
$query->set('category_name', 'Other News'); // change this into your cat name
}
return $query;
}
add_filter('pre_get_posts', 'only_news_cat');


You can add as many "if statement" as you want, or you can delete unused ones.