WordPress pagination fuction in shortcode always displayed on the top - php
WordPress pagination function in shortcode always displayed on the top. Please have a look on below code
/*-------------------------------------------------------------------------*/
/* Custom Pagination */
/*-------------------------------------------------------------------------*/
function suareztheme_pagination($pages = '', $range = 2){
$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 ){
$pagination_html .= '<div class="pagination">';
if( $paged > 2 && $paged > $range + 1 && $showitems < $pages ){
$pagination_html .= '«';
}
if( $paged > 1 && $showitems < $pages ){
$pagination_html .= '‹';
}
for ( $i = 1; $i <= $pages; $i++ ){
if ( 1 != $pages && ( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )){
if ( $paged == $i ){
$pagination_html .= '<span class="current">' . $i . '</span>';
} else{
$pagination_html .= '' . $i . '';
}
}
}
if ( $paged < $pages && $showitems < $pages ){
$pagination_html .= '›';
}
if ( $paged < $pages - 1 && $paged + $range - 1 < $pages && $showitems < $pages ){
$pagination_html .= '»';
}
$pagination_html .= '</div>';
return $pagination_html;
}
}
Then I called it in the shortcode function.
/*-------------------------------------------------------------------------*/
/* Grid Shortcode */
/*-------------------------------------------------------------------------*/
function RecentBlog($atts, $content = null) {
extract(shortcode_atts(array(
"comments" => 'true',
"date" => 'true',
"columns" => '4',
"limit" => '-1',
"title" => 'true',
"description" => 'true',
"cat_slug" => '',
"post_type" => '',
"excerpt_length" => '15',
"readmore_text" => '',
"pagination" => 'false'
), $atts));
global $post;
$postformat = get_post_format();
....
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
......
if (have_posts()) : while (have_posts()) : the_post();
$postformat = get_post_format();
if( $postformat == "" ) $postformat="standard";
$protected = "";
$portfoliogrid .= '<div class="' . $column_no . ' post grid-post post-' . $postformat . '">';
.....////
$portfoliogrid .= '<div class="summary-info">';
.......
$portfoliogrid .='</div>';
// If either of title and description needs to be displayed.
if ( $title == "true" || $description == "true" ) {
$portfoliogrid .='<div class="work-details">';
......
$portfoliogrid .='</div>';
}
$portfoliogrid .='</div>';
endwhile; endif;
if ( $pagination == "true" ){
if ( isset( $additional_loop ) ){
echo suareztheme_pagination( $additional_loop->max_num_pages );
} else {
echo suareztheme_pagination();
}
if ( function_exists("suareztheme_pagination") ) {
} else {
next_posts_link('«« Older Posts');
previous_posts_link('Newer Posts »»');
}
}
wp_reset_query();
return $portfoliogrid;
}
add_shortcode( "recentblog", "RecentBlog" );
The above code is working fine , but it has one problem, when it display in specific page, it always displayed on the top regardless it's place. So appreciate your support to help me, thanks in advance.
In the shortcode callback, you need to concatenate and return the html, e.g.:
$portfoliogrid .= suareztheme_pagination();
Note that you'll have to use get_next_posts_link() and get_previous_posts_link() instead of next_posts_link() and previous_posts_link().
Related
How to use PHP pagination in array's
Since I am trying to learn more about PHP I would like to add Pagination to array's I have a JSON output that I can read and echo via a foreach. But I am not able to show 10 or 20 records. I have used the code from this page: But I miss the place where I can store the echo for the array. $persons = '[ {"FrstName":"Henry","Middlename":"","LastName":"Walton","Online":true,"DeptId":"4"}, {"FrstName":"Klaus","Middlename":"","LastName":"Mikaelson","Online":true,"DeptId":"2"}, {"FrstName":"Kylo","Middlename":"","LastName":"Ren","Online":false,"DeptId":"4"}, {"FrstName":"Stan","Middlename":"","LastName":"Lee","Online":false,"DeptId":"3"}, {"FrstName":"Kevin","Middlename":"","LastName":"McNally","Online":false,"DeptId":"3"}, {"FrstName":"Katherine","Middlename":"","LastName":"Pierce","Online":false,"DeptId":"2"}, {"FrstName":"Clint","Middlename":"","LastName":"Barton","Online":true,"DeptId":"3"}, {"FrstName":"Avery","Middlename":"","LastName":"Walton","Online":true,"DeptId":"4"}, {"FrstName":"Peter","Middlename":"","LastName":"Kap","Online":true,"DeptId":"2"}, {"FrstName":"Denzo","Middlename":"","LastName":"Falc","Online":false,"DeptId":"4"}, {"FrstName":"Eveline","Middlename":"","LastName":"Benzel","Online":false,"DeptId":"3"}, {"FrstName":"Bill","Middlename":"","LastName":"Libuz","Online":false,"DeptId":"3"}, {"FrstName":"April","Middlename":"","LastName":"Gonzo","Online":false,"DeptId":"2"}, {"FrstName":"Harry","Middlename":"","LastName":"Geraldson","Online":true,"DeptId":"3"}, {"FrstName":"Heraldson","Middlename":"","LastName":"McGree","Online":false,"DeptId":"3"} {"FrstName":"Abraham","Middlename":"","LastName":"Retz","Online":true,"DeptId":"4"}, {"FrstName":"June","Middlename":"","LastName":"Pharee","Online":true,"DeptId":"2"}, {"FrstName":"Anthony","Middlename":"","LastName":"Gonzales","Online":false,"DeptId":"4"}, {"FrstName":"Billy","Middlename":"","LastName":"Scott","Online":false,"DeptId":"3"}, {"FrstName":"Anika","Middlename":"","LastName":"Rose","Online":false,"DeptId":"3"}, {"FrstName":"Kristen","Middlename":"","LastName":"Fontana","Online":false,"DeptId":"2"}, {"FrstName":"Olivia","Middlename":"","LastName":"Menzel","Online":true,"DeptId":"3"}, {"FrstName":"Mark","Middlename":"van","LastName":"Gad","Online":false,"DeptId":"3"} {"FrstName":"Hope","Middlename":"van","LastName":"Dyne","Online":false,"DeptId":"3"} ]'; $page = ! empty( $_GET['page'] ) ? (int) $_GET['page'] : 1; $total = count( $persons ); //total items in array $limit = 10; //per page $totalPages = ceil( $total/ $limit ); //calculate total pages $page = max($page, 1); //get 1 page when $_GET['page'] <= 0 $page = min($page, $totalPages); //get last page when $_GET['page'] > $totalPages $offset = ($page - 1) * $limit; if( $offset < 0 ) $offset = 0; $yourDataArray = array_slice( $persons, $offset, $limit ); $link = 'index.php?page=%d'; $pagerContainer = '<div style="width: 300px;">'; if( $totalPages != 0 ) { if( $page == 1 ) { $pagerContainer .= ''; } else { $pagerContainer .= sprintf( ' « prev page', $page - 1 ); } $pagerContainer .= ' <span> page <strong>' . $page . '</strong> from ' . $totalPages . '</span>'; if( $page == $totalPages ) { $pagerContainer .= ''; } else { $pagerContainer .= sprintf( ' next page » ', $page + 1 ); } } $pagerContainer .= '</div>'; echo $pagerContainer; I would like to know how I can fix this to use pagination for my array.
You need to json_decode() your JSON first to access array methods (count(), array_slice()). Also, your JSON data was invalid, missing some commas, fixed that. <?php $persons = '[ {"FrstName":"Henry","Middlename":"","LastName":"Walton","Online":true,"DeptId":"4"}, {"FrstName":"Klaus","Middlename":"","LastName":"Mikaelson","Online":true,"DeptId":"2"}, {"FrstName":"Kylo","Middlename":"","LastName":"Ren","Online":false,"DeptId":"4"}, {"FrstName":"Stan","Middlename":"","LastName":"Lee","Online":false,"DeptId":"3"}, {"FrstName":"Kevin","Middlename":"","LastName":"McNally","Online":false,"DeptId":"3"}, {"FrstName":"Katherine","Middlename":"","LastName":"Pierce","Online":false,"DeptId":"2"}, {"FrstName":"Clint","Middlename":"","LastName":"Barton","Online":true,"DeptId":"3"}, {"FrstName":"Avery","Middlename":"","LastName":"Walton","Online":true,"DeptId":"4"}, {"FrstName":"Peter","Middlename":"","LastName":"Kap","Online":true,"DeptId":"2"}, {"FrstName":"Denzo","Middlename":"","LastName":"Falc","Online":false,"DeptId":"4"}, {"FrstName":"Eveline","Middlename":"","LastName":"Benzel","Online":false,"DeptId":"3"}, {"FrstName":"Bill","Middlename":"","LastName":"Libuz","Online":false,"DeptId":"3"}, {"FrstName":"April","Middlename":"","LastName":"Gonzo","Online":false,"DeptId":"2"}, {"FrstName":"Harry","Middlename":"","LastName":"Geraldson","Online":true,"DeptId":"3"}, {"FrstName":"Heraldson","Middlename":"","LastName":"McGree","Online":false,"DeptId":"3"}, {"FrstName":"Abraham","Middlename":"","LastName":"Retz","Online":true,"DeptId":"4"}, {"FrstName":"June","Middlename":"","LastName":"Pharee","Online":true,"DeptId":"2"}, {"FrstName":"Anthony","Middlename":"","LastName":"Gonzales","Online":false,"DeptId":"4"}, {"FrstName":"Billy","Middlename":"","LastName":"Scott","Online":false,"DeptId":"3"}, {"FrstName":"Anika","Middlename":"","LastName":"Rose","Online":false,"DeptId":"3"}, {"FrstName":"Kristen","Middlename":"","LastName":"Fontana","Online":false,"DeptId":"2"}, {"FrstName":"Olivia","Middlename":"","LastName":"Menzel","Online":true,"DeptId":"3"}, {"FrstName":"Mark","Middlename":"van","LastName":"Gad","Online":false,"DeptId":"3"}, {"FrstName":"Hope","Middlename":"van","LastName":"Dyne","Online":false,"DeptId":"3"} ]'; // Make the JSON an array, so count() and array_slice() work $persons = json_decode($persons, true); $page = ! empty( $_GET['page'] ) ? (int) $_GET['page'] : 1; $total = count( $persons ); //total items in array // $limit = 10; //per page // Set limit to 3 for testing: $limit = 3; $totalPages = ceil( $total/ $limit ); //calculate total pages $page = max($page, 1); //get 1 page when $_GET['page'] <= 0 $page = min($page, $totalPages); //get last page when $_GET['page'] > $totalPages // Uncomment this for testing // $page = 2; $offset = ($page - 1) * $limit; if( $offset < 0 ) $offset = 0; $yourDataArray = array_slice( $persons, $offset, $limit ); $link = 'index.php?page=%d'; $pagerContainer = '<div style="width: 300px;">'; if( $totalPages != 0 ) { if( $page == 1 ) { $pagerContainer .= ''; } else { $pagerContainer .= sprintf( ' « prev page', $page - 1 ); } $pagerContainer .= ' <span> page <strong>' . $page . '</strong> from ' . $totalPages . '</span>'; if( $page == $totalPages ) { $pagerContainer .= ''; } else { $pagerContainer .= sprintf( ' next page » ', $page + 1 ); } } $pagerContainer .= '</div>'; echo $pagerContainer; foreach($yourDataArray as $person) { echo "\n"; echo "First name: " . $person['FrstName']; echo " - Middle name: " . $person['Middlename']; echo " - Last name: " . $person['LastName']; echo " - Online: " . $person['Online']; echo " - Dept: " . $person['DeptId']; } https://3v4l.org/8hR84
Pagination with Custom Post Type and Taxonomies return 404
I've been trying to fix this issue since last week but I can't get it done. The problem itself sounds quite simple, my pagination on WordPress works just fine when it's not dealing with taxonomies, for example: www.example.com/post_type/2 but if I try to use a taxonomy, I can't go past page 1 (www.example.com/taxonomy/term/2). Custom Post Type = noticias and Taxonomy = assunto, so I don't think the name is a conflicting factor here. $custom_query_args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; $args = array( 'post_type' => 'noticias', 'paged' => $custom_query_args['paged'], 'posts_per_page' => 5); $loop = new WP_Query( $args ); $temp_query = $wp_query; $wp_query = NULL; $wp_query = $loop; while ( $loop->have_posts() ) : $loop->the_post(); <!-- Content here --> endwhile; wp_reset_postdata(); if (function_exists("pagination")) { pagination($wp_query->max_num_pages); } $wp_query = $temp_query; The function that I'm using atm: function pagination($pages = '', $range = 4) { $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=\"pagination\"><span>Página ".$paged." de ".$pages."</span>"; if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>« Primeira</a>"; if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>‹ Anterior</a>"; for ($i=1; $i <= $pages; $i++) { if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )) { echo ($paged == $i)? "<span class=\"current\">".$i."</span>":"".$i.""; } } if ($paged < $pages && $showitems < $pages) echo "Próxima ›"; if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>Última »</a>"; echo "</div>\n"; } }
Ok, so I got it done. I don't know if this is the best way to solve the problem but it was the only one that I found. This answer is a "fork" of jcarroll780's answer on the same problem with some modifications. First I added these functions into my functions.php: $option_posts_per_page = get_option( 'posts_per_page' ); add_action( 'init', 'my_modify_posts_per_page', 0); function my_modify_posts_per_page() { add_filter( 'option_posts_per_page', 'my_option_posts_per_page' ); } function my_option_posts_per_page( $value ) { global $option_posts_per_page; if ( is_tax( 'assunto') ) { return 5; } else { return $option_posts_per_page; } } Then I used the following loop to display the correct pagination (taxonomy-assunto.php): $term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) ); $paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; query_posts(array_merge( $wp_query->query, array('post_type'=>'noticias', 'posts_per_page' => 5, 'paged'=>$paged ) ) ); if (have_posts()) : while ( have_posts() ) : the_post(); // Content Here endwhile; endif; if ( function_exists( 'wp_pagenavi' ) ) { wp_pagenavi(); } It worked pretty well for me, I hope this answers your questions aswell.
Author latest posts pagination
I'm having difficulties in adding pagination for custom wordpress query showing latest author's posts from category 'blog'. I found this code somewhere on stackoverflow. Could someone help me/give some tips how to add pagination to this query? I've spent on it couple hours and couldn't came up with something working. Best for me would be numeric pagination: 1 | 2 | 3 | 4 | so on... functions.php function the_latest_author_posts($post) { $relatedargs = array( 'author' => $post->post_author, 'post__not_in' => array( $post->ID), 'posts_per_page' => 8, 'category_name' => 'blog', ); $relatedquery = new WP_Query( $relatedargs ); while($relatedquery->have_posts()){ $relatedquery->the_post(); $ID = get_the_ID(); ?> <div class="post-blog"> <?php if(has_post_thumbnail()) { $relatedthumbnail = wp_get_attachment_image_src( get_post_thumbnail_id($ID), 'medium', false); $relatedthumbnail_large = wp_get_attachment_image_src( get_post_thumbnail_id($ID), 'full', false); ?> <?php } ?> <h1><span><?php the_title(); ?></span></h1> <h6><i class="fa fa-clock-o" aria-hidden="true"></i> <?php echo get_the_time('j') . '/' . get_the_time('m') . '/' . get_the_time('Y') . ' '; ?></h6> <?php $content = get_the_content(); echo wp_trim_words( $content , '25' ); ?> </div> <?php wp_reset_postdata();}} I call this function in my template in this way: <?php if ( have_posts() ) { while ( have_posts() ) { the_post(); the_latest_author_posts($post); } } ?>
First add this function in your functions function numeric_pagination($pages = '', $range = 2){ $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='pagination'>"; if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>«</a>"; if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>‹</a>"; for ($i=1; $i <= $pages; $i++) { if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )) { echo ($paged == $i)? "<span class='current'>".$i."</span>":"<a href='".get_pagenum_link($i)."' class='inactive' >".$i."</a>"; } } if ($paged < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($paged + 1)."'>›</a>"; if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>»</a>"; echo "</div>\n"; } } and use it after your loop numeric_pagination();
WordPress - adding pagination - visual composer
I have a WordPress theme that uses visual composer built in plugin. The blog and porfolio are inside a function that is then expressed as shortcode on a page. Whenever I try to add some pagination function it doesnt work. The pagination works on other "normal" pages like archives, or search. Any clues? <?php function hsv_blog( $atts, $content = '', $id = '' ) { extract( shortcode_atts( array( 'id' => '', 'class' => '', 'cats' => '', 'limit' => '', 'page_name' => '', ), $atts ) ); $args = array( 'post_type' => 'post', 'posts_per_page' => $limit, 'tax_query' => array( array( 'taxonomy' => 'category', 'field' => 'ids', 'terms' => explode(',', $cats), ), ) ); ob_start(); $q = new WP_Query( $args ); if ( $q->have_posts() ) while ( $q->have_posts() ) : $q->the_post(); $post_format = (get_post_format() == true) ? get_post_format():'standard'; switch ($post_format) { case 'aside': case 'quote': $class = ' grey'; break; default: $class = ''; break; } global $hsv_opt; // general settings $type_website = $hsv_opt['general-type-website']; $pg_class = ( $type_website == 2 ) ? ' home' : ''; ?> <div id="post-<?php the_ID(); ?>" <?php post_class('element clearfix col1-3 blog auto bi'.$class.$pg_class); ?>> <?php get_template_part('post-formats/content', $post_format ); ?> </div> <?php endwhile; ?> <?php wp_reset_query(); ?> <?php return ob_get_clean(); } add_shortcode( 'hsv_blog', 'hsv_blog' ); ?> pagination in the functions file: function wpbeginner_numeric_posts_nav() { if( is_singular() ) return; global $wp_query; /** Stop execution if there's only 1 page */ if( $wp_query->max_num_pages <= 1 ) return; $paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1; $max = intval( $wp_query->max_num_pages ); /** Add current page to the array */ if ( $paged >= 1 ) $links[] = $paged; /** Add the pages around the current page to the array */ if ( $paged >= 3 ) { $links[] = $paged - 1; $links[] = $paged - 2; } if ( ( $paged + 2 ) <= $max ) { $links[] = $paged + 2; $links[] = $paged + 1; } echo '<div class="navigation"><ul>' . "\n"; /** Previous Post Link */ if ( get_previous_posts_link() ) printf( '<li>%s</li>' . "\n", get_previous_posts_link() ); /** Link to first page, plus ellipses if necessary */ if ( ! in_array( 1, $links ) ) { $class = 1 == $paged ? ' class="active"' : ''; printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' ); if ( ! in_array( 2, $links ) ) echo '<li>…</li>'; } /** Link to current page, plus 2 pages in either direction if necessary */ sort( $links ); foreach ( (array) $links as $link ) { $class = $paged == $link ? ' class="active"' : ''; printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link ); } /** Link to last page, plus ellipses if necessary */ if ( ! in_array( $max, $links ) ) { if ( ! in_array( $max - 1, $links ) ) echo '<li>…</li>' . "\n"; $class = $paged == $max ? ' class="active"' : ''; printf( '<li%s>%s</li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max ); } /** Next Post Link */ if ( get_next_posts_link() ) printf( '<li>%s</li>' . "\n", get_next_posts_link() ); echo '</ul></div>' . "\n";}
I had the same problem, trying to create an element in Visual Composer that displays elements from my custom post. The solution: inside your while loop add: $totalPages = $q->max_num_pages; Now you can pass $totalPages to your pagination function. Below I've added my pagination function that worked. Example of usage: my_pagination($totalPages); function my_pagination($pages = '', $range = 2) { $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='pagination'>"; if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>«</a>"; if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>‹</a>"; for ($i=1; $i <= $pages; $i++) { if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )) { echo ($paged == $i)? "<span class='current'>".$i."</span>":"<a href='".get_pagenum_link($i)."' class='inactive' >".$i."</a>"; } } if ($paged < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($paged + 1)."'>›</a>"; if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>»</a>"; echo "</div>\n"; } }
Pagination not working in Wordpress
Can't figure out why this stopped working all of a sudden. The website is: http://www.revival.tv/sermons/topical-messages/ once it goes to page two it just uses the archive template :( not sure why it is doing this, it worked fine for the last year. Wondering if a Wordpress update did it, we just noticed it. I am using the following for the pagination: function pagination($pages = '', $range = 2) { $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($pages != 1) { echo "<div class='pagination group'>"; if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<a href='".get_pagenum_link(1)."'>«</a>"; if($paged > 1 && $showitems < $pages) echo "<a href='".get_pagenum_link($paged - 1)."'>‹</a>"; for ($i=1; $i <= $pages; $i++) { if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )) { echo ($paged == $i)? "<span class='current-page'>".$i."</span>":"<a href='".get_pagenum_link($i)."' class='inactive' >".$i."</a>"; } } if ($paged < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($paged + 1)."'>›</a>"; if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo "<a href='".get_pagenum_link($pages)."'>»</a>"; echo "</div>\n"; } } and then this rewrite for the sermons: // Rewrite the Sermon URL function sermon_rewrite() { $cpt = 'sermon'; $tax = 'series'; add_rewrite_rule( '^'.$cpt.'s/([^/]+)/page/([0-9]+)/?$', 'index.php?post_type='.$cpt.'&'.$tax.'=$matches[1]&paged=$matches[2]', 'top' ); add_rewrite_rule( '^'.$cpt.'s/([^/]+)/([^/]+)/?', 'index.php?post_type='.$cpt.'&'.$tax.'=$matches[1]&'.$cpt.'=$matches[2]', 'top' ); add_rewrite_rule( '^'.$cpt.'s/([^/]+)/([^/]+)/([^/]+)/?', 'index.php?post_type='.$cpt.'&'.$tax.'=$matches[1]&'.$tax.'=$matches[2]&'.$cpt.'=$matches[3]', 'top' ); } add_action('init','sermon_rewrite'); // Update Wordpress with the rewrite function sermon_link( $link, $post = 0 ) { $cpt = 'sermon'; $tax = 'series'; $parent = null; $terms = get_the_terms($post->ID, $tax); if( !$terms ) { $child = 'other'; } else { $child_obj = array_pop($terms); $parent_obj = get_term_by('id', $child_obj->parent, $tax); $child = $child_obj->slug; if ( $parent_obj ) { $parent = $parent_obj->slug; } } if ( $post->post_type == $cpt && $terms ) { return home_url(user_trailingslashit($cpt.'s/'.$parent.'/'.$child.'/'.$post->post_name)); } else if ( $post->post_type == $cpt && !$terms ) { return home_url(user_trailingslashit($cpt.'s/'.$child.'/'.$post->post_name)); } else { return $link; } } add_filter('post_type_link', 'sermon_link', 1, 3);
I think I have found the problem (I'm just trial and erroring here using your provided URL). The following URL successfully loads page 2. http://www.revival.tv/sermons/topical-messages/page/2/?post_type=sermons Which leads me to think that simply adding the s after index.php?post_type='.$cpt should do the trick. See sample below: add_rewrite_rule( '^'.$cpt.'s/([^/]+)/page/([0-9]+)/?$', 'index.php?post_type='.$cpt.'s&'.$tax.'=$matches[1]&paged=$matches[2]', 'top' );