WordPress.org

Ready to get started?Download WordPress

Forums

Pagination not working on page of posts (10 posts)

  1. Elmarie
    Member
    Posted 3 years ago #

    I've created a custom page to display posts from a specific category. But the pagination doesn't work. When you click on "next" the link looks like this: http://localhost:8080/mysite/page/2/ but it's still displaying the same posts as on the first page. Can anybody please help???

    My code looks like this:
    <?php
    /*
    Template Name: PageOfPosts
    */

    get_header(); ?>

    <div id="page-content" class="narrowcolumn">

    <?php

    $cat = 1;
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $post_per_page = 2; // -1 shows all posts
    //$do_not_show_stickies = 1; // 0 to show stickies
    $args=array(
    'category__in' => array($cat),
    'orderby' => 'date',
    'order' => 'DESC',
    'paged' => $paged,
    'showposts' => $post_per_page
    //'caller_get_posts' => $do_not_show_stickies
    );
    $temp = $wp_query; // assign orginal query to temp variable for later use
    $wp_query = null;
    $wp_query = new WP_Query($args);

    if( have_posts() ) :
    while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
    <div id="post-<?php the_ID(); ?>" class="homepage-posts">
    <h2>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></h2>

    <?php
    $images = get_children( array( 'post_parent' => $post->ID, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'orderby' => 'menu_order', 'order' => 'ASC', 'numberposts' => 999 ) );
    if ( $images ) :
    $total_images = count( $images );
    $image = array_shift( $images );
    $image_img_tag = wp_get_attachment_image( $image->ID, 'thumbnail' );
    ?>
    <div class="homepage-thumb">
    "><?php echo $image_img_tag; ?>
    </div><!-- .gallery-thumb -->
    <div class="homepage-summary">
    <?php the_excerpt( __( 'Read more <span class="meta-nav">→</span>', 'twentyten' ) ); ?>
    <?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'twentyten' ), 'after' => '</div>' ) ); ?>
    </div><!-- .entry-content -->
    <?php else : ?>
    <div class="homepage-summary-full">
    <?php the_excerpt( __( 'Read more <span class="meta-nav">→</span>', 'twentyten' ) ); ?>
    <?php wp_link_pages( array( 'before' => '<div class="page-link">' . __( 'Pages:', 'twentyten' ), 'after' => '</div>' ) ); ?>
    </div><!-- .entry-content -->
    <?php endif; ?>

    </div>
    <?php endwhile; ?>

    <div class="navigation">
    <div class="alignright"><?php previous_posts_link('Newer Entries »') ?></div>
    <div class="alignleft"><?php next_posts_link('« Older Entries') ?></div>

    </div>
    <?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;

    $wp_query = $temp; //reset back to original query

    ?>

    </div>

    <?php get_sidebar(); ?>

    <?php get_footer(); ?>

  2. Chase Adams
    Member
    Posted 3 years ago #

    For starters, why not simply use a category page?

  3. Elmarie
    Member
    Posted 3 years ago #

    Hi Chase

    I'm very new to wordpress. How do I create a category page? Is it better to use a category page instead of creating a page of posts?

    Thank you!

  4. Chase Adams
    Member
    Posted 3 years ago #

    Welcome to the wonderful world of WordPress!

    If you're wanting to display posts from a specific category, your theme should have a category.php file...which means you should be able to type http://localhost/category/mysite/fu with fu being your category (assuming mysite is a sub-site of your localhost).

    Are you trying to set that the posts from that category as the homepage?

    Here's a link to the WordPress Codex on Category Templates. Let me know if that doesn't answer all the questions you have.

    Chase Adams
    The Chase Scene | Chase Your Passion
    Do You have a gravatar? Set it up!
    Twitter — @realchaseadams

  5. Elmarie
    Member
    Posted 3 years ago #

    Hi Chase

    I have 2 category pages. One will be set as the homepage.

    Thank you very much for this information.

    Kind Regards,
    Elmarie

  6. Elmarie
    Member
    Posted 3 years ago #

    Hi Chase

    Thank you for your advice - it worked! Just one question how do I set my new category page as my homepage?

    Thank you!

  7. Chase Adams
    Member
    Posted 3 years ago #

    I think the best thing to do would be to go to your index.php and delete this bit that you put in originally:

    $cat = 1;
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $post_per_page = 2; // -1 shows all posts
    //$do_not_show_stickies = 1; // 0 to show stickies
    $args=array(
    'category__in' => array($cat),
    'orderby' => 'date',
    'order' => 'DESC',
    'paged' => $paged,
    'showposts' => $post_per_page
    //'caller_get_posts' => $do_not_show_stickies
    );
    $temp = $wp_query; // assign orginal query to temp variable for later use
    $wp_query = null;
    $wp_query = new WP_Query($args);

    And replace it with this:
    query_posts('cat=4&posts_per_page=10');

    To get your cat, simply go to your Dashboard > Posts > Categories and scroll over the link for the category. The category you want to use is in the link.

    posts_per_page obviously is how many you want to display on each page.

  8. Elmarie
    Member
    Posted 3 years ago #

    Hi

    I didn't add that code to my index page. I've created a page of posts for the categories. I tried to change it there but it didn't work.

  9. Chase Adams
    Member
    Posted 3 years ago #

    Duplicate your index.php and rename it front-page.php

    Paste query_posts('cat=4&posts_per_page=10'); above the if(have_posts()) bit and it should work.

    WordPress is a very powerful system that comes preset so that you can customize down to very intricate levels. The point of the front-page.php file is so you can have something totally different show up when you visit than your index.php (which is considered the actual "blog" file).

    I suggest doing this because the people who created WordPress set it up so you wouldn't have to try and create a work around, it just works...don't try to reinvent the wheel. :)

    Good luck.

    Chase Adams
    Web Developer
    The Chase Scene | Chase Your Passion
    Twitter — @realchaseadams
    Facebook — @realchaseadams
    678-614-7146
    Do You have a gravatar? Set it up!

  10. Elmarie
    Member
    Posted 3 years ago #

    Thank you!

Topic Closed

This topic has been closed to new replies.

About this Topic