【问题标题】:ACF Pagination not working when the_content is present当 the_content 存在时,ACF 分页不起作用
【发布时间】:2017-02-12 19:09:15
【问题描述】:

我在 Wordpress 中有这个自定义模板,其中包含一个对 ACF 字段进行分页的脚本。以下是源代码:

<?php get_header(); ?>

<div class="container">
    <div class="row">
        <div class="col-xs-12">
            <h1 class="pagetitle"><?php the_title(); ?></h1>
        </div> <!-- col -->
    </div> <!-- row -->
</div> <!-- container -->

<?php

if( get_query_var('page') ) {
    $page = get_query_var( 'page' );
} else {
    $page = 1;
}

$row              = 0;
$files_per_page     = 30; // How many images to display on each page
$files              = get_field( 'fisier' );
$total            = count( $files );
$pages            = ceil( $total / $files_per_page );
$min              = ( ( $page * $files_per_page ) - $files_per_page ) + 1;
$max              = ( $min + $files_per_page ) - 1;

?>

<div class="container">
    <div class="row">
        <div class="col-md-8">
            <section id="content">
                <div class="wrapper">

                    <?php while ( have_posts() ) : the_post(); ?>
                    <?php the_content(); ?>
                    <?php endwhile; ?>

                    <?php if( have_rows('fisier') ): ?>
                    <div class="article-files">
                        <h4>Files</h4>
                        <div class="row">
                            <?php while ( have_rows('fisier') ) : the_row();
                                    $row++;
                                    if($row < $min) { continue; }
                                    if($row > $max) { break; }
                            ?>
                                <div class="col-xs-12 file">
                                <?php $x = 0; ?>
                                <?php $file = get_sub_field('link'); if( $file ): ?>
                                    <a href="<?php echo $file; ?>" class="file-title" target="_blank"><i class="fa fa-angle-right" aria-hidden="true"></i> <?php the_sub_field('nume_fisier'); ?></a>
                                    <div class="file-description"><?php the_sub_field('descriere'); ?></div>
                                    <?php $x = 1; ?>
                                <?php endif; ?>
                                <?php $file = get_sub_field('fisier_intern'); if( $file ): ?>
                                    <a href="<?php echo $file; ?>" class="file-title" target="_blank"><i class="fa fa-angle-right" aria-hidden="true"></i> <?php the_sub_field('nume_fisier'); ?></a>
                                    <div class="file-description"><?php the_sub_field('descriere'); ?></div>
                                    <?php $x = 1; ?>
                                <?php endif; ?>
                                <?php if( $x == 0 ): ?>
                                    <a href="#" class="file-title"><i class="fa fa-angle-right" aria-hidden="true"></i> <?php the_sub_field('nume_fisier'); ?></a>
                                    <div class="file-description"><?php the_sub_field('descriere'); ?></div>
                                    <?php $x = 1; ?>
                                <?php endif; ?>
                                </div>
                            <?php endwhile; ?>
                        </div>
                        <div class="file-pagination">
                        <?php
                        echo paginate_links( array(
                        'base' => get_permalink() . '%#%' . '/',
                        'format' => '?page=%#%',
                        'current' => $page,
                            'show_all' => false,
                            'prev_text' => __('«'),
                            'next_text' => __('»'),
                        'total' => $pages
                      ));
                        ?>
                        </div> <!-- file-pagination -->
                    </div>
                    <?php endif; ?>

                </div> <!-- wrapper -->
            </section> <!-- content -->
        </div> <!-- col -->
        <div class="col-md-4">
            <aside id="sidebar">
                <?php get_sidebar(); ?>
            </aside> <!-- aside -->
        </div> <!-- col -->
    </div> <!-- row -->
</div> <!-- container -->

<?php get_footer(); ?>

上面的代码不显示分页链接,但如果我删除这部分:

<?php while ( have_posts() ) : the_post(); ?>
<?php the_content(); ?>
<?php endwhile; ?>

一切都很完美。关于如何解决此问题的任何想法?

谢谢

【问题讨论】:

  • 抱歉,如果这没有帮助(我在移动设备上,很难正确阅读代码),但在循环输出后我没有看到重置功能。也许在循环和分页之间放置:wp_reset_postdata(); 可能会恢复函数的原始查询

标签: wordpress pagination advanced-custom-fields


【解决方案1】:

解决了问题!我不得不在the_content()之后移动分页设置,如下所示:

<?php get_header(); ?>

<div class="container">
    <div class="row">
        <div class="col-xs-12">
            <h1 class="pagetitle"><?php the_title(); ?></h1>
        </div> <!-- col -->
    </div> <!-- row -->
</div> <!-- container -->

<div class="container">
    <div class="row">
        <div class="col-md-8">
            <section id="content">
                <div class="wrapper">

                    <?php while ( have_posts() ) : the_post(); ?>
                    <?php the_content(); ?>
                    <?php endwhile; ?>


                    <?php

                    if( get_query_var('page') ) {
                        $page = get_query_var( 'page' );
                    } else {
                        $page = 1;
                    }

                    $row              = 0;
                    $files_per_page     = 30; // How many images to display on each page
                    $files              = get_field( 'fisier' );
                    $total            = count( $files );
                    $pages            = ceil( $total / $files_per_page );
                    $min              = ( ( $page * $files_per_page ) - $files_per_page ) + 1;
                    $max              = ( $min + $files_per_page ) - 1;

                    ?>

                    <?php if( have_rows('fisier') ): ?>
                    <div class="article-files">
                        <h4>Files</h4>
                        <div class="row">
                            <?php while ( have_rows('fisier') ) : the_row();
                                    $row++;
                                    if($row < $min) { continue; }
                                    if($row > $max) { break; }
                            ?>
                                <div class="col-xs-12 file">
                                <?php $x = 0; ?>
                                <?php $file = get_sub_field('link'); if( $file ): ?>
                                    <a href="<?php echo $file; ?>" class="file-title" target="_blank"><i class="fa fa-angle-right" aria-hidden="true"></i> <?php the_sub_field('nume_fisier'); ?></a>
                                    <div class="file-description"><?php the_sub_field('descriere'); ?></div>
                                    <?php $x = 1; ?>
                                <?php endif; ?>
                                <?php $file = get_sub_field('fisier_intern'); if( $file ): ?>
                                    <a href="<?php echo $file; ?>" class="file-title" target="_blank"><i class="fa fa-angle-right" aria-hidden="true"></i> <?php the_sub_field('nume_fisier'); ?></a>
                                    <div class="file-description"><?php the_sub_field('descriere'); ?></div>
                                    <?php $x = 1; ?>
                                <?php endif; ?>
                                <?php if( $x == 0 ): ?>
                                    <a href="#" class="file-title"><i class="fa fa-angle-right" aria-hidden="true"></i> <?php the_sub_field('nume_fisier'); ?></a>
                                    <div class="file-description"><?php the_sub_field('descriere'); ?></div>
                                    <?php $x = 1; ?>
                                <?php endif; ?>
                                </div>
                            <?php endwhile; ?>
                        </div>
                        <div class="file-pagination">
                        <?php
                        echo paginate_links( array(
                        'base' => get_permalink() . '%#%' . '/',
                        'format' => '?page=%#%',
                        'current' => $page,
                            'show_all' => false,
                            'prev_text' => __('«'),
                            'next_text' => __('»'),
                        'total' => $pages
                      ));
                        ?>
                        </div> <!-- file-pagination -->
                    </div>
                    <?php endif; ?>

                </div> <!-- wrapper -->
            </section> <!-- content -->
        </div> <!-- col -->
        <div class="col-md-4">
            <aside id="sidebar">
                <?php get_sidebar(); ?>
            </aside> <!-- aside -->
        </div> <!-- col -->
    </div> <!-- row -->
</div> <!-- container -->

<?php get_footer(); ?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-15
    • 1970-01-01
    • 2019-10-17
    • 2019-09-10
    • 1970-01-01
    • 1970-01-01
    • 2015-12-23
    • 1970-01-01
    相关资源
    最近更新 更多