Trying to achieve WP Pagination with only Prev and Next buttons - php

I'm trying to modify my code for pagination in such a way that it only displays the Prev and Next buttons with no pages in the middle. This is proving to be kind of hard because paginate_links function doesn't have many options in terms of arguments which I can pass to disable the pages from being displayed.
Also using next_posts_link() and previous_posts_link() functions doesn't really help because that links to mydomain.com/page/2 whereas my default homepage is something like this mydomain.com/?fp_type=hot (to display particular kind of custom posts)
This is my pagination code
<?php
global $wp_query;
global $wp_rewrite;
if( is_search() ){
if( (int) get_query_var('page') > 0 ){
$current = get_query_var('page');
}else{
if( (int) get_query_var('paged') > 0 ){
$current = get_query_var('paged');
}else{
$current = 1;
}
}
$wp_query = new WP_Query('paged='. $current . '&s='.get_query_var('s') );
$pagination = array(
'base' => #add_query_arg( 'paged' , '%#%' ),
'format' => '',
'total' => $wp_query -> max_num_pages,
'current' => $current,
'show_all' => false,
'prev_next'=> true,
'prev_text'=> __('« Previous','cosmotheme'),
'next_text'=> __('Next »','cosmotheme'),
'type' => 'array'
);
if( $wp_rewrite->using_permalinks() ){
$pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 'search', remove_query_arg( 's', get_pagenum_link( 1 ) ) ) ) . 'page/%#%/', 'paged' );
}
if( !empty($wp_query->query_vars['s'] ) ){
$pagination['add_args'] = array( 's' => urlencode( get_query_var( 's' ) ) );
}
$pgn = paginate_links( $pagination );
if( !empty( $pgn ) ){
echo '<div class="pag">';
echo '<ul class="b_pag center p_b">';
if( $current == 1 ){
$current--;
}
foreach($pgn as $k => $link){
print '<li>' . str_replace("'",'"',$link) . '</li>';
}
echo '</ul>';
echo '</div>';
}
}else{
$wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;
$pagination = array(
'base' => #add_query_arg('paged','%#%'),
'format' => '',
'total' => $wp_query->max_num_pages,
'current' => $current,
'show_all' => false,
'type' => 'array'
);
if( $wp_rewrite->using_permalinks() ){
$pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 'fp_type' , remove_query_arg( 'type' , remove_query_arg( 's', get_pagenum_link( 1 ) ) ) ) ) . 'page/%#%/', 'paged' );
}
if( !empty($wp_query->query_vars['s'] ) ){
$pagination['add_args'] = array( 's' => urlencode( get_query_var( 's' ) ) );
}
if( !empty( $wp_query->query_vars['type'] ) ){
$pagination['add_args'] = array( 'type' => get_query_var( 'type' ) );
}
if( !empty( $wp_query->query_vars['fp_type'] ) ){
$pagination['add_args'] = array( 'fp_type' => get_query_var( 'fp_type' ) );
}
$pgn = paginate_links( $pagination );
if( $current == 1 ){
$current--;
}
if(!empty($pgn)){
echo '<div class="pag">';
echo '<ul class="b_pag center p_b">';
foreach($pgn as $k => $link){
print '<li>' . str_replace( "'" , '"' , $link ) . '</li>';
}
echo '</ul>';
echo '</div>';
}
} ?>

Add this code to that page where you want to show the post.
<?php
$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'cat' => '7',
'post_type' => 'post',
'posts_per_page' => 10,
'paged' => $page,
);
query_posts($args);?>
add below code after the post code:
<?php while ( have_posts() ) : the_post(); ?>
<div>
<ul>
<li>
<?php the_content(); ?>
</li>
</ul>
</div>
<?php endwhile; // end of the loop. ?>
<div class="navigation"><p><?php posts_nav_link(); ?></p></div>

Related

Wordpress Custom Post type Category View with Pagination

I have a custom post type 'charters' and within my category.php page I have a loop pulling in 'charter' posts for the current category id. I have a custom pagination setup displaying page numbers when I click page 2 link, it goes to URL /page/2 which displays a 404 error. I can't figure out why I am getting the 404. If I adjust my posts per page the pagination updates and displays the correct number of pages but the links do not show up.
Category.php Code:
<?php $paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$product_args = array(
'post_type' => 'charters',
'posts_per_page' => 10, //the same as the parse_query filter in our functions.php file
'paged' => $paged,
'page' => $paged,
'cat' => $cat
);
$product_query = new WP_Query( $product_args ); ?>
<?php if ( $product_query->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $product_query->have_posts() ) : $product_query->the_post(); ?>
<article class="loop">
<h3><?php the_title(); ?></h3>
<div class="content">
<?php the_excerpt(); ?>
</div>
</article>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php
if (function_exists( 'custom_pagination' )) :
custom_pagination( $product_query->max_num_pages,"",$paged );
endif;
?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
Functions.php Code:
function prefix_change_cpt_archive_per_page( $query ) {
//* for cpt or any post type main archive
if ( $query->is_main_query() && ! is_admin() && is_post_type_archive( 'product' ) ) {
$query->set( 'posts_per_page', '10' );
}
}
add_action( 'pre_get_posts', 'prefix_change_cpt_archive_per_page' );
function prefix_change_category_cpt_posts_per_page( $query ) {
if ( $query->is_main_query() && ! is_admin() && is_category( 'test-category' ) ) {
$query->set( 'post_type', array( 'product' ) );
$query->set( 'posts_per_page', '2' );
}
}
add_action( 'pre_get_posts', 'prefix_change_category_cpt_posts_per_page' );
function custom_pagination( $numpages = '', $pagerange = '', $paged='' ) {
if (empty($pagerange)) {
$pagerange = 2;
}
global $paged;
if (empty($paged)) {
$paged = 1;
}
if ($numpages == '') {
global $wp_query;
$numpages = $wp_query->max_num_pages;
if(!$numpages) {
$numpages = 1;
}
}
$pagination_args = array(
'base' => get_pagenum_link(1) . '%_%',
'format' => 'page/%#%',
'total' => $numpages,
'current' => $paged,
'show_all' => False,
'end_size' => 1,
'mid_size' => $pagerange,
'prev_next' => True,
'prev_text' => __('«'),
'next_text' => __('»'),
'type' => 'plain',
'add_args' => false,
'add_fragment' => ''
);
$paginate_links = paginate_links($pagination_args);
if ($paginate_links) {
echo "<nav class='custom-pagination'>";
echo "<span class='page-numbers page-num'>Page " . $paged . " of " . $numpages . "</span> ";
echo $paginate_links;
echo "</nav>";
}
My Permalink Settings are set to "Post Name" I have a feeling this has something to do with rewrites but I can not figure out how to get the page urls to work correctly. An example of my categories are as follows:
/category/long-beach/
/category/san-diego/
you should try this:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'charters',
'cat'=> $cat,
'post_per_page'=> 6,
'paged' => $paged
);
$my_query = new WP_Query( $args );
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><?php the_title(); ?></p><?php
endwhile;
}
wp_reset_query();
?>
<?php echo pnavigation( $query ); ?>
then you need to add following code to function.php :
function pnavigation( $wp_query ) {
$big = 999999999; // need an unlikely integer
$pages = paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $wp_query->max_num_pages,
'prev_next' => false,
'type' => 'array',
'prev_next' => TRUE,
'prev_text' => '←',
'next_text' => '→',
) );
if( is_array( $pages ) ) {
$paged = ( get_query_var('paged') == 0 ) ? 1 : get_query_var('paged');
echo '<ul class="pagination pagination-lg">';
foreach ( $pages as $page ) {
echo "<li>$page</li>";
}
echo '</ul>';
}
}
you need to just replace url (client-testimonials) with your url
function pagination_rewrite() {
add_rewrite_rule('client-testimonials/page/?([0-9]{1,})/?$', 'index.php?pagename=client-testimonials&paged=$matches[1]', 'top');
}
add_action('init', 'pagination_rewrite');
If you are using default pagination then please use below code.
<?php
$current_page = get_queried_object();
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'charters',
'paged' => $paged,
'cat'=> $cat);
$my_query = new WP_Query( $args );
if( $my_query->have_posts() ) {
while ($my_query->have_posts()) : $my_query->the_post(); ?>
<p><?php the_title(); ?></p><?php
endwhile;
}
next_posts_link( 'Older Entries', $my_query->max_num_pages );
previous_posts_link( 'Newer Entries' );
wp_reset_query();
?>
Add below code in your function.php
function posts_on_categorypage( $query ) {
if ( $query->is_category()) {
$query->set( 'posts_per_page', '10' );
}
}
add_action( 'pre_get_posts', 'posts_on_categorypage' );
There was a couple of errors in my code. This is what I get for copy/pasting code and not going through it line by line. Thanks everyone who helped out, but I got this to work by changing:
Bad Code:
function prefix_change_category_cpt_posts_per_page( $query ) {
if ( $query->is_main_query() && ! is_admin() && is_category( 'test-category' ) ) {
$query->set( 'post_type', array( 'product' ) );
$query->set( 'posts_per_page', '2' );
}
}
Good Code:
function prefix_change_category_cpt_posts_per_page( $query ) {
if ( $query->is_main_query() && ! is_admin() && is_category( $cat ) ) {
$query->set( 'post_type', array( 'charters' ) );
$query->set( 'posts_per_page', '10' );
}
}

Pagination not working for custom post type

Video Explanation https://drive.google.com/open?id=0B4HLAyVud6iLNjlHMGF3RXA3Qkk
I am trying to make a pagination, so far it is showing page links properly like 1 2 3 4 > but the problem is when i navigate to page 2 or any other page it take me to 404 page. Here is my pagination function
if ( ! function_exists( 'sp_pagination' ) ) {
function sp_pagination( $query = '', $echo = true ) {
$prev_arrow = is_rtl() ? 'fa fa-angle-right' : 'fa fa-angle-left';
$next_arrow = is_rtl() ? 'fa fa-angle-left' : 'fa fa-angle-right';
// Get global $query
if ( ! $query ) {
global $wp_query;
$query = $wp_query;
}
// Set vars
$total = $query->max_num_pages;
$big = 999999999;
// Display pagination
if ( $total > 1 ) {
// Get current page
if ( $current_page = get_query_var( 'paged' ) ) {
$current_page = $current_page;
} elseif ( $current_page = get_query_var( 'page' ) ) {
$current_page = $current_page;
} else {
$current_page = 1;
}
// Get permalink structure
if ( get_option( 'permalink_structure' ) ) {
if ( is_page() ) {
$format = 'page/%#%/';
} else {
$format = '/%#%/';
}
} else {
$format = '&paged=%#%';
}
$args = apply_filters( 'sp_pagination_args', array(
'base' => str_replace( $big, '%#%', html_entity_decode( get_pagenum_link( $big ) ) ),
'format' => $format,
'current' => max( 1, $current_page ),
'total' => $total,
'mid_size' => 3,
'type' => 'list',
'prev_text' => '<span class="' . $prev_arrow . '"></span>',
'next_text' => '<span class="' . $next_arrow . '"></span>',
) );
// Output pagination
if ( $echo ) {
echo '<div class="sp-pagination">' . paginate_links( $args ) . '</div>';
} else {
return '<div class="sp-pagination">' . paginate_links( $args ) . '</div>';
}
}
}
}
And here is my query
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$portfolio_grid_query = new WP_Query(
array(
'post_type' => 'portfolio',
'post_status' => 'publish',
'name' => $portfolio_grid_title_slug,
'posts_per_page' => $portfolio_grid_item_limit,
'category_name' => $portfolio_grid_cats,
'order' => $portfolio_order,
'orderby' => $portfolio_orderby,
'paged' => $paged
)
);
here all the while loop and post structure goes and after while loop ends i put the pagination function like so:
sp_pagination($portfolio_grid_query, true);
It is showing all the navigation structure properly but the issue is when i navigate it takes me to 404 page instead of page 2 to show other remaining posts. Keep in mind i have total of 9 posts and i set posts_per_page to 6, which is showing me 6 posts on current page and if i click on page 2 link it should take me to other page to show remaining 3 posts.

WordPress Pagination buttons show the same posts on all pages

I am having an issue with the pagination on my WordPress blog page. The next and previous page buttons take me to a new page but all pages show the same 10 blog posts.
Here is the code I am using:
<?php
$args = array('cat' => '-27');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
get_template_part( 'content', get_post_format() );
endwhile;
global $wp_query;
$total = $wp_query->max_num_pages;
if ( $total > 1 ) {
if ( !$current_page = get_query_var('paged') )
$current_page = 1;
if( get_option('permalink_structure') ) {
$format = '?&paged=%#%';
} else {
$format = 'page/%#%/';
}
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => $format,
'current' => $current_page,
'total' => $total,
'mid_size' => 4,
'type' => 'list'
));
}
?>
Any help would be greatly appreciated!

Wordpress Multiple Post Type Loop shows same results on every page

So I am using WordPress with an underscores theme. I used code from twenty fourteen's theme with pagination.
if ( ! function_exists( 'public_notices_paging_nav' ) ) :
function public_notices_paging_nav() {
global $wp_query, $wp_rewrite;
// Don't print empty markup if there's only one page.
if ( $wp_query->max_num_pages < 2 ) {
echo ("<h4 class='one-page-results'>End of Results</h4>");
return;
}
$paged = get_query_var( 'paged' ) ? intval( get_query_var( 'paged' ) ) : 1;
$pagenum_link = html_entity_decode( get_pagenum_link() );
$query_args = array();
$url_parts = explode( '?', $pagenum_link );
if ( isset( $url_parts[1] ) ) {
wp_parse_str( $url_parts[1], $query_args );
}
$pagenum_link = remove_query_arg( array_keys( $query_args ), $pagenum_link );
$pagenum_link = trailingslashit( $pagenum_link ) . '%_%';
$format = $wp_rewrite->using_index_permalinks() && ! strpos( $pagenum_link, 'index.php' ) ? 'index.php/' : '';
$format .= $wp_rewrite->using_permalinks() ? user_trailingslashit( $wp_rewrite->pagination_base . '/%#%', 'paged' ) : '?paged=%#%';
// Set up paginated links.
$links = paginate_links( array(
'base' => $pagenum_link,
'format' => $format,
'total' => $wp_query->max_num_pages,
'current' => $paged,
'mid_size' => 1,
'add_args' => array_map( 'urlencode', $query_args ),
'prev_text' => __( '← Previous', 'public_notices' ),
'next_text' => __( 'Next →', 'public_notices' ),
'type' => 'list',
) );
if ( $links ) :
?>
<nav class="navigation paging-navigation" role="navigation">
<h1 class="screen-reader-text"><?php _e( 'Posts navigation', 'public_notices' ); ?></h1>
<?php echo $links; ?>
</nav><!-- .navigation -->
<?php
endif;
}endif;
This worked great when I only had one post type, but now I've made a loop that displays two post types and it works. But on every page its the same group of posts.
<?php
$postLoop = array(
'post_type' => array('public_notice_post', 'post'),
'orderby' => 'date',
'order' => 'DESC'
);
$query = new WP_Query( $postLoop );
if ($query->have_posts()) :
while ($query->have_posts()) : $query->the_post();
get_template_part( 'template-parts/content', get_post_format() );
endwhile;
public_notices_paging_nav();
endif;
wp_reset_query(); ?>
Anything will help. I've tried a lot.
Maybe you got a loop with no "wp_reset_query()" and this may caused some bugs on other loop.. just look all your loop to be sure there noone without reset query.
On which kind of page you coded this loop ? This is a custom template or other ?
Sorry for my english, im french.
To fix the original problem of duplicated posts on every page all I had to do was write my code like this. HOWEVER I am still having an issue of page count, but that will addressed elsewhere.
<?php
$page = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$ppp = get_option( 'posts_per_page' );
if ( $page == 1 )
$offset = 6;
else
$offset = 6 + ( $page - 1 ) * $ppp;
?>
<?php
$postLoop = array(
'post_type' => array('public_notice_post', 'post'),
'orderby' => 'date',
'order' => 'DESC',
'paged' => $paged,
'posts_per_page'=> $ppp,
'offset' => $offset
);
$wp_query = new WP_Query( $postLoop );
if ($wp_query->have_posts()) :
while ($wp_query->have_posts()) : the_post();
get_template_part( 'template-parts/content', get_post_format() );
endwhile;
public_notices_paging_nav();
endif;
wp_reset_query(); ?>

Custom pagination - arrows not working

I have a strange problem where the arrows generated for next & previous pages don't work. I just can't seem to fix it. The html seems to be fine and numbers work, but when clicked on nothing happens: http://sketchbookmagazine.com/category/fashion/
Pagination code:
function pagination($prev = '«', $next = '»') {
global $wp_query, $wp_rewrite;
$wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;
$pagination = array(
'base' => #add_query_arg('paged','%#%'),
'format' => '',
'total' => $wp_query->max_num_pages,
'current' => $current,
'prev_text' => __($prev),
'next_text' => __($next),
'type' => 'plain'
);
if( $wp_rewrite->using_permalinks() )
$pagination['base'] = user_trailingslashit( trailingslashit( remove_query_arg( 's', get_pagenum_link( 1 ) ) ) . 'page/%#%/', 'paged' );
if( !empty($wp_query->query_vars['s']) )
$pagination['add_args'] = array( 's' => get_query_var( 's' ) );
echo paginate_links( $pagination );
};
html:
<div class="pagination"><?php pagination('«','»'); ?></div>
Any help is greatly appreciated!
I have never seen this construct:
$wp_query->query_vars['paged'] > 1 ? $current = $wp_query->query_vars['paged'] : $current = 1;
Try changing that to:
$current = $wp_query->query_vars['paged'] > 1 ? $wp_query->query_vars['paged'] : 1;

Categories