在新主题的帖子中显示分页 [英] Displaying Pagination in posts in new theme

查看:94
本文介绍了在新主题的帖子中显示分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发自定义主题,并且希望在blog页面中显示分页.

I am developing Custom Theme and I want to display pagination in blog page.

这是wp_query:

Here's the wp_query:

    <?php
        $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
            $args = array(
            'posts_per_page' => 2,
            'paged'          => $paged
        );
        $the_query = new WP_Query( $args );
            while ($the_query -> have_posts()) : $the_query -> the_post(); 
    ?>

显示TitleExcerpt的代码:和endwhile循环

The Code to display Title and Excerpt: and endwhile loop

    <a href="<?php the_permalink() ?>"><div class="titleline"><h2><?php the_title(); ?></h2></div></a>

        <li><?php the_excerpt(__('(more…)')); ?></li>
    <?php 
        endwhile;
        wp_reset_postdata();
    ?>

Pagination从此处开始:

And the Pagination Starts Here:

<div class="alignleft"><?php previous_posts_link('&laquo; Previous') ?></div>
<div class="alignright"><?php next_posts_link('More &raquo;') ?></div>

我在这里错过了什么吗?任何建议将不胜感激..

Am I missing anything here? Any suggestion will be appreciated..

推荐答案

将此代码放在模板文件中

Put this code in your template file

<?php
global $query_string;
                                query_posts($query_string . "post_type=post&post_status=publish&posts_per_page=2");  
                                if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
                                <h4><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h4>
                                 <h5>Posted By :  <?php the_author(); ?> | Date : <?php echo the_date('d-m-y'); ?></h5>
                                <p><?php  the_excerpt(); ?></p>

                            <?php endwhile;
                                endif; ?>
                                <?php pagination_numeric_posts_nav(); ?>
                        <?php wp_reset_query(); ?>

并将此函数放在主题的functions.php文件中

And put this function in functions.php file of theme

function pagination_numeric_posts_nav() {

    if( is_singular() )
        return;

    global $wp_query;

    /** Stop execution if there's only 1 page */
    if( $wp_query->max_num_pages <= 1 )
        return;

    $paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
    $max   = intval( $wp_query->max_num_pages );

    /** Add current page to the array */
    if ( $paged >= 1 )
        $links[] = $paged;

    /** Add the pages around the current page to the array */
    if ( $paged >= 3 ) {
        $links[] = $paged - 1;
        $links[] = $paged - 2;
    }

    if ( ( $paged + 2 ) <= $max ) {
        $links[] = $paged + 2;
        $links[] = $paged + 1;
    }

    echo '<div class=""><ul class="pagination">' . "\n";

    /** Previous Post Link */
    if ( get_previous_posts_link() )
        printf( '<li>%s</li>' . "\n", get_previous_posts_link() );

    /** Link to first page, plus ellipses if necessary */
    if ( ! in_array( 1, $links ) ) {
        $class = 1 == $paged ? ' class="active"' : '';

        printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );

        if ( ! in_array( 2, $links ) )
            echo '<li>…</li>';
    }

    /** Link to current page, plus 2 pages in either direction if necessary */
    sort( $links );
    foreach ( (array) $links as $link ) {
        $class = $paged == $link ? ' class="active"' : '';
        printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
    }

    /** Link to last page, plus ellipses if necessary */
    if ( ! in_array( $max, $links ) ) {
        if ( ! in_array( $max - 1, $links ) )
            echo '<li>…</li>' . "\n";

        $class = $paged == $max ? ' class="active"' : '';
        printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
    }

    /** Next Post Link */
    if ( get_next_posts_link() )
        printf( '<li>%s</li>' . "\n", get_next_posts_link() );

    echo '</ul></div>' . "\n";

}

将此CSS添加到您的style.css文件中

add this css in your style.css file

.navigation li a,
.navigation li a:hover,
.navigation li.active a,
.navigation li.disabled {
    color: #fff;
    text-decoration:none;
}

.navigation li {
    display: inline;
}

.navigation li a,
.navigation li a:hover,
.navigation li.active a,
.navigation li.disabled {
    background-color: #6FB7E9;
    border-radius: 3px;
    cursor: pointer;
    padding: 12px;
    padding: 0.75rem;
}

.navigation li a:hover,
.navigation li.active a {
    background-color: #3C8DC5;
}

并检查您的阅读设置

And check your reading setting

如果您的博客页面不是静态首页,则更改query_posts参数

Change query_posts parameter if your blog page is not your static front page

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('posts_per_page'=>2,'paged'=>$paged);

query_posts($args);

这篇关于在新主题的帖子中显示分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆