当the_content存在时,ACF分页不起作用

时间:2017-02-10 16:42:25

标签: wordpress pagination advanced-custom-fields

我在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; ?>

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

由于

1 个答案:

答案 0 :(得分:0)

解决了问题!我必须在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(); ?>