简体   繁体   English

表中的 WordPress ACF 中继器分页

[英]WordPress ACF repeater pagination in table

I'm having trouble with displaying pagination bar.我在显示分页栏时遇到问题。 Currently the pagination bar displays at the top of the table on the left hand side.目前,分页栏显示在左侧表格的顶部。 I need it to display after the table is displayed aligned centered.我需要在表格显示居中对齐后显示它。 I think I need to escape the loop before displaying the pagination bar, but I'm not sure how to go about it.我想我需要在显示分页栏之前转义循环,但我不知道该怎么做。

<table id="mytable" class="table table-bordred table-striped">
    <tbody>
    <?php
    /*
    * Paginate Advanced Custom Field repeater
    */
    if ( get_query_var( 'page' ) ) {
        $page = get_query_var( 'page' );
    } else {
        $page = 1;
    }
    // Variables
    $row           = 0;
    $jobs_per_page = 12; // How many jobs to display on each page
    $jobs          = get_field( 'job_list' );
    $total         = count( $jobs );
    $pages         = ceil( $total / $jobs_per_page );
    $min           = ( ( $page * $jobs_per_page ) - $jobs_per_page ) + 1;
    $max           = ( $min + $jobs_per_page ) - 1;
    // ACF Loop
    if ( have_rows( 'job_list' ) ) :
        ?>
        <?php
        while ( have_rows( 'job_list' ) ) :
            the_row();
            $row++;
            // set repeater variables
            $image         = get_sub_field( 'image' );
            $job_title     = get_sub_field( 'job_title' );
            $company_name  = get_sub_field( 'company_name' );
            $location      = get_sub_field( 'location' );
            $job_time      = get_sub_field( 'job_time' );
            $job_date      = get_sub_field( 'job_date' );
            $cta_link_text = get_sub_field( 'cta_link_text' );
            $cta_link      = get_sub_field( 'cta_link' );
            // Ignore this job if $row is lower than $min
            if ( $row < $min ) {
                continue; }
            // Stop loop completely if $row is higher than $max
            if ( $row > $max ) {
                break; }
            ?>
    
                <tr>
                    <td style="padding-top:20px; padding-bottom:20px;"><img src="<?php echo $image; ?>" /></td>
                    <td style="padding-top:16px;"><strong><?php echo $job_title; ?></strong><br><span style="font-size:11px;"><?php echo $company_name; ?></span></td>
                    <td style="padding-top:16px;"><strong><?php echo $location; ?></strong><br><span style="font-size:11px;"><?php echo $job_time; ?></span></td>
                    <td style="padding-top:24px;"><strong><?php echo $job_date; ?></strong></td>
                    <td style="padding-top:20px;" align="center"><button onclick="location.href='<?php echo $cta_link; ?>'" style="background-color:#ea9732; border-color:#ea9732;" class="btn btn-primary btn-md" data-title="Edit" data-toggle="modal" data-target="#edit" ><strong><?php echo $cta_link_text; ?></strong></button></td>
                </tr>
                    
            <?php
        endwhile;
        // Pagination
        echo paginate_links(
            array(
                'base'    => get_permalink() . '%#%' . '/',
                'format'  => '?page=%#%',
                'current' => $page,
                'total'   => $pages,
            )
        );
        ?>
    <?php else : ?>
    No jobs found
    <?php endif; ?>
    
    </tbody>
</table>

You should move the pagination outside of the table and probably rearrange your if , else and while statements.您应该将分页移到表格之外,并可能重新排列您的ifelsewhile语句。

<?php
$page = get_query_var('page') ?: 1;

// Variables
$row           = 0;
$jobs_per_page = 12; // How many jobs to display on each page
$jobs          = get_field('job_list');
$total         = count($jobs);
$pages         = ceil($total / $jobs_per_page);
$min           = (($page * $jobs_per_page) - $jobs_per_page) + 1;
$max           = ($min + $jobs_per_page) - 1;
// ACF Loop
if (have_rows('job_list')) :
?>
<table id="mytable" class="table table-bordred table-striped">
    <tbody>   
    <?php
    while (have_rows('job_list')) :
        the_row();
        $row++;
        // set repeater variables
        $image         = get_sub_field('image');
        $job_title     = get_sub_field('job_title');
        $company_name  = get_sub_field('company_name');
        $location      = get_sub_field('location');
        $job_time      = get_sub_field('job_time');
        $job_date      = get_sub_field('job_date');
        $cta_link_text = get_sub_field('cta_link_text');
        $cta_link      = get_sub_field('cta_link');
        // Ignore this job if $row is lower than $min
        if ($row < $min) {
            continue;
        }
        // Stop loop completely if $row is higher than $max
        if ($row > $max) {
            break;
        }
    ?>

        <tr>
            <td style="padding-top:20px; padding-bottom:20px;"><img src="<?php echo $image; ?>" /></td>
            <td style="padding-top:16px;"><strong><?php echo $job_title; ?></strong><br><span style="font-size:11px;"><?php echo $company_name; ?></span></td>
            <td style="padding-top:16px;"><strong><?php echo $location; ?></strong><br><span style="font-size:11px;"><?php echo $job_time; ?></span></td>
            <td style="padding-top:24px;"><strong><?php echo $job_date; ?></strong></td>
            <td style="padding-top:20px;" align="center"><button onclick="location.href='<?php echo $cta_link; ?>'" style="background-color:#ea9732; border-color:#ea9732;" class="btn btn-primary btn-md" data-title="Edit" data-toggle="modal" data-target="#edit"><strong><?php echo $cta_link_text; ?></strong></button></td>
        </tr>

    <?php endwhile; ?>
    </tbody>
</table>
<?php
// Pagination
echo paginate_links(
    array(
        'base'    => get_permalink() . '%#%' . '/',
        'format'  => '?page=%#%',
        'current' => $page,
        'total'   => $pages,
    )
);

else : ?>

<p>No jobs found</p>

<?php 
endif;

If you cant do that or you don't want do that from some reason, wrap the pagination inside a table row and a data cell with colspan set to 4 to make it take all the table width如果您不能这样做,或者由于某种原因不想这样做,请将分页包装在表格行和colspan设置为 4 的数据单元格中,以使其占据所有表格宽度

<tr>
    <td colspan="4">
        <?php echo paginate_links(array(
            'base'    => get_permalink() . '%#%' . '/',
            'format'  => '?page=%#%',
            'current' => $page,
            'total'   => $pages,
        )); ?>
    </td>
</tr>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM