简体   繁体   中英

Add pagination is custom archives page on WordPress (Thesis Theme)

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.

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' ) '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');

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";
    }
}

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM