3

For some reason I can'g get pagination using previous_posts_link and next_posts_link working.

Here is the code I have...

<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>

<?php // The Query
$the_query = new WP_Query(
array(
    'post_type'=>'article',
    'posts_per_page'=>2,
    'orderby'=>'date',
    'paged'=>$paged
)
);

// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();

if($the_query->current_post == 0 && $paged == 1) :
?>
<article class="latest">
    <div class="summary">
        <h4>LATEST ARTICLE:</h4>

        <h2><?=the_title(); ?></h2>
        <div class="entry-meta">
            <?php proagent_posted_on(); ?>
        </div><!-- .entry-meta -->
        <div class="entry-content">
            <?=get_the_content_limit(300, 'More'); ?>

        </div><!-- .entry-content -->

        <div class="entry-topics"><?=get_the_term_list($post->ID , 'topics', 'Topics: ',', '); ?></div>
    </div>
    <?=the_post_thumbnail('feature-post-thumbnail'); ?>
</article>

<?php else: ?>
<article class="previous">
    <?=the_post_thumbnail(); ?>
    <div class="summary">
        <h2><?=the_title(); ?></h2>
        <div class="entry-meta">
            <?php proagent_posted_on(); ?>
        </div><!-- .entry-meta -->
        <div class="entry-content">
            <?=get_the_content_limit(300, 'More'); ?>

        </div><!-- .entry-content -->

        <div class="entry-topics"><?=get_the_term_list($post->ID , 'topics', 'Topics: ',', '); ?></div>
    </div>
</article>
<?php endif; ?>

<?php endwhile; ?>
<nav id="nav-below" class="navigation">
    <div class="alignleft"><?php previous_posts_link('« Newer Articles') ?></div>
    <div class="alignright"><?php next_posts_link('Older Articles »') ?></div>
</nav><!-- #nav-below -->

Also.. just noticed it works when I go to /articles/page/2 there is a link to /articles/ like there should be but from the /articles/ page there is no link to page 2.. WHY?

PLEASE help if you can... this is driving me crazy.

Thanks! mark.

  • In what context are you trying to make this work? Page template? Category template? index.php? That can have important implications for the code. – tollmanz Jul 3 '11 at 4:35
  • It's a custom post category template. I'm trying to show items in my custom post type called "articles" and this is the articles home page. I've defined a page, also called articles.php that I'm coding in. Any help would be very very very much appreciated – markstewie Jul 3 '11 at 9:14
4

The pagination works only, if the paged attribute in the the global $wp_query variable is set. You could store your query manually in this global or just use query_posts() instead of new WP_Query().

<?php
    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    $the_query = query_posts(
        array(
            'post_type'=>'article',
            'posts_per_page'=>2,
            'orderby'=>'date',
            'paged'=>$paged
        )
    );
    // ...
?>

The function query_posts() will unset an existing $wp_query global, runs a new WP_Query() and stores the result again in the global $wp_query variable.

4

your page-slug has to be different than the custom post type name

  • 2
    Please post it as a comment or else explain the how it works a little bit. – bravokeyl Jun 9 '16 at 12:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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