【问题标题】:Wordpress Pagination with custom template带有自定义模板的 Wordpress 分页
【发布时间】:2017-10-14 14:33:05
【问题描述】:

我正在使用自定义模板开发一个网站,但在尝试对网站进行分页时遇到了一些问题(149 页,每页 10 个帖子)。我正在使用一个名为 template-tests.php 的页面,其中应显示分页链接和数字,另一个名为 theme-functions.php 的页面应在其中编写分页函数。 这是我得到的:

1- theme-functions.php(仅用于分页的功能):

function pagination_test() {

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( 'page', 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="navigation"><ul>' . "\n";

/** Previous Post Link */
if ( get_previous_posts_link() )
    printf( '<li>%s</li>' . "\n", get_previous_posts_link('<i class="fa fa-angle-left"></i> &nbsp; Previous') );

/** 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('Next &nbsp; <i class="fa fa-angle-right"></i>') );

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

}

2- 这是 template-tests.php 的摘录:

<main id="primary" class="site-main" role="main">

    <?php if (have_posts()) : while (have_posts()) : the_post();

    get_template_part('content', get_post_format());

    endwhile;
    ?>

        <?php if( '' !== $post->post_content ): ?>

            <?php zilla_page_before(); ?>
            <!--BEGIN .page-->
            <article <?php post_class() ?> id="post-<?php the_ID(); ?>">
            <?php zilla_page_start(); ?>

                <!--BEGIN .entry-content -->
                <div class="entry-content">
                    <?php the_content(__('Read more...', 'zilla')); ?>
                    <?php wp_link_pages(array('before' => '<p><strong>'.__('Pages:', 'zilla').'</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
                <!--END .entry-content -->
                </div>

            <?php zilla_page_end(); ?>
            <!--END .page-->
            </article>
            <?php zilla_page_after(); ?>

        <?php endif; ?>

    <?php endwhile; endif; ?>

    <section class="featured content">
        <?php zilla_print_home_featured_post(); ?>
    </section>

    <section class="recent content <?php echo zilla_get_mod_state('homepage_recent_layout') ? esc_attr( zilla_get_mod('homepage_recent_layout') ) : 'grid'; ?>">
        <div class="container">
            <div class="col-md-8">
                <?php zilla_print_home_recent_posts(); ?>
            </div>

            <div id="affix-home" class="text-right col-md-4 hidden-xs hidden-sm affix">
                <br />
                <?php /* Widgetised Area */
                if( is_active_sidebar( 'sidebar' ) )
                 dynamic_sidebar( 'sidebar' ); ?>
            </div>

            <div class="col-md-9 pagination">
                <?php pagination_test(); ?>
            </div>
        </div>
    </section>

<!--END #primary .site-main-->
</main>

我已经阅读了一百万个有关 StackOverflow 的论坛和主题,但似乎没有任何东西可以解决我的问题。 当我在浏览器上打开页面时,分页链接正确显示,但在第一次单击“下一步”后,循环似乎停止了。所以在第一次循环之后,每次我点击“下一步”它都会把我带到第二个页面,这是最奇怪的错误。

编辑#1:包含页面的链接正在正确更新,但页面内容始终是第一页中的内容。例如:如果我单击“下一步”,则链接将更新为“site/page/2”...“site/page/3”...但内容保持不变。

【问题讨论】:

    标签: php html wordpress pagination


    【解决方案1】:

    尝试改变

    $paged = get_query_var('page', 1);

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-07
      • 2013-07-28
      相关资源
      最近更新 更多