dongyou2714 2018-09-24 21:28 采纳率: 0%
浏览 60

如何为wordpress创建分页?

I have a question regarding pagination on WordPress which is unfamiliar for me, because I haven't worked with this, regarding my code from below I have a file called portfolio.php which it has the code from below:

<?php
/**
 * The template for displaying the portfolio component
 *
 * @package Certy_Theme
 */

// Prevent direct script access.
if ( ! defined( 'ABSPATH' ) ) {
    die( 'No direct script access allowed' );
}
//global
$section = get_query_var( 'section');
$title = $section['title'];
$subtitle = $section['subtitle'];
$menu_id = $section['menu_id'];
$enable_bottom_line = $section['enable_bottom_line'];

$categories = $section['categories'];
$disable_related_projects = $section['disable_related_projects'];
$title_for_related_projects = $section['title_for_related_projects'];

$tax_query = '';
if(!empty($categories)){
    $tax_query = array(
        array(
            'taxonomy' => 'certy_portfolio_category',
            'field' => 'term_id',
            'terms' => $categories
        )
    );
}

$args_posts = array(
    'posts_per_page'   => 2,
    'orderby'          => 'name',
    'order'            => 'ASC',
    'post_type'        => 'certy_portfolio',
    'tax_query'        => $tax_query,
    'post_status'      => 'publish',
    'suppress_filters' => true
);

$all_cat = true;
if(!empty($categories) && count($categories)==1):
    $all_cat = false;
endif;
$enable_bottom_line = $section['enable_bottom_line'];
$brd_class = '';
if(!empty($enable_bottom_line)){
    $brd_class=' brd-btm';
}
?>
<section<?php if(!empty($menu_id)):?> id="<?php echo esc_attr($menu_id);?>"<?php endif;?> class="section padd-box<?php echo esc_attr($brd_class);?>">
    <?php if(!empty($title)):?>
        <h2 class="title-lg text-upper"><?php echo esc_html($title);?></h2>
    <?php endif;?>
    <?php if(!empty($subtitle)):?>
        <h3 class="title-thin text-muted"><?php echo esc_html($subtitle);?></h3>
    <?php endif;?>
    <div class="pf-wrap">
        <?php if($all_cat):?>
            <div class="pf-filter padd-box">
                <button data-filter="*"><?php esc_html_e('All','certy');?></button>
                <?php
                $args = array(
                    'hide_empty'             => false,
                    'include'                => $categories,
                    'number'                 => '',
                );

                $terms = get_terms('certy_portfolio_category', $args);

                foreach($terms as $term):
                    ?>
                    <button data-filter=".pf-<?php echo esc_attr($term->slug);?>"><?php echo esc_html($term->name);?></button>
                <?php endforeach;?>
            </div>
        <?php endif;?>

        <div class="pf-grid">
            <div class="pf-grid-sizer"></div>
            <?php
            $posts_array = get_posts( $args_posts );
            if($posts_array): $i=1;
                foreach($posts_array as $post):setup_postdata($post);
                    $image_size_for_grid = get_field('image_size_for_grid');
                    $popup_slider = get_field('popup_slider');
                    $info_fields = get_field('info_fields');
                    $terms = get_the_terms( get_the_ID(), 'certy_portfolio_category' );
                    $term_slug = '';
                    $term_name = '';
                    if($terms):
                        foreach($terms as $term_item){
                            $term_slug .= ' pf-'.$term_item->slug;
                            $term_name .= $term_item->name.' / ';
                        }
                    endif;
                    ?>

                    <article class="pf-grid-item<?php echo esc_attr($term_slug);?>">
                        <a class="pf-project" href="#pf-popup-<?php the_ID();?>">
                            <figure class="pf-figure">
                                <?php
                                if(has_post_thumbnail()):
                                    the_post_thumbnail( 'certy-portfolio');
                                endif;
                                ?>
                            </figure>

                            <div class="pf-caption text-center">
                                <div class="valign-table">
                                    <div class="valign-cell">
                                        <h2 class="pf-title text-upper"><?php the_title();?></h2>
                                        <?php if(has_excerpt()):?>
                                            <div class="pf-text clear-mrg">
                                                <?php the_excerpt();?>
                                            </div>
                                        <?php endif;?>

                                        <span class="pf-btn btn btn-primary"><?php esc_html_e('View More','certy');?></span>
                                    </div>
                                </div>
                            </div>
                        </a><!-- .pf-project -->

                        <div id="pf-popup-<?php the_ID();?>" class="pf-popup clearfix">
                            <?php if(!empty($popup_slider)):?>
                                <div class="pf-popup-col1">
                                    <div class="pf-popup-media cr-slider" data-init="none">
                                        <?php foreach($popup_slider as $slide):
                                            if($slide['media_type']=='image' && !empty($slide['image'])){
                                                $image = wp_get_attachment_image_src( $slide['image'], 'certy-portfolio-popup');
                                                $data_url = $image[0];
                                            }elseif($slide['media_type']=='iframe' && !empty($slide['iframe_url'])){
                                                $data_url = $slide['iframe_url'];
                                            }elseif($slide['media_type']=='video' && (!empty($slide['mp4_file']) || !empty($slide['webm_file']) || !empty($slide['ogv_file']))){
                                                $data_url = '';
                                                if(!empty($slide['poster_image'])){
                                                    $poster = wp_get_attachment_image_src( $slide['poster_image'], 'certy-portfolio-popup');
                                                    $data_url .='poster: '.$poster[0].', ';
                                                }
                                                if(!empty($slide['mp4_file'])){
                                                    $data_url .='mp4: '.$slide['mp4_file'].', ';
                                                }
                                                if(!empty($slide['webm_file'])){
                                                    $data_url .='webm: '.$slide['webm_file'].', ';
                                                }
                                                if(!empty($slide['ogv_file'])){
                                                    $data_url .='ogv: '.$slide['ogv_file'];
                                                }
                                            }
                                            ?>
                                            <div class="pf-popup-slide">
                                                <div class="pf-popup-embed" data-type="<?php echo esc_attr($slide['media_type'] );?>" data-width="419" data-height="293" data-url="<?php echo esc_attr($data_url);?>"></div>
                                            </div>
                                        <?php endforeach;?>
                                    </div>
                                </div>
                            <?php endif;?>

                            <div class="pf-popup-col2">
                                <div class="pf-popup-info">
                                    <h2 class="pf-popup-title text-upper"><?php the_title();?></h2>
                                    <?php if(!empty($terms)):?>
                                        <p class="text-muted">
                                            <strong><?php echo esc_html($term_name);?></strong>
                                        </p>
                                    <?php endif;?>
                                    <?php if(!empty($info_fields)):?>
                                        <dl class="dl-horizontal">
                                            <?php foreach($info_fields as $field):
                                                if(!empty($field['name'])):
                                                    ?>
                                                    <dt><?php echo esc_html($field['name']);?></dt>
                                                <?php endif;?>
                                                <?php if(!empty($field['value'])):?>
                                                <dd><?php echo wp_kses_post($field['value']);?></dd>
                                            <?php endif;?>
                                            <?php endforeach;?>
                                        </dl>
                                    <?php endif;?>

                                    <?php the_content();?>
                                </div>
                                <?php if(empty($disable_related_projects)):?>
                                    <div class="pf-popup-rel">
                                        <?php if(!empty($title_for_related_projects)):?>
                                            <h2 class="pf-rel-title text-upper"><?php echo esc_html($title_for_related_projects);?></h2>
                                        <?php endif;?>
                                        <?php
                                        $related_term = get_the_terms( get_the_ID(), 'certy_portfolio_category' );
                                        $related_term_id = array();
                                        if(!empty($related_term)):
                                            foreach($related_term as $term):
                                                $related_term_id[] = $term->term_id;
                                            endforeach;
                                            $args_posts = array(
                                                'posts_per_page'   => 2,
                                                'orderby'          => 'date',
                                                'order'            => 'DESC',
                                                'post_type'        => 'certy_portfolio',
                                                'tax_query'        => array(
                                                    array(
                                                        'taxonomy' => 'certy_portfolio_category',
                                                        'field' => 'term_id',
                                                        'terms' => $related_term_id
                                                    )
                                                ),
                                                'post_status'      => 'publish',
                                                'suppress_filters' => true
                                            );
                                            $posts_array = get_posts( $args_posts );
                                            if($posts_array):
                                                ?>
                                                <div class="pf-rel-carousel cr-carousel" data-init="none">
                                                    <?php foreach($posts_array as $post):setup_postdata($post);?>
                                                        <div class="pf-rel-project">
                                                            <a class="pf-rel-href" href="#pf-popup-<?php the_ID();?>">
                                                                <?php
                                                                if(has_post_thumbnail()):
                                                                    the_post_thumbnail( 'certy-portfolio-popup-related');
                                                                endif;
                                                                ?>
                                                                <span class="pf-rel-cover">
                                                                        <span class="btn btn-primary btn-sm"><?php esc_html_e('View','certy');?></span>
                                                                    </span>
                                                            </a>
                                                        </div>
                                                        <?php $i++; endforeach; wp_reset_postdata();?>
                                                </div>
                                            <?php endif;?>
                                        <?php endif;?>
                                    </div>
                                <?php endif;?>
                            </div>
                        </div>
                    </article>
                    <?php $i++; endforeach; wp_reset_postdata();?>
            <?php endif;?>
        </div>
    </div>
</section>

I heard that I need to have as well some info regarding a function. May I know what I have to add in the code from above, and as well in functions.php, to show my pagination ?

Thanks, and I look forward for your answers.

  • 写回答

1条回答 默认 最新

  • doucang5542 2018-09-24 23:40
    关注

    Use paginate_links(). Prepend this before your endforeach.

    <?php
    
    $posts_array = get_posts( $args_posts );
    foreach($posts_array as $post):setup_postdata($post);    
        // You Loop
    
    $args = [
        'base' => get_pagenum_link(1) . '%_%',    
        'current' => max(1, get_query_var('paged')),
        'total' => $posts_array->max_num_pages,
        'prev_text'    => __('« prev'),
        'next_text'    => __('next »')
    ];
    echo paginate_links($args); // Your pagination
    endforeach;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?