手动编号分页不在wordpress模板文件

时间:2015-10-30 04:37:02

标签: php wordpress pagination

我有这个代码可以工作,显示"较旧的条目"和"较新的条目"。但是,我想编辑此代码以显示" 1"," 2"," 3"等等,没有左/右箭头或Prev / Nex文本。因此,我做了研究并感到困惑。我可以理解教程如Wordpress Pagination(编号分页在底部),How To Add Pagination To Your WordPress ThemePaginate Links,但他们没有说明将代码放在何处只有第二个教程完全解释了我需要做什么,但即便如此,这种方式还没有工作......

我的代码有效:

$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;

// the query
$the_query = new WP_Query( 'cat=9&posts_per_page=9&paged=' . $paged ); 
?>

<?php if ( $the_query->have_posts() ) : ?>

<?php
// the loop
while ( $the_query->have_posts() ) : $the_query->the_post(); 
?>

<div class="proyectpost">
            <article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

            <div class="innerpost">

             <div class="postthumbnail">
            <?php if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
    $image_src = wp_get_attachment_image_src( get_post_thumbnail_id(),'full' );
     echo '<img src="' . $image_src[0]  . '" width="100%"  />';
    } ?>
            </div>
            <div class="posttitle">
        <h2><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
            </div><!-- .entry-header -->

        <div class="postsubtitle">
        <div class="datepanel">

        </div>
        </div>

</div>
</article><!-- #post-## --> 

</div>


<?php endwhile; ?>

<?php

// next_posts_link() usage with max_num_pages
next_posts_link( 'Older Entries', $the_query->max_num_pages );
previous_posts_link( 'Newer Entries' );
?>

<?php 
// clean up after the query and pagination
wp_reset_postdata(); 
?>

<?php else:  ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

第二篇教程说要将这段代码添加到functions.php:

// Numbered Pagination
if ( !function_exists( 'wpex_pagination' ) ) {

    function wpex_pagination() {

        $prev_arrow = is_rtl() ? '&rarr;' : '&larr;';
        $next_arrow = is_rtl() ? '&larr;' : '&rarr;';

        global $wp_query;
        $total = $wp_query->max_num_pages;
        $big = 999999999; // need an unlikely integer
        if( $total > 1 )  {
             if( !$current_page = get_query_var('paged') )
                 $current_page = 1;
             if( get_option('permalink_structure') ) {
                 $format = 'page/%#%/';
             } else {
                 $format = '&paged=%#%';
             }
            echo paginate_links(array(
                'base'          => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
                'format'        => $format,
                'current'       => max( 1, get_query_var('paged') ),
                'total'         => $total,
                'mid_size'      => 3,
                'type'          => 'list',
                'prev_text'     => $prev_arrow,
                'next_text'     => $next_arrow,
             ) );
        }
    }

}

并用此代码替换默认分页

<?php wpex_pagination(); ?>

如何让它在我的wordpress模板php文件中显示编号的分页?

1 个答案:

答案 0 :(得分:1)

在进一步研究之后,我找到了一些线索,这些线索让我灵感地想出在哪里放置正确的代码,以便我可以编号。

您使用此代码:

<?php
$big = 999999999; // need an unlikely integer

echo paginate_links( array(
    'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
    'format' => '?paged=%#%',
    'current' => max( 1, get_query_var('paged') ),
    'total' => $the_query->max_num_pages
) );
?>

替换:

<?php

// next_posts_link() usage with max_num_pages
next_posts_link( 'Older Entries', $the_query->max_num_pages );
previous_posts_link( 'Newer Entries' );
?>

我觉得让我感到困惑的是当我在研究时,有些人说要在functions.php中添加一些代码,而有些人则说在某些地方使用其他代码。因此,当我阅读paginate links时,此页面不清楚是否应将上述新代码添加到functions.php或替换“prev / next”链接。

无论如何,它现在有效! :)

相关问题