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.

Is it possible to include index pagination on a static front page?

I have a child theme of twenty eleven and I am attempting to have a home page where there is a slider for sticky posts followed by the recent posts with a custom query, that I would like to be paginated.

I created a page template, and set that page template to be the static front page.

For some reason, pagination only works when I am logged in to WordPress. If I visit the site as an anonymous visitor, I get a 404 and the URL looks like "example.com/2011/09/268" instead of "example.com/page/2".

Am I going about this the completely wrong way?

Below is the code for the page template:

<?php
get_header(); ?>

        <div id="primary">
            <div id="content" role="main">

                <?php the_post(); ?>

                <?php
                    $sticky = get_option( 'sticky_posts' );

                    // Proceed only if sticky posts exist.
                    if ( ! empty( $sticky ) ) :

                    $featured_args = array(
                        'post__in' => $sticky,
                        'post_status' => 'publish',
                        'posts_per_page' => 10,
                        'no_found_rows' => true,
                    );

                    // The Featured Posts query.
                    $featured = new WP_Query( $featured_args );

                    // Proceed only if published posts exist
                    if ( $featured->have_posts() ) :

                    $counter_slider = 0;

                    ?>


                <div class="featured-posts">
                    <h1 class="showcase-heading"><?php _e( 'Featured Posts', 'twentyeleven' ); ?></h1>

                <div class="flexslider">
                  <ul class="slides">
                <?php
                    // Let's roll.
                    while ( $featured->have_posts() ) : $featured->the_post();

                    if ( has_post_thumbnail()) : ?>
                    <li>
                        <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" >
                           <?php the_post_thumbnail(); ?>
                           <p class="flex-caption"><a href="<?php the_permalink(); ?>"
                            title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyeleven' ),
                            the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark">
                            <?php the_title(); ?></a></p>
                       </a>
                    </li>
                    <?php
                    endif; // End check for post thumbnail ?>
                <?php endwhile; ?>
                  </ul>
                </div>

                <?php //endif; // End check for more than one sticky post. ?>
                </div><!-- .featured-posts -->
                <?php endif; // End check for published posts. ?>
                <?php endif; // End check for sticky posts. ?>

                <?php
                    // Display our recent posts
                    // Setup for pagination of custom loop
                    $paged = (get_query_var('page')) ? get_query_var('page') : 1;

                    $recent_args = array(
                        'order' => 'DESC',
                        'post__not_in' => get_option( 'sticky_posts' ),
                        'category__not_in' => array( 46, 47 ),
                        'tax_query' => array(
                            array(
                                'taxonomy' => 'post_format',
                                'terms' => array( 'post-format-aside', 'post-format-link', 'post-format-quote', 'post-format-status' ),
                                'field' => 'slug',
                                'operator' => 'NOT IN',
                            ),
                        ),
                        'posts_per_page' => 10,
                        'paged' => $paged,
                    );

                    // Our new query for the Recent Posts section.
                    query_posts( $recent_args );

                    // The first Recent post is displayed normally
                    //if ( $wp_query->have_posts() ) : $wp_query->the_post();
                    ?>
                    <h1 class="showcase-heading"><?php _e( 'Recent Posts', 'twentyeleven' ); ?></h1>
                    <?php twentyeleven_content_nav( 'nav-above' ); ?>
                    <div class="recent-posts">
                    <?php
                        if ( $wp_query->have_posts() ) :
                        while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
                            <div class="recent-post">
                            <?php if ( "image" == get_post_format() ) :

                                get_template_part( 'content', 'image' );
                            else : ?>
                                <h2 class="entry-title">
                                    <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyeleven' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a>
                                </h2>
                                <div class="entry-meta">
                                    <?php twentyeleven_posted_on(); ?>
                                </div><!-- .entry-meta -->
                                <?php if( has_post_thumbnail() ) : ?>
                                    <a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyeleven' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark">
                                    <?php the_post_thumbnail('excerpt-thumbnail', array('class' => 'alignleft') ); endif; ?></a>
                                <?php the_excerpt(); ?>
                                <p class="comments-link">
                                        <?php comments_popup_link( '<span class="leave-reply">' . __( 'Leave a Comment', 'twentyeleven' ) . '</span>', __( '<b>1</b> Comment', 'twentyeleven' ), __( '<b>%</b> Comments', 'twentyeleven' ) ); ?>
                                </p>
                            <?php endif; ?>
                            </div>
                            <hr />
                        <?php
                        endwhile; ?>
                        <?php
                        endif; ?>
                    <?php
                    twentyeleven_content_nav( 'nav-below' );
                    // Reset because we used query_posts
                    wp_reset_query(); ?>
                    </div> <!-- End recent-posts -->

                <?php //get_template_part( 'content', 'page' ); ?>

                <?php //comments_template( '', true ); ?>

            </div><!-- #content -->
        </div><!-- #primary -->

<?php get_sidebar(); ?>
<?php get_footer(); ?>
share|improve this question
    
Please explain why you set a static front page. I just don’t see it. –  toscho Mar 10 '12 at 8:58
    
Yes, that's why I think I'm doing it wrong. I modeled it after the Showcase template from the twentyeleven theme. I didn't know how to redirect the home page to a page template to use the custom loops I wanted to use. –  jjeaton Mar 10 '12 at 10:29
    
It turns out that the Jetpack stats module is the reason for it working when I'm logged in, but if I'm going about this the wrong way, please let me know. –  jjeaton Mar 10 '12 at 10:30
add comment

1 Answer 1

up vote 1 down vote accepted

Create a file front-page.php as a copy your index.php. Add the slider code on top of the page. WordPress will now take care of the pagination for you.

If you need the slider on the first page only wrap it into a conditional:

// First page
if ( empty ( $GLOBALS['paged'] ) or 1 == $GLOBALS['paged'] )
{
    // slider code
}
// the regular loop follows
share|improve this answer
    
Do I need to do the $temp = $wp_query replacements? Should I be using query_posts? I tried this with front-page.php and I can't get it to work. I can't just use index.php because the $recent_args in my code is the query I'm trying to use for the front page. Thanks. –  jjeaton Mar 10 '12 at 11:58
1  
WP_Query() is fine. You shouldn’t use query_posts(). –  toscho Mar 10 '12 at 12:18
    
Thanks toscho, I was able to get it working. –  jjeaton Mar 10 '12 at 13:13
add comment

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.