Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have created a new page with a jquery carousel which rotates between various different news topics. You can see here:

However below the carousel is a static list of additional headlines. What I need to do is find php code that says IF a story appears in the carousel, THEN exclude it from the posts below. This is to avoid duplication.

I cannot simply increase the offset. The offset is currently set to '4'. The carousel contains 12 unique posts at any given time. I cannot increase the offset because if I get five or six posts in a row from one category, then they would be missed from the section below by increasing the offset.

I hope this makes sense. I'm not even sure my goal is attainable(!). Does this all need to be done within the one loop?

The carousel is a loop - it works with the following code:

<?php query_posts("showposts=1&offset=0&cat=$panel"); ?>

where

<div id="panel<?php print $panelCount; ?>" class="panel">

from

<?php 
$panelArray = array('131','129,131,313,318','129','318'); 
$panelCount = 0; 
foreach( $panelArray as &$panel ) { 
    $panelCount = $panelCount + 1;
}
?>

Thanks,

Andy

share|improve this question
3  
Please modify your carousel to use a proper new WP_Query(), rather than query_posts(), and then we should be able to help provide a solution. –  Chip Bennett May 16 '13 at 19:14

2 Answers 2

Track the IDs, and "exclude" them with post__not_in. It is hard to tell exactly what does what with the disjointed way you posted your code but the general case solution is ..

$c_posts = new WP_Query("showposts=10");
$tracker = array();
if ($c_posts->have_posts()) {
  while ($c_posts->have_posts()) {
   $c_posts->the_post();
   $tracker[] = get_the_ID();
   // more code
  }
}
$other_loop = new WP_Query(array('post__not_in' => $tracker));

Depending on how much control you have over things, there are other ways to do it. For example,if you need 5 posts in the carousel and 10 in the body you could pull all 15 at once, use the first five in the carousel and the use up the rest later, thus saving yourself a query.

Reference

http://codex.wordpress.org/Class_Reference/WP_Query

share|improve this answer

If your slider shows posts in a particular category, which is defined by the variable $panel, then you can cleanly separate the slider posts from the normal flow.

Display Slider

Use new WP_Query() in your template to output the slider:

$slider = new WP_Query( array(
    // 'showposts' is deprecated; use 'posts_per_page'
    'posts_per_page' => 10,
    // For an integer; 
    // if $panel is a string (category slug), use 'category_name'
    'cat' => $panel 
) );

if ( $slider->have_posts() ) : while ( $slider->have_posts() ) : $slider->the_post();
    // Slider loop/markup here
endwhile; endif;
wp_reset_postdata();

// Normal loop here
if ( have_posts() ) : while ( have_posts() ) : the_post();
    // Normal loop markup
endwhile; endif;

Filter the main loop query

Add a filter callback to functions.php to filter the slider posts:

function wpse99748_filter_pre_get_posts( $query ) {
    if ( ! is_singular() && $query->is_main_query() ) {
        $panel = array(); // Need to use ID here
        $query->set( 'category__not_in', $panel );
    }
}
add_action( 'pre_get_posts', 'wpse99748_filter_pre_get_posts' );
share|improve this answer
    
Thanks for your reply Chip. It may take me a little while to see if this works. –  Andrew Abbott May 19 '13 at 18:43
    
Hi - what do you mean by 'Need to use ID here'? –  Andrew Abbott May 19 '13 at 20:48
    
The category__not_in parameter requires an array of Post IDs. –  Chip Bennett May 20 '13 at 0:58
    
I am not looking to exclude any categories from the slider at the top, though. All posts need to pass through the category into the content below it. So all categories are eligible for all locations. I need to be able to prevent duplication of posts in the carousel from the area beneath. –  Andrew Abbott May 20 '13 at 18:19
    
I'm not sure how to use the array of post IDs - I can't reference them explicitly because the post IDs are constantly changing... ? –  Andrew Abbott May 20 '13 at 18:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.