Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在单页WordPress中分页自定义文章类型_Php_Pagination_Wordpress - Fatal编程技术网

Php 在单页WordPress中分页自定义文章类型

Php 在单页WordPress中分页自定义文章类型,php,pagination,wordpress,Php,Pagination,Wordpress,我有两个自定义帖子类型: 学校 教师 还有使用post2post插件连接的。因此,当我显示一个学校单一页面school single.php时,会显示学校信息和在该学校工作的教师列表。一切都很好。 对我来说,我想通过分页显示教师列表,但这不起作用 注册自定义帖子类型 function school() { $labels = array( 'name' => _x( 'schools', 'Post Type General

我有两个自定义帖子类型:

  • 学校
  • 教师
还有使用post2post插件连接的。因此,当我显示一个学校单一页面
school single.php时,
会显示学校信息和在该学校工作的教师列表。一切都很好。 对我来说,我想通过分页显示教师列表,但这不起作用

注册自定义帖子类型

function school() {

    $labels = array(
        'name'                  => _x( 'schools', 'Post Type General Name', 'text_domain' ),
        'singular_name'         => _x( 'school', 'Post Type Singular Name', 'text_domain' ),
        'menu_name'             => __( 'school', 'text_domain' ),
        'name_admin_bar'        => __( 'school', 'text_domain' ),
        'parent_item_colon'     => __( 'Parent Item:', 'text_domain' ),
        'all_items'             => __( 'All Items', 'text_domain' ),
        'add_new_item'          => __( 'Add New Item', 'text_domain' ),
        'add_new'               => __( 'Add New', 'text_domain' ),
        'new_item'              => __( 'New Item', 'text_domain' ),
        'edit_item'             => __( 'Edit Item', 'text_domain' ),
        'update_item'           => __( 'Update Item', 'text_domain' ),
        'view_item'             => __( 'View Item', 'text_domain' ),
        'search_items'          => __( 'Search Item', 'text_domain' ),
        'not_found'             => __( 'Not found', 'text_domain' ),
        'not_found_in_trash'    => __( 'Not found in Trash', 'text_domain' ),
        'items_list'            => __( 'Items list', 'text_domain' ),
        'items_list_navigation' => __( 'Items list navigation', 'text_domain' ),
        'filter_items_list'     => __( 'Filter items list', 'text_domain' ),
    );
    $args = array(
        'label'                 => __( 'school', 'text_domain' ),
        'description'           => __( 'school Description', 'text_domain' ),
        'labels'                => $labels,
        'supports'              => array( 'title', 'editor', ),
        'taxonomies'            => array( 'category', 'post_tag' ),
        'hierarchical'          => false,
        'public'                => true,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'menu_position'         => 5,
        'show_in_admin_bar'     => true,
        'show_in_nav_menus'     => true,
        'can_export'            => true,
        'has_archive'           => true,        
        'exclude_from_search'   => false,
        'publicly_queryable'    => true,
        'capability_type'       => 'page',
    );
    register_post_type( 'school', $args );

}
add_action( 'init', 'school', 0 );
分页代码:


有两种方法可以完成此任务

  • 在分页代码中使用查询字符串这种方法的唯一问题可能是不利于搜索引擎优化的URL
  • 为了使这个搜索引擎优化友好,你必须使用可湿性粉剂重写API,你可以找到我们关于这方面的更多

  • 所以选择了你方便的任何一种方式

    我面临同样的问题。你找到解决办法了吗?如果你有,请回答。非常感谢。
    <?php
    /**
     * WordPress Bootstrap Pagination
     */
    
    add_action( 'wp_loaded', 'the_pagination' );
    
    
    // Bootstrap pagination function
    
    function the_pagination($pages = '', $range = 1) {  
    
        $showitems = ($range * 2) + 1;  
    
        global $paged;
    
        if(empty($paged)) $paged = 1;
    
        if($pages == '') {
    
             global $wp_query; 
    
             $pages = $wp_query->max_num_pages;
    
             if(!$pages) {
                $pages = 1;
             }
    
        }   
    
        if(1 != $pages) {
    
            echo '<div class="text-center">'; 
            echo '<nav><ul class="pagination"><li class="disabled hidden-xs"><span><span aria-hidden="true">Page '.$paged.' of '.$pages.'</span></span></li>';
    
            if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<li><a href='".get_pagenum_link(1)."' aria-label='First'>&laquo;<span class='hidden-xs'> First</span></a></li>";
    
            if($paged > 1 && $showitems < $pages) echo "<li><a href='".get_pagenum_link($paged - 1)."' aria-label='Previous'>&lsaquo;<span class='hidden-xs'> Previous</span></a></li>";
    
    
    
            for ($i=1; $i <= $pages; $i++) {
    
                if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )) {
    
                    echo ($paged == $i)? "<li class=\"active\"><span>".$i." <span class=\"sr-only\">(current)</span></span>
    
                    </li>":"<li><a href='".get_pagenum_link($i)."'>".$i."</a></li>";
    
                }
            }
    
    
    
            if ($paged < $pages && $showitems < $pages) echo "<li><a href=\"".get_pagenum_link($paged + 1)."\"  aria-label='Next'><span class='hidden-xs'>Next </span>&rsaquo;</a></li>";  
    
            if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<li><a href='".get_pagenum_link($pages)."' aria-label='Last'><span class='hidden-xs'>Last </span>&raquo;</a></li>";
    
            echo "</ul></nav>";
            echo "</div>";
    
        }
    
    }
    
    <?php
    
    $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
    $connected = new WP_Query( array(
        'connected_type' => 'school-teacher',
        'connected_items' => get_queried_object_id(),
        'nopaging' => false,
        'posts_per_page' => 1,
        'paged' => $paged,
        ),
    ) );
    
    if ( $connected->have_posts() ) :
        while ( $connected->have_posts() ) : $connected->the_post();
        ....
        endwhile;
    endif;
    
    /* Pagination */
    the_pagination($connected->max_num_pages);
    
    /* Restore original Post Data */
    wp_reset_postdata();