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'm trying to display the first image attachment from each post on the front page of the site. This code below works on other pages, however pagination does not work on is_front_page().

First page to show 1-20 thumbnails, clicking next would bring you to page 2 showing 21-40 thumbnails, etc.

I've reset my permalinks, and have no errors in PHP log.

Any ideas?

function homepage_grid() {
global $wpdb, $post; 
 ?>
    <div id="content">
        <div class="post_box">
            <div class="grid_thumbs">
                <?php
                $limit = 20;
                $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                query_posts('showposts=' . $limit . '&paged=' . $paged . '&orderby=date&order=DESC');
                ?>

                    <?php while (have_posts()) : the_post();
                        $attachment = array_values( get_children( array( 'post_parent' => $post->ID, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC', 'numberposts'  => 1 ) ) ); 
                        var_dump($attachment);
                        ?><div class="thumbnails"><a href="<?php the_permalink() ?>" title="<?php the_title('', ''); ?>"><?php if( $attachment ) echo '<img src="' . wp_get_attachment_thumb_url($attachment[0]->ID) . '" />'; ?></a></div>
                    <?php endwhile; ?>
                        <div class="clear"></div>
                    <?php post_navigation(); ?>
                    <br />
            </div> <!-- div.grid_thumbs close -->
            <div class="clear"></div>

        </div>
    </div>
<?php }

function post_navigation() {
    global $wp_query;

    if ($wp_query->is_home || $wp_query->is_archive || $wp_query->is_search) {
        if ($wp_query->max_num_pages > 1) {
            $previous = 'Previous Entries';
            $next = 'Next Entries';
            echo "\t\t\t<div class=\"prev_next\">\n";

            if ($wp_query->query_vars['paged'] <= 1) {
                echo "\t\t\t\t<p class=\"previous\">";
                next_posts_link($previous);
                echo "</p>\n";
            }
            elseif ($wp_query->query_vars['paged'] < $wp_query->max_num_pages) {
                echo "\t\t\t\t<p class=\"previous floated\">";
                next_posts_link($previous);
                echo "</p>\n";

                echo "\t\t\t\t<p class=\"next\">";
                previous_posts_link($next);
                echo "</p>\n";
            }
            elseif ($wp_query->query_vars['paged'] >= $wp_query->max_num_pages) {
                echo "\t\t\t\t<p class=\"next\">";
                previous_posts_link($next);
                echo "</p>\n";
            }

            echo "\t\t\t</div>\n\n";
        }
    }
}
share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

According to the WP_Query docs:

Pagination Note: Use get_query_var('page'); if you want your query to work in a Page template that you've set as your static front page. The query variable 'page' also holds the pagenumber for a single paginated Post or Page that includes the Quicktag in the post content.

Display posts from current page on a static front page:

$paged = (get_query_var('page')) ? get_query_var('page') : 1;
$query = new WP_Query( array( 'paged' => $paged ) );
share|improve this answer
    
I've tried what you have here (and according to the codex), but it wasn't working for me. After Googling, I found this and it works - sort of. --> if ( get_query_var('paged') ) { $paged = get_query_var('paged'); } elseif ( get_query_var('page') ) { $paged = get_query_var('page'); } else { $paged = 1; }, but doing this breaks previous_posts_link, so I'm still at a loss. –  Pat Jul 29 '13 at 12:21
    
And I'm seeing that next_posts_link only goes to page 2, when I should have 87 pages to go through. –  Pat Jul 29 '13 at 12:39
    
Since your answer technically answers my question, I'm going to mark yours as the answer and open a new question about my previous/next_posts_link issue. –  Pat Jul 29 '13 at 12:43
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.