如何在wordpress中的页面代码中创建分页?

时间:2015-11-30 10:42:26

标签: wordpress wordpress-theming

大家好我是wordpress的新手我想在页面中创建分页,但仍然不能。我尝试在youtube上下载插件或在Google中添加代码和serch但我还是不能帮助我的人吗?

这是我的网页代码:

 <?php 

    $zerif_total_posts = get_option('posts_per_page'); /* number of latest posts to show */

    if( !empty($zerif_total_posts) && ($zerif_total_posts > 0) ):

        echo '<section class="latest-news" id="latestnews">';

            echo '<div class="container">';

                /* SECTION HEADER */

                echo '<div class="section-header">';

                    $zerif_latestnews_title = get_theme_mod('zerif_latestnews_title');

                    /* title */
                    if( !empty($zerif_latestnews_title) ):

                        echo '<h2 class="dark-text">' . $zerif_latestnews_title . '</h2>';

                    else:

                        echo '<h2 class="dark-text">' . __('Our Client','zerif-lite') . '</h2>';

                    endif;

                /* our Client */

                    if(is_active_sidebar( 'menu_client' )):
                    echo '<div id="menu_client">';

                    dynamic_sidebar( 'menu_client' );
                    echo '</div>';
                    endif;

                    /* subtitle */
                    $zerif_latestnews_subtitle = get_theme_mod('zerif_latestnews_subtitle');

                    if( !empty($zerif_latestnews_subtitle) ):

                        echo '<div class="dark-text section-legend">'.$zerif_latestnews_subtitle.'</div>';

                    endif;

                echo '</div><!-- END .section-header -->';

                echo '<div class="clear"></div>';

                echo '<div id="carousel-homepage-latestnews" class="carousel slide" data-ride="carousel">';

                    /* Wrapper for slides */

                    echo '<div class="carousel-inners" role="listbox">';


                        $zerif_latest_loop = new WP_Query( array( 'post_type' => 'post', 'posts_per_page' => $zerif_total_posts, 'order' => 'DESC','ignore_sticky_posts' => true ) );

                        $newSlideActive = '<div class="item active">';
                        $newSlide       = '<div class="item">';
                        $newSlideClose  = '<div class="clear"></div></div>';
                        $i_latest_posts= 0;
                        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                        $page_id = get_the_ID(); 
                        if ( $zerif_latest_loop->have_posts() ) :

                            while ( $zerif_latest_loop->have_posts() ) : $zerif_latest_loop->the_post();

                                $i_latest_posts++;

                                if ( !wp_is_mobile() ){

                                        if($i_latest_posts == 1){
                                            echo $newSlideActive;
                                        }
                                        else if($i_latest_posts % 4 == 1){
                                            echo $newSlide;
                                        }

                                        echo '<div class="col-sm-3 latestnews-box">';

                                            echo '<div class="latestnews-img">';

                                                echo '<a class="latestnews-img-a" href="'.get_permalink().'" title="'.get_the_title().'">';

                                                    if ( has_post_thumbnail() ) :
                                                        the_post_thumbnail();
                                                    else:
                                                        echo '<img src="'.esc_url( get_template_directory_uri() ).'/images/blank-latestposts.png" alt="'.get_the_title().'" />';
                                                    endif; 

                                                echo '</a>';

                                            echo '</div>';

                                            echo '<div class="latesnews-content">';

                                                echo '<h3 class="latestnews-title"><a href="'.get_permalink().'" title="'.get_the_title().'">'.get_the_title().'</a></h3>';

                                                the_excerpt();

                                            echo '</div>';

                                        echo '</div><!-- .latestnews-box"> -->';

                                        /* after every four posts it must closing the '.item' */
                                        if($i_latest_posts % 4 == 0){
                                            echo $newSlideClose;
                                        }

                                } else {

                                    if( $i_latest_posts == 1 ) $active = 'active'; else $active = ''; 

                                    echo '<div class="item '.$active.'">';
                                        echo '<div class="col-md-3 latestnews-box">';
                                            echo '<div class="latestnews-img">';
                                                echo '<a class="latestnews-img-a" href="'.get_permalink().'" title="'.get_the_title().'">';
                                                    if ( has_post_thumbnail() ) :
                                                        the_post_thumbnail();
                                                    else:
                                                        echo '<img src="'.esc_url( get_template_directory_uri() ).'/images/blank-latestposts.png" alt="'.get_the_title().'" />';
                                                    endif; 
                                                echo '</a>';
                                            echo '</div>';
                                            echo '<div class="latesnews-content">';
                                                echo '<h3 class="latestnews-title"><a href="'.get_the_permalink().'" title="'.get_the_title().'">'.get_the_title().'</a></h3>';
                                                the_excerpt();
                                            echo '</div>';
                                        echo '</div>';
                                    echo '</div>';
                                }

                            endwhile;               
                        endif;  
                        $max_num_pages = $loop->max_num_pages;
                        sofg_pagination($max_num_pages,$paged,$page_id);
                        if ( !wp_is_mobile() ) {
                            // if there are less than 10 posts
                            if($i_latest_posts % 4!=0){
                                echo $newSlideClose;
                            }
                        }
                        wp_reset_postdata(); 

                    echo '</div><!-- .carousel-inner -->';

                    /* Controls */
                    echo '<a class="left carousel-control" href="#carousel-homepage-latestnews" role="button" data-slide="prev">';

                    echo '</a>';
                    echo '<a class="right carousel-control" href="#carousel-homepage-latestnews" role="button" data-slide="next">';

                    echo '</a>';
                echo '</div><!-- #carousel-homepage-latestnews -->';

            echo '</div><!-- .container -->';
        echo '</section>';

endif;

?>

2 个答案:

答案 0 :(得分:0)

function.php

中创建功能
function sofg_pagination($max_num_pages,$paged,$page_id){
    if($max_num_pages > 1){
    echo '<div class="post-wrap pgns">';
    echo '<ul class="pagination_list">';
    echo '<li><a href="'.get_permalink($page_id).'page/1">First</a></li>';
    for($i=1; $i<= $max_num_pages; $i++){
        if($paged==$i){ 
            echo '<li class="active"><a href="'.get_permalink($page_id).'page/'.$i.'">'.$i.'</a></li>';
        }
        else{ echo '<li><a href="'.get_permalink($page_id).'page/'.$i.'">'.$i.'</a></li>'; }
    }
    echo '<li><a href="'.get_permalink($page_id).'page/'.$max_num_pages.'">Last</a></li>';
    echo '</ul></div>';
    }
}  

获取您的listing page

分页

定义前循环

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$page_id = get_the_ID(); 

使用循环后

$max_num_pages_1 = $max_num_pages->max_num_pages;
sofg_pagination($max_num_pages_1,$paged,$page_id);

enter image description here

请参阅分页http://hiddenwhy.igexsolutions.com/blog/

答案 1 :(得分:0)

尝试它的工作......

$zerif_total_posts = get_option('posts_per_page'); /* number of latest posts to show */
            if( !empty($zerif_total_posts) && ($zerif_total_posts > 0) ):
                echo '<section class="latest-news" id="latestnews">';
                    echo '<div class="container">';
                        /* SECTION HEADER */
                        echo '<div class="section-header">';
                            $zerif_latestnews_title = get_theme_mod('zerif_latestnews_title');
                            /* title */
                            if( !empty($zerif_latestnews_title) ):
                                echo '<h2 class="dark-text">' . $zerif_latestnews_title . '</h2>';
                            else:
                                echo '<h2 class="dark-text">' . __('Our Client','zerif-lite') . '</h2>';
                            endif;
                        /* our Client */
                            if(is_active_sidebar( 'menu_client' )):
                            echo '<div id="menu_client">';
                            dynamic_sidebar( 'menu_client' );
                            echo '</div>';
                            endif;
                            /* subtitle */
                            $zerif_latestnews_subtitle = get_theme_mod('zerif_latestnews_subtitle');
                            if( !empty($zerif_latestnews_subtitle) ):
                                echo '<div class="dark-text section-legend">'.$zerif_latestnews_subtitle.'</div>';
                            endif;
                        echo '</div><!-- END .section-header -->';
                        echo '<div class="clear"></div>';
                        echo '<div id="carousel-homepage-latestnews" class="carousel slide" data-ride="carousel">';
                            /* Wrapper for slides */
                            echo '<div class="carousel-inners" role="listbox">';
                            echo $zerif_total_posts;
                                $zerif_latest_loop = new WP_Query( array( 'post_type' => 'post', 'posts_per_page' => $zerif_total_posts, 'order' => 'DESC','ignore_sticky_posts' => true ) );
                                $newSlideActive = '<div class="item active">';
                                $newSlide       = '<div class="item">';
                                $newSlideClose  = '<div class="clear"></div></div>';
                                $i_latest_posts= 0;
                                $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                                $page_id = get_the_ID(); 
                                if ( $zerif_latest_loop->have_posts() ) :
                                    while ( $zerif_latest_loop->have_posts() ) : $zerif_latest_loop->the_post();
                                        $i_latest_posts++;
                                        if ( !wp_is_mobile() ){
                                                if($i_latest_posts == 1){
                                                    echo $newSlideActive;
                                                }
                                                else if($i_latest_posts % 4 == 1){
                                                    echo $newSlide;
                                                }
                                                echo '<div class="col-sm-3 latestnews-box">';
                                                    echo '<div class="latestnews-img">';
                                                        echo '<a class="latestnews-img-a" href="'.get_permalink().'" title="'.get_the_title().'">';
                                                            if ( has_post_thumbnail() ) :
                                                                the_post_thumbnail();
                                                            else:
                                                                echo '<img src="'.esc_url( get_template_directory_uri() ).'/images/blank-latestposts.png" alt="'.get_the_title().'" />';
                                                            endif; 
                                                        echo '</a>';
                                                    echo '</div>';
                                                    echo '<div class="latesnews-content">';
                                                        echo '<h3 class="latestnews-title"><a href="'.get_permalink().'" title="'.get_the_title().'">'.get_the_title().'</a></h3>';
                                                        the_excerpt();
                                                    echo '</div>';
                                                echo '</div><!-- .latestnews-box"> -->';
                                                /* after every four posts it must closing the '.item' */
                                                if($i_latest_posts % 4 == 0){
                                                    echo $newSlideClose;
                                                }
                                        } else {
                                            if( $i_latest_posts == 1 ) $active = 'active'; else $active = ''; 
                                            echo '<div class="item '.$active.'">';
                                                echo '<div class="col-md-3 latestnews-box">';
                                                    echo '<div class="latestnews-img">';
                                                        echo '<a class="latestnews-img-a" href="'.get_permalink().'" title="'.get_the_title().'">';
                                                            if ( has_post_thumbnail() ) :
                                                                the_post_thumbnail();
                                                            else:
                                                                echo '<img src="'.esc_url( get_template_directory_uri() ).'/images/blank-latestposts.png" alt="'.get_the_title().'" />';
                                                            endif; 
                                                        echo '</a>';
                                                    echo '</div>';
                                                    echo '<div class="latesnews-content">';
                                                        echo '<h3 class="latestnews-title"><a href="'.get_the_permalink().'" title="'.get_the_title().'">'.get_the_title().'</a></h3>';
                                                        the_excerpt();
                                                    echo '</div>';
                                                echo '</div>';
                                            echo '</div>';
                                        }
                                    endwhile;               
                                endif;  
                                $max_num_pages = $zerif_latest_loop->max_num_pages;
                                sofg_pagination($max_num_pages,$paged,$page_id);
                                if ( !wp_is_mobile() ) {
                                    // if there are less than 10 posts
                                    if($i_latest_posts % 4!=0){
                                        echo $newSlideClose;
                                    }
                                }
                                wp_reset_postdata(); 
                            echo '</div><!-- .carousel-inner -->';
                            /* Controls */
                            echo '<a class="left carousel-control" href="#carousel-homepage-latestnews" role="button" data-slide="prev">';
                            echo '</a>';
                            echo '<a class="right carousel-control" href="#carousel-homepage-latestnews" role="button" data-slide="next">';
                            echo '</a>';
                        echo '</div><!-- #carousel-homepage-latestnews -->';
                    echo '</div><!-- .container -->';
                echo '</section>';
        endif;