2

I'm using the following php code on a page to show 10 posts per page from a single category.

function my_page_of_posts5() {
    if (is_page('10')) {
        $custom_loop = new WP_Query('posts_per_page=10&cat=9');
        echo '<div class="my-archives"><ul class="archive-list">';
        if ( $custom_loop->have_posts() ) : 
            while ( $custom_loop->have_posts() ) : $custom_loop->the_post();
                echo '<li><a class="archive-link" href="' 
                    . get_permalink() . '">' 
                    . get_the_title() 
                    . '</a> <span class="my-comment-count">( ';
                comments_number('0', '1', '%');
                echo ' )</span></li>';
                the_excerpt();
            endwhile;
            wp_reset_query();
        endif;
        echo '</ul></div>';
    }
}
add_action('thesis_hook_after_content','my_page_of_posts5');

I'm using Thesis WordPress Theme Framework.

I want to display pagination after these posts on the page.

What code shall I put there to show the pagination?

I've tried using the WP-Paginate plugin and putting the wp-paginate(); function on the page but it doesn't work fine.

Need another way to sort it out.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Bhanu Chawla
  • 1,138
  • 2
  • 13
  • 26

4 Answers4

1

Without knowing more about what the exact behavior of your query is when you attempt to use wp_paginate, the best I can suggest is adding 'paged=' . get_query_var( 'paged' ) to your WP_Query args, comme ça:

$custom_loop = new WP_Query('posts_per_page=10&cat=9&paged=' . get_query_var( 'paged' ));

Once you add that, wp_paginate might work for you.

Give it a shot!

UPDATE

Ok, from the start:

  1. declare and grab the paged query var
  2. use it as an argument in your query
  3. call posts_nav_link to get the necessary nav

Your whole snippet should work like so:

function my_page_of_posts5() {
    if (is_page('10')) {
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; // what page? if none, one
        $custom_loop = new WP_Query('posts_per_page=10&cat=9&paged=' . $paged);
        echo '<div class="my-archives"><ul class="archive-list">';
        if ( $custom_loop->have_posts() ) : while ( $custom_loop->have_posts() ) : $custom_loop->the_post();
            echo '<li><a class="archive-link" href="' . get_permalink() . '">' . get_the_title() . '</a> <span class="my-comment-count">( ';
            comments_number('0', '1', '%');
            echo ' )</span></li>';
            the_excerpt();
            endwhile;
            wp_reset_query();
        endif;
    echo '</ul></div>';
    posts_nav_link(); // your next and previous links
    }
}
add_action('thesis_hook_after_content','my_page_of_posts5');
crowjonah
  • 2,858
  • 1
  • 23
  • 27
  • Not working for me. See, I followed this tutorial for the posts on a page: http://diythemes.com/thesis/rtfm/add-category-posts-on-a-page/ – Bhanu Chawla Nov 21 '12 at 12:03
  • 1
    Are you including `posts_nav_link`? You can paginate query results all you want, but if you don't have any way to get to them, it's no good! – crowjonah Nov 21 '12 at 14:14
  • I reckon that would work fine. But where do I put wp_paginate() function? – Bhanu Chawla Nov 21 '12 at 17:23
  • You'd have to replace `posts_nav_link` with `wp_paginate`, I suppose. – crowjonah Nov 21 '12 at 19:39
  • Still not working. Not sure why the `wp_paginate();` function isn't even displaying the pagination. :/ – Bhanu Chawla Nov 22 '12 at 11:33
  • Why don't you try the standard issue Wordpress nav link functions first and eliminate the possibility that it's a plugin issue. – crowjonah Nov 22 '12 at 16:33
  • I'm trying `posts_nav_link();` now and it's still not working. As you can see on this page here: http://recruitmentjobs.co.uk/people-moves/ – Bhanu Chawla Nov 22 '12 at 17:15
  • Did you check out the page mate? Any help is appreciated. Thanks – Bhanu Chawla Nov 25 '12 at 16:19
  • Well, http://recruitmentjobs.co.uk/people-moves/page/2/ seems to work, so you must be nearly there. You might need to kill the `wp_reset_query();` line, as that might be stripping the info the pagination links need. Try that! – crowjonah Nov 26 '12 at 21:07
  • Well, removing `wp_reset_query();` didn't change anything but pagination was displayed when I used `` in thesis_after_content_box hook (using OpenHooks). – Bhanu Chawla Nov 27 '12 at 10:10
  • Aahh! But even that isn't working fine for it. Check out the pagination here: http://recruitmentjobs.co.uk/people-moves/ I can't go back to page 1 using that. – Bhanu Chawla Nov 28 '12 at 10:36
  • 1
    It looks like the wp_paginate plugin is defining the `current` page value incorrectly, likely because it is reading the `$paged` query_var of the wrong loop. So you've probably put it in the wrong place. – crowjonah Nov 28 '12 at 15:33
1

Try using this code instead, replace it what you have now.
It adds a pagination function I always use, no need for an extra plugin. Easy to customize.

<?php
function my_page_of_posts5() {
    if (is_page('10')) {
        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; // what page? if none, one
        $custom_loop = new WP_Query('posts_per_page=10&cat=9&paged=' . $paged);
        echo '<div class="my-archives"><ul class="archive-list">';
        if ( $custom_loop->have_posts() ) : while ( $custom_loop->have_posts() ) : $custom_loop->the_post();
            echo '<li><a class="archive-link" href="' . get_permalink() . '">' . get_the_title() . '</a> <span class="my-comment-count">( ';
            comments_number('0', '1', '%');
            echo ' )</span></li>';
            the_excerpt();
            endwhile;
            wp_reset_query();
        endif;
    echo '</ul></div>';
    theme_pagination();
    }
}
add_action('thesis_hook_after_content','my_page_of_posts5');

// based on http://www.kriesi.at/archives/how-to-build-a-wordpress-post-pagination-without-plugin
function theme_pagination($pages = '', $range = 2) {
    $showitems = ($range * 2)+1;

    global $paged;
    if(empty($paged)) $paged = 1;

    if($pages == '') {
        global $wp_query;
        $pages = $wp_query->max_num_pages;
        if(!$pages) {
            $pages = 1;
        }
    }

    if(1 != $pages) {
        echo '<div class="pagination">';
        //if($paged > 1 && $showitems < $pages)
            echo '<a href="'.get_pagenum_link($paged - 1).'" class="previous" >';
            echo _x('previous', 'paginate', THEME_L10n);
            echo '<span>previouw-arrow</span></a>';

        for ($i=1; $i <= $pages; $i++) {
            if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )) {
                if ($paged == $i){
                    echo '<span class="current number">'.$i.'</span>';
                } else {
                    echo '<a href="'.get_pagenum_link($i).'" class="inactive number" >'.$i.'</a>';
                }
            }
        }

        //if ($paged < $pages && $showitems < $pages)
        echo '<a href="'.get_pagenum_link($paged + 1).'" class="next" ><span>next-arrow</span>';
        echo _x('next', 'paginate', THEME_L10n);
        echo '</a>';
        echo "</div>\n";
    }
}
janw
  • 6,672
  • 6
  • 26
  • 45
  • is your code and situation the same as the code in the question? – janw Nov 30 '12 at 08:28
  • It isn't working for me either. Nothing's changed after using your code. It works similar to the default code I've pasted in this question. Thanks for a try anyway :) – Bhanu Chawla Dec 03 '12 at 13:23
  • Have you put a `die()` in the filter to test that the function is actually executed? – janw Dec 03 '12 at 14:13
1

You may try adding this pagination code at the end of the code in your example:

//Set Blog Reading Settings to 10!
global $wp_query;
$big = 999999999; // need an unlikely integer
echo paginate_links( array( 'base'    => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
                            'format'  => '?paged=%#%',
                            'current' => max( 1, get_query_var( 'paged' ) ),
                            'total'   => $wp_query->max_num_pages,
                            'end_size'=> 1,
                            'mid_size'=> 5 ) );

The code works in archive and category pages of normal free themes, but the one you are using is not free and is not open source, so it's hard to predict the result.

I think the posts quantity should be removed:

WP_Query('posts_per_page=10&cat=9')
// Just leave it like this:
WP_Query('cat=9')

The reason is the number of posts per page in WP is configured in the Reading Settings. Placing a quantity in the query might be blocking the pagination. Needless to say, there must be more than 10 posts for pagination to work.

Good luck!

Felipe Alameda A
  • 11,791
  • 3
  • 29
  • 37
0

There is a Two possible Solutions :

(i). Solution #1 :

i wish you will think that wp-pagenavi is the best solution here. And it is too easy to integrate it in Thesis theme. As a Thesis theme user you are using thesis openhook plugin already.

Now just follow the steps to add pagenavi in Thesis theme.

  • Open Apperance > Thesis Openhook.
  • Find After Content.
  • Just paste the code in the box. <?php cr_pagenavi(); ?>
  • Check Execute PHP on this hook and click save.

(ii). Solution #2 :

1. First of all I installed a plugin which is Simple Pagination (and its literally simple to use). If you’re using thesis, copy and paste this code in thesis_hook_after_content and hit save.

<div class="pagination">
<?php wp_simple_pagination(); ?>
</div>

2. Go to your custom_functions.php and paste this code to remove the Previous and Next post

function no_home_post_nav() {
if (is_home())
  remove_action('thesis_hook_after_content', 'thesis_post_navigation');
}
add_action('thesis_hook_before_content','no_home_post_nav');

3. Check out your new pagination on your homepage and customize it to any style you want.

John Peter
  • 2,870
  • 3
  • 27
  • 46
  • Ok I tried both of them and this is what happened. Solution 1: This broke the rest of the page execution so nothing was appearing after the edited hook. Solution 2: You can see this live in action on http://recruitmentjobs.co.uk/people-moves/ as it's only displaying a div saying Pages and the pages aren't there. Thanks for a try anyway. It looks like this issue isn't resolving. :( – Bhanu Chawla Dec 07 '12 at 11:55
  • Any help on this please? It's been a while. – Bhanu Chawla Jan 24 '13 at 15:47