0

In my loop.php (see below) I basically have a condition where if the page is not paginated (i.e. if the URL does not look something like this: example.com/page/2) then display 2 posts with a category of "featured-content"...otherwise if the page is paginated then display 5 posts. This works fine except it is not properly querying the "featured-content" category as posts marked with this category are not always showing up in the loop feed.

I hope I articulated the issue properly, if not please let me know and I'll clarify.

            <!-- Featured Content Slider -->
        <?php include('includes/featured-content-slider.inc.php'); ?>
        <!-- /Featured Content Slider -->

        <!-- 960 16 Column Grid -->
        <div class="container_16" <?php if (!is_paged()) { echo "id=home";} ?>> 

            <!-- Featured News -->
            <section class="grid_10 featured-news" <?php if (is_paged()) {echo "id=margin-top-120-o";}?>>

                <!-- Featured News Heading -->
                <h1><span class="biography">Featured Content</span></h1>
                <!-- /Featured News Heading -->

                    <!-- Featured News Loop -->
                    <?php
                    if (!is_paged()) {
                        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                            $args=array(
                                'post_type'=>'post',
                                'category_name' => 'featured-content',
                                'posts_per_page' => 2,
                                'paged'=>$paged
                            );
                            $temp = $wp_query;
                            $wp_query= null;
                            $wp_query = new WP_Query($args);
                    } else {
                        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                            $args=array(
                                'post_type'=>'post',
                                'category_name' => 'featured-content',
                                'posts_per_page' => 5,
                                'paged'=>$paged
                            );
                            $temp = $wp_query;
                            $wp_query= null;
                            $wp_query = new WP_Query($args);
                    }
                        if (function_exists('wp_pagenavi')) { wp_pagenavi(); }
                        if ( $wp_query->have_posts() ) : while ( $wp_query->have_posts() ) : $wp_query->the_post();
                    ?> 
                    <!-- /Featured News Loop -->

                        <!-- Post --> 
                        <article class="post" id="post-<?php the_ID(); ?>" <?php post_class(); ?> >

                            <!-- Featured News Title -->
                            <span><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( '%s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
                            </span>
                            <!-- /Featured News Title -->

                            <!-- Featured News Meta -->
                            <p class="featured-news-post-meta">By <span class="featured-news-author"><?php echo get_the_author(); ?></span> / <?php echo get_the_date('m.d.Y'); ?></p>
                            <!-- /Featured News Meta -->

                            <!-- Featured News Thumbnail -->
                            <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail(array(640,320)); ?></a>
                            <!-- /Featured News Thumbnail -->

                            <!-- Featured News Excerpt -->
                            <p class="featured-news-excerpt"><?php the_excerpt(); ?></p>
                            <!-- /Featured News Excerpt -->

                            <!-- Featured News Social Links -->
                            <?php include('includes/social.inc.php'); ?>
                            <!-- /Featured News Social Links -->

                        </article>
                        <!-- /Post -->

                    <!-- Featured News Loop -->
                    <?php
                        endwhile; endif;
                        /* PageNavi at Bottom */
                        if (function_exists('wp_pagenavi')){wp_pagenavi();}
                        $wp_query = null;
                        $wp_query = $temp;
                        wp_reset_query();
                    ?>
                    <!-- /Featured News Loop -->

                <!-- Pagination -->
                <?php if ($wp_query->max_num_pages > 1) : ?>
                    <div class="grid_10 pagination older-news">
                        <?php next_posts_link( __( '<span class="arrow">&larr;</span> Older', 'twentyten' ) ); ?>
                        <div class="pagination newer-news">
                        <?php previous_posts_link( __( 'Newer <span class="arrow">&rarr;</span>', 'twentyten' ) ); ?>
                        </div>
                    </div>
                <?php endif; ?>
                <!-- /Pagination -->

            </section>
            <!-- /Featured News -->

            <!-- What's Happening -->
            <div <?php if (is_paged()) {echo "id=margin-top-120-o";}?>>
                <?php include('includes/whats-happening.inc.php'); ?>
            </div>
            <!-- /What's Happening -->

        </div>
        <!-- /960 16 Column Grid -->

1 Answer 1

1

i'm not sure what you are trying to achieve w/ the $paged variable. seems like you could simplify your query though

$posts_per_page = is_paged() ? 5 : 2; 

$args=array(
      'post_type'=>'post',
      'category_name' => 'featured-content',
      'posts_per_page' => $posts_per_page
);

$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query($args);

edit, adding an offset (and fixing syntax errors):

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$posts_per_page = !is_paged() ? 5 : 2; 
$offset = is_paged() ? 5 + 2*($paged-2) : 0;

$args=array(
      'post_type'=>'post',
      'category_name' => 'featured-content',
      'posts_per_page' => $posts_per_page,
      'offset' => $offset
);

$featured = new WP_Query($args);

if($featured->have_posts()):

    echo '<ul>';

    while ($featured->have_posts()): $featured->the_post();
        echo '<li>' . get_the_title(). '</li>';
    endwhile;

    echo "</ul>";
endif;

it is possible that if you don't have a ton of featured items, you will run out as you go back through the paging.

6
  • That works; however, when I paginate to page 3, 4, 5, etc. it shows the same posts..
    – Matt
    Commented Jul 17, 2012 at 3:13
  • ok, you didn't specify that (though i could maybe have inferred it from the $paged). i think you need to set the offset. lemme think. Commented Jul 17, 2012 at 3:20
  • Hey, thanks for the help...I tried this code and it seems to break the loop. No posts show...
    – Matt
    Commented Jul 17, 2012 at 14:29
  • which loop does it break? the featured or the main? i'm not testing, so there are possible/likely syntax errors. Commented Jul 17, 2012 at 14:32
  • The code that starts just below <!--Featured News Loop-->. I removed the if/else statement and just plugged that code in there..is that wrong?
    – Matt
    Commented Jul 17, 2012 at 14:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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