1
votes

I have two custom posts type :

  • School
  • teacher

and there are connected using post 2 post plugin. so when I display a school single page school-single.php, it's show me school informations and list of teachers working in this school. everything working great. for me I want to display the list of teachers by pagination and this not working.

Register Custom Post Type

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 );

Pagination code : By wp_bs_pagination

<?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>";

    }

}

The query that list teachers in school-single.php

<?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();

The pagination displays correctly:

Pagination

the url of the page 2 is : wwww.mywebsite.com/school/school_name/page/2 when I click on it redirect me to same page. wwww.mywebsite.com/school/school_name.

I think the single page doesn't accept parameter in url. Anyway can someone propose a solution !?

1
i face the same problem. did you find the solutions? if you did then give answer please. thank youNahid Hasan

1 Answers

1
votes

There are 2 way by which you can get this done

  1. Using Query String inside the Pagination code only problem with this approach could be not seo friendly URL
  2. to make this SEO friendly you have to use the WP Rewrite API you can find our more about this WP Rewrite API

So chose the Either way convenient to you