Not sure if this was since upgrading to PHP 7.2 or the latest WordPress version but the following code used to allow me to add pagination for repeater values. Now what seems to happen is that the page just reloads the page. The pagination link shows as /example/2/, this used to load the page but now it is just reloading example.
Any ideas?
<?php
/*
* Paginatation on Advanced Custom Fields Repeater
*/
if ( get_query_var('paged') ) {
$page = get_query_var('paged');
} elseif ( get_query_var('page') ) {
$page = get_query_var('page');
} else {
$page = 1;
}
// Variables
$row = 0;
$images_per_page = 10; // How many images to display on each page
$images = get_field( 'image_gallery' );
$total = count( $images );
$pages = ceil( $total / $images_per_page );
$min = ( ( $page * $images_per_page ) - $images_per_page ) + 1;
$max = ( $min + $images_per_page ) - 1;
// ACF Loop
if( have_rows( 'image_gallery' ) ) : ?>
<?php while( have_rows( 'image_gallery' ) ): the_row();
$row++;
// Ignore this image if $row is lower than $min
if($row < $min) { continue; }
// Stop loop completely if $row is higher than $max
if($row > $max) { break; } ?>
<?php $img_obj = get_sub_field( 'image' ); ?>
<a href="<?php echo $img_obj['sizes']['large']; ?>">
<img src ="<?php echo $img_obj['sizes']['thumbnail']; ?>" alt= "Your ALT Tag" />
</a>
<?php endwhile;
// Pagination
echo paginate_links( array(
'base' => get_permalink() . '%#%' . '/',
'format' => '?paged=%#%',
'current' => $page,
'total' => $pages
) );
?>
<?php else: ?>
<p>No images found</p>
<?php endif; ?>
There was a bug tracker after 5.5:
https://core.trac.wordpress.org/ticket/50976#comment:7
You might check some of the solutions there. It seems to be an issue with the query var "page" which is reserved in WordPress core, since it normally uses ?p and ?page to redirect to a certain page.
So you might use something else.
Related
For years I use a custom pagination script that I include to all my websites and it paginates flawlessly through posts. For the first time ever I created custom post types. My same script will not paginate through pages... Also all pages links send me to home. It displays exactly the correct pages number based on posts per page. It just won't work. I have used all scripts I could find on net.
Is there anything I am missing?
Has archive is true by the way. Below code example is from archive-drivers.php
I also used both custom CPT creation with script in functions and also used a plugin. Just to check if something was wrong.
<?php
$args = array(
'posts_per_page' => 1,
'paged' => $paged,
'orderby' => 'post_date',
'order' => 'DESC',
'post_type' => 'Drivers',
'post_status' => 'publish',
'suppress_filters' => true
);
// get the results
$wp_query = new WP_Query( $args );
?>
<?php if ($wp_query->have_posts()): // the Loop ?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
(...my code to display the custom post type posts...)
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<!-- end of Code for the Loop -->
<?php endif; ?>
<!-- Code for PAGINATION -->
<?php
if (function_exists("custom_pagination"))
{
custom_pagination();
}
?>
<!-- End of Code for PAGINATION -->
<?php wp_reset_postdata(); ?>
<!-- end of Code for the Loop -->
and
// Bootstrap Custom Pagination function in Functions.php
function custom_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="as-pagination" style="text-align:center; align:center;">';
echo '<div> Page ' .$paged . ' of ' .$pages.'</div>';
echo '<ul>';
// If we want to always see th First Page link
if($paged > 1) echo '<li></i></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>
</li>":"<li><a href='".get_pagenum_link($i)."'>".$i."</a></li>";
}
}
//if ($paged < $pages-1 && $paged+$range-1 < $pages && $showitems < $pages) echo '<li>»</li>';
if($paged < $pages) echo '<li><i class="far fa-arrow-right"></i></li>';
echo "</ul>";
echo "</div>";
}
}
And the answer came from a FB friend and has to do with pre_get_posts. Only that way you can tell wp how many posts per page you want for your custom post type. The 'posts_per_page' in args won't work.
function my_cptui_change_posts_per_page( $query ) {
if ( is_admin() || ! $query->is_main_query() ) {
return;
}
if ( is_post_type_archive( 'drivers' ) ) {
$query->set( 'posts_per_page', 1 );
}
}
add_filter( ' ', 'my_cptui_change_posts_per_page' );
I have added advance search in Wordpress Search.. please check below image..
Below is my search Code in wordpress :
global $wp_query;
$args = array (
's' => $s,
'cat' => $category,
'year' => $year,
'monthnum' => $monthnum
);
$query = new WP_Query($args) ;
<?php if ( $query->have_posts() ) : ?>
<?php while (have_posts() ) : the_post(); ?>
<article>
<h2><?php the_title(); ?></h2>
<div class="entry">
<?php the_content( __( 'Read More »', 'tie' ) ); ?>
</div>
</article>
<?php endwhile; ?>
<?php endif; ?>
From above code i am getting proper result... but when i change the category the result stays same...??
If i get search results as 1,2,3,4,5,6,7,8,9 and on change on category i need 1,5,6,8,9
what is wrong with my code..??
First check your $category is null or not. if it not showing the related to category.
also make sure you $category is integer
See what WP_QUERY use category arguments.
cat (int) - use category id. category_name (string) - use
category slug. category__and (array) - use category id.
category__in (array) - use category id. category__not_in (array)
- use category id.
Change your loop to below code.
For using pagination you may need to change your $args array with the required arguments.
// for number of page.
posts_per_page , paged used for pagination.
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$per_page = 8; // records per page.
$args = array (
's' => $s,
'cat' => $category,
'year' => $year,
'monthnum' => $monthnum ,
'posts_per_page' => $per_page,
'post_status' => 'publish',
'paged' => $paged,
);
// the query
$query = new WP_Query( $args ); ?>
<?php if ( $query->have_posts() ) : ?>
<!-- pagination here -->
<!-- the loop -->
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php if (function_exists("pagination_custom")) {
pagination_custom($query->max_num_pages);
}
?>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
UPDATE : For Pagination:
// Pagination code
function pagination_custom($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\"><ul>";
if ($paged > 2 && $paged > $range + 1 && $showitems < $pages) echo "<a href='" . get_pagenum_link(1) . "'>« First</a>";
if ($paged > 1 && $showitems < $pages) echo "<a href='" . get_pagenum_link($paged - 1) . "'>‹ Previous</a>";
for ($i = 1; $i <= $pages; $i++) {
if (1 != $pages && (!($i >= $paged + $range + 1 || $i <= $paged - $range - 1) || $pages <= $showitems)) {
echo ($paged == $i) ? "<li>" . $i . "</li>" : "<li>" . $i . "</li>";
}
}
if ($paged < $pages && $showitems < $pages) echo "Next ›";
if ($paged < $pages - 1 && $paged + $range - 1 < $pages && $showitems < $pages) echo "<a href='" . get_pagenum_link($pages) . "'>Last »</a>";
echo "</ul></div>\n";
}
}
I had originally set up my menu to set categories, but that limits my use of categories to the menu structure.
Ideally what I'd prefer to do is used page templates.
So I create a new page template 'page-for-cat.php' and name the template 'page of category'.
I add in this query to the page
<?php
query_posts('category_name=my-cat');
while (have_posts()) : the_post();
the_content();
endwhile;
?>
I used this post as reference
I then make a new page in wordpress and set the page template to 'page of category'. I call the page 'Test page'.
I then add that 'Test Page' wordpress page to the menu structure.
So far, so good...
What I can't get to work is the pagination to previous and next pages to work with this set up.
Where am I going wrong, hav I missed something?
Thanks
You should never ever use query_posts
Note: This function isn't meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination).
Here is a post I recently did on WPSE. This template lets you choose from a dropdown which category you want to display on the page. What is nice, you have one template for all pages, not one page template per category. Here is that page and post with pagination
From what I can understand is that you are specifically looking for a page template that you can set under page attributes in the page admin add/edit screen and auto assign a category to it.
Here is a page template that does exactly that. You can create a new page in the back end, assign this template to the page and then set a specific category to it, all from the back end.
Here is how it work
I have adapted this template from a tutorial by digitalraindrops that you can go and check out at the link provided. I have made a couple modifications to it to work on twentyfourteen. I have also removed the original WP_Query as it is used in a way that actually is the same as doing a normal query_posts query, which should never be used.
Note: This function isn't meant to be used by plugins or themes. As explained later, there are better, more performant options to alter the main query. query_posts() is overly simplistic and problematic way to modify main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination).
It should be noted before I start, you can also just create a proper category.php template, create a custom menu and add the required category to the menu. You should also note that you can use the category name as page slug, but you cannot have any child pages for these pages, this will interfere and break the Template Hierarchy
To accomplish all of what we need, you need to create a custom meta box which will be added to the page add/edit screen when the specific template is selected. This meta box will be used to select which category will be used for the page selected. You will also be able to set custom posts_per_page and sort posts. Here is the code to add the custom meta box. Add these code to your functions.php or any functions related file
<?php
/**-----------------------------------------------------------------------------
*
* Add a post metabox with options to the admin page screen.
* After selcting the page-pop.php template as a page template,
* this metabox will appear in the admin page screen.
* From here you can choose which category's posts to display
* and how the posts will be displayed on the page
*
* #package WordPress
* #subpackage Twenty_Fourteen
* #since Twenty Fourteen 1.0
*
*------------------------------------------------------------------------------*/
/**-----------------------------------------------------------------------------
*
* 1. Only add meta boxes for the pop template
*
* #since Twenty Fourteen 1.0
*
*------------------------------------------------------------------------------*/
add_action('admin_init', 'pietergoosen_add_pop_meta_box');
function pietergoosen_add_pop_meta_box(){
$post_id = isset( $_GET['post'] ) ? $_GET['post'] : 0 ;
if($post_id) {
$template_file = get_post_meta($post_id,'_wp_page_template',TRUE);
if ($template_file == 'page-pop.php') {
add_meta_box('pop_meta_box', __( 'Page of Posts with the same name', 'pietergoosen' ), 'pietergoosen_pop_meta_options', 'page', 'side', 'core');
} else {
$meta = get_post_meta($post_id, '_cat_id', true);
if( $meta ) {
pietergoosen_pop_update_post_meta($post_id, '_cat_id', '');
pietergoosen_pop_update_post_meta($post_id, '_page_title', '');
pietergoosen_pop_update_post_meta($post_id, '_posts_title', '');
pietergoosen_pop_update_post_meta($post_id, '_order_by', '');
pietergoosen_pop_update_post_meta($post_id, '_asc', '');
pietergoosen_pop_update_post_meta($post_id, '_post_count', '');
pietergoosen_pop_update_post_meta($post_id, '_days', '');
remove_meta_box( 'pop_meta_box', 'page', 'side' );
}
}
}
add_action('save_post', 'pietergoosen_pop_update_post_meta_box');
}
/**-----------------------------------------------------------------------------
*
* 2. Built the list to display the options in the metabox
*
* #since Twenty Fourteen 1.0
*
*------------------------------------------------------------------------------*/
$order_list = array(
'none' => array( 'value' => 'none','label' => 'None' ),
'id' => array( 'value' => 'ID','label' => 'Post ID' ),
'author' => array( 'value' => 'author','label' => 'Author' ),
'title' => array( 'value' => 'title','label' => 'Post Title' ),
'date' => array( 'value' => 'date', 'label' => 'Post Date' ),
'modified' => array( 'value' => 'modified','label' => 'Modified Date' ),
'parent' => array( 'value' => 'parent','label' => 'Parent Post' ),
'rand' => array( 'value' => 'rand','label' => 'Random' ),
'comment_count' => array( 'value' => 'comment_count','label' => 'Comment Count' ),
'menu_order' => array( 'value' => 'menu_order','label' => 'Menu Order' ),
);
$sort = array(
'DESC' => array( 'value' => 'DESC','label' => 'Descending' ),
'ASC' => array( 'value' => 'ASC','label' => 'Ascending' ),
);
function pietergoosen_pop_meta_options(){
$post_id = !empty($_GET['post']) ? $_GET['post'] : 0;
if( !$post_id ) return;
$template_file = get_post_meta($post_id,'_wp_page_template',TRUE);
if ($template_file != 'page-pop.php') return;
global $order_list,$post_styles,$sort;
$categories = get_categories();
//Check if we have values
$post_meta=array();
$post_meta = get_post_meta( $post_id,false );
$cat_id = isset( $post_meta['_cat_id'] ) ? $post_meta['_cat_id'][0] : 1;
$page_title = isset( $post_meta['_page_title'] ) && $post_meta['_page_title'] ? $post_meta['_page_title'][0] : '';
$posts_title = isset( $post_meta['_posts_title'] ) && $post_meta['_posts_title'] ? $post_meta['_posts_title'][0] : '';
$order_by = isset( $post_meta['_order_by'] ) ? $post_meta['_order_by'][0] : 'date';
$asc = isset( $post_meta['_asc'] ) ? $post_meta['_asc'][0] : 'DESC';
$post_count = isset( $post_meta['_post_count'] ) ? $post_meta['_post_count'][0] : get_option('posts_per_page');
if(!$post_count || !is_numeric( $post_count )) $post_count = get_option('posts_per_page');
$days = isset( $post_meta['_days'] ) ? $post_meta['_days'][0] : '0';
if($days && !is_numeric( $days )) $days = '0';
?>
<!-- Sart the meta boxes -->
<div class="inside">
<p><label><strong><?php _e( 'Page Title', 'pietergoosen' ); ?></strong></label></p>
<input id="_posts_title" name="_posts_title" type="text" style="width: 98%;" value="<?php echo $posts_title; ?>"/>
<p><label><strong><?php _e( 'Post Title', 'pietergoosen' ); ?></strong></label></p>
<input id="_page_title" name="_page_title" type="text" style="width: 98%;" value="<?php echo $page_title; ?>"/>
<p><label><strong><?php _e( 'Category', 'pietergoosen' ); ?></strong></label></p>
<select id="_cat_id" name="_cat_id">
<?php
//Category List
foreach ($categories as $cat) :
$selected = ( $cat->cat_ID == $cat_id ) ? ' selected = "selected" ' : '';
$option = '<option '.$selected .'value="' . $cat->cat_ID;
$option = $option .'">';
$option = $option .$cat->cat_name;
$option = $option .'</option>';
echo $option;
endforeach;
?>
</select>
<p><label><strong><?php _e( 'Sort by', 'pietergoosen' )?></strong></label></p>
<select id="_order_by" name="_order_by">
<?php
foreach ($order_list as $output) :
$selected = ( $output['value'] == $order_by ) ? ' selected = "selected" ' : '';
$option = '<option '.$selected .'value="' . $output['value'];
$option = $option .'">';
$option = $option .$output['label'];
$option = $option .'</option>';
echo $option;
endforeach;
?>
</select>
<p><label><strong><?php _e( 'Order', 'pietergoosen' )?><strong></label></p>
<select id="_asc" name="_asc">
<?php
foreach ($sort as $output) :
$selected = ( $output['value'] == $asc ) ? ' selected = "selected" ' : '';
$option = '<option '.$selected .'value="' . $output['value'];
$option = $option .'">';
$option = $option .$output['label'];
$option = $option .'</option>';
echo $option;
endforeach;
?>
</select>
<p><strong><label><?php _e( 'Posts per Page', 'pageofposts' ); ?><strong></label></p>
<input id="_post_count" name="_post_count" type="text" value="<?php echo $post_count; ?>" size="3" />
<p><strong><label><?php _e( 'Posts in the last days', 'pageofposts' ); ?><strong></label></p>
<input id="_days" name="_days" type="text" value="<?php echo $days; ?>" size="3" />
</div>
<!-- End page of posts meta box -->
<?php
}
function pietergoosen_pop_update_post_meta_box( $post_id ){
if ( empty( $_POST ) ) {
return;
} else {
$template_file = get_post_meta($post_id,'_wp_page_template',TRUE);
if ($template_file != 'page-pop.php') return;
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
return $post_id;
} else {
if ( $_POST['post_type'] == 'page' ) {
if ( !current_user_can( 'edit_page', $post_id ) )
return $post_id;
} else {
if ( !current_user_can( 'edit_post', $post_id ) )
return $post_id;
}
$meta = isset( $_POST['_cat_id'] ) ? $_POST['_cat_id'] : 1;
pietergoosen_pop_update_post_meta($post_id, '_cat_id', $meta);
$meta = isset( $_POST['_page_title'] ) ? $_POST['_page_title'] : '';
pietergoosen_pop_update_post_meta($post_id, '_page_title', $meta);
$meta = isset( $_POST['_posts_title'] ) ? $_POST['_posts_title'] : '';
pietergoosen_pop_update_post_meta($post_id, '_posts_title', $meta);
$meta = isset( $_POST['_order_by'] ) ? $_POST['_order_by'] : 'date';
pietergoosen_pop_update_post_meta($post_id, '_order_by', $meta);
$meta = isset( $_POST['_asc'] ) ? $_POST['_asc'] : 'DESC';
pietergoosen_pop_update_post_meta($post_id, '_asc', $meta);
$meta = isset( $_POST['_post_count'] ) ? $_POST['_post_count'] : get_option('posts_per_page');
pietergoosen_pop_update_post_meta($post_id, '_post_count', $meta);
$meta = isset( $_POST['_days'] ) ? $_POST['_days'] : 0;
pietergoosen_pop_update_post_meta($post_id, '_days', $meta);
return;
}
}
}
function pietergoosen_pop_update_post_meta($post_id, $key, $data) {
$post_meta = get_post_meta($post_id, $key, true);
if( $data != '' && $post_meta != $data) {
update_post_meta($post_id, $key, $data);
} elseif ( $post_meta != '' && $data == '' ) {
delete_post_meta($post_id, $key);
}
}
?>
Now for the page template. Create a file in your root directory and call it page-pop.php. You have to call your template this. If you need to rename it, check in the code above and change all instances of page-pop.php to the template name of your choice
The first section will call back all the saved values from the meta box. These values will be fed back into your custom WP_Query to call back all the posts according to category.
The first loop is your normal page template loop which you can use to display content added to the WYSIWYG editor in the page add/edit screen. The second loop/query is the query that will return your posts. You should note that all markup here is for the default twenty fourteen theme, you will need to modify it to suite your theme.
Here is the code that should go into your page-pop.php template
<?php
/**
* Template Name: Page of Posts
*/
get_header(); ?>
<?php
//See if we have any values
$post_meta = array();
$post_meta = get_post_meta( $post->ID,false );
$catid = isset( $post_meta['_cat_id'] ) ? $post_meta['_cat_id'][0] : 1;
$page_title = isset( $post_meta['_page_title'] ) ? $post_meta['_page_title'][0] : '';
$posts_title = isset( $post_meta['_posts_title'] ) ? $post_meta['_posts_title'][0] : '';
$orderby = isset( $post_meta['_order_by'] ) ? $post_meta['_order_by'][0] : 'date';
$asc = isset( $post_meta['_asc'] ) ? $post_meta['_asc'][0] : 'DESC';
$list_style = isset( $post_meta['_list_style'] ) ? $post_meta['_list_style'][0] : 'default';
$post_count = isset( $post_meta['_post_count'] ) ? $post_meta['_post_count'][0] : get_option('posts_per_page');
if(!$post_count || !is_numeric( $post_count ))
$post_count = get_option('posts_per_page');
$days = isset( $post_meta['_days'] ) ? $post_meta['_days'][0] : 0;
if($days && !is_numeric( $days ))
$days = 0;
$do_not_show_stickies = ($list_style == 'default') ? 0 : 1;
?>
<div id="main-content" class="main-content">
<div id="primary" class="content-area">
<div id="content" class="site-content" role="main">
<!-- Page Title -->
<?php if( $posts_title ) : ?>
<article id="posts-title">
<header class="entry-header">
<h2 class="entry-title"><?php echo $posts_title; ?></h2>
</header><!-- .entry-header -->
</article><!-- #posts-title -->
<?php endif; ?>
<?php the_post(); ?>
<?php global $post;
if( $post->post_content || $page_title ) : ?>
<div class="entry-content">
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php if( $page_title ) : ?>
<header class="entry-header">
<h1 class="entry-title"><?php echo $page_title; ?></h1>
</header><!-- .entry-header -->
<?php endif; ?>
<?php if( $post->post_content ) : ?>
<div class="entry-content">
<?php the_content(); ?>
<?php wp_link_pages( array( 'before' => '<div class="page-link"><span>' . __( 'Pages:', 'pietergoosen' ) . '</span>', 'after' => '</div>' ) ); ?>
</div><!-- .entry-content -->
<footer class="entry-meta">
</footer><!-- .entry-meta -->
<?php endif; ?>
</article><!-- #post-<?php the_ID(); ?> -->
</div>
<?php endif; ?>
<?php
/**-----------------------------------------------------------------------------
*
* Start our custom query to display category posts
*
*------------------------------------------------------------------------------*/
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'cat' => $catid,
'posts_per_page' => $post_count,
'paged' => $paged,
'orderby' => $orderby,
'order' => $asc,
'ignore_sticky_posts' => $do_not_show_stickies,
);
if( $days ) {
function pop_filter_where( $where = '') {
global $days;
$where .= " AND post_date > '" . date('Y-m-d', strtotime('-' .$days .' days')) . "'";
return $where;
}
add_filter( 'posts_where', 'pop_filter_where' );
$cat_query = new WP_Query($args);
remove_filter( 'posts_where', 'pop_filter_where' );
} else {
$cat_query = new WP_Query($args);
}
// Output
if ( $cat_query->have_posts() ) :
$counter = 1; //Starts counter for post column lay out
// Start the Loop.
while ( $cat_query->have_posts() ) : $cat_query->the_post(); ?>
<div class="entry-column<?php echo ( $counter%2 ? ' left' : ' right' ); ?>">
<?php get_template_part( 'content', get_post_format() ); ?>
</div>
<?php
$counter++; //Update the counter
endwhile;
// next_posts_link() usage with max_num_pages
next_posts_link( 'Older Entries', $cat_query->max_num_pages );
previous_posts_link( 'Newer Entries' );
wp_reset_postdata();
else :
get_template_part( 'content', 'none' );
endif; ?>
</div><!-- #content -->
</div><!-- #primary -->
<?php get_sidebar( 'content' ); ?>
</div><!-- #main-content -->
<?php
get_footer();
I hope I understood you correctly and hope this is what you need
This is my code anyone can please help me how to add pagination in user results page?
if (!empty($users)){
foreach($users as $user){
$user = get_userdata($user->ID);
<div class="wg_rec_dashboard_img">
<?php $wg_front_dp = get_user_meta($user->ID, 'wg_dp', true);?>
<img src="<?php echo $wg_front_dp; ?>">
</div>
<div class="wg_rec_username">
<?php echo $user->first_name; ?>
</div>
I have tried Wordpress' built-in pagination but its not working here.
You can use following;
<?php
$number = 50; // Update this according to your needs. Users perpage
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$offset = ($paged - 1) * $number;
$users = get_users();
$query = get_users('&offset='.$offset.'&number='.$number);
$total_users = count($users);
$total_query = count($query);
$total_pages = intval($total_users / $number) + 1;
// Iterate perpage users
foreach($query as $q){
$user = get_userdata($q->ID);
?>
<div class="wg_rec_dashboard_img">
<?php $wg_front_dp = get_user_meta($user->ID, 'wg_dp', true);?>
<img src="<?php echo $wg_front_dp; ?>">
</div>
<div class="wg_rec_username">
<?php echo $user->first_name; ?>
</div>
<?php } ?>
// Pagination part
<?php
if ($total_users > $total_query) {
?>
<div id="pagination" class="clearfix">
<span class="pages">Pages:</span>
<?php
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => 'page/%#%/',
'current' => $current_page,
'total' => $total_pages,
'prev_next' => false,
'type' => 'list',
));
?>
</div>
Place this code in your theme's function.php (you can style the page navigation with CSS as you prefer):
The PHP:
if (!function_exists('_numeric_posts_nav')) {
function _numeric_posts_nav() {
global $wp_query;
/** Stop execution if there's if it is single past/page or only 1 page */
if( is_singular() ) return;
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";
}
}
The CSS:
div.navigation ul li{list-style:none;list-style-type:none;display:inline;padding:4px 4px;margin:4px 4px;border:1px solid #ccc;background-color:#eaeaea}
div.navigation ul li.active{background-color:#FC0;border:1px solid #C60}
Now, use the function in your template file like this:
if (function_exists('_numeric_posts_nav')) { _numeric_posts_nav(); }
It will display pagination if number of posts are greater that 10
I have 6 posts in Wordpress. I'm trying to display last 5 posts on page. Here is my code:
<?php
/*
Template Name: Posts Template
*/
?>
//header...
<?php $the_query = new WP_Query( array('showposts' => 5, 'post_type' => 'post')); ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<h1><?php echo the_title(); ?></h1>
<?php endwhile; ?>
<?php posts_nav_link(); ?>
//footer
Pagination shows me that there is 4 pages, but like I said I have only 6 posts, so how it is possible? Also Pagination seems to not work correctly, doesn't matter on what page I am, it shows always last 5 posts.
Any idea what am I doing wrong?
Try adding a paged parameter to your query. You can also have a read of the documentation on the topic, there are some examples. Oh, and it's probably more forward-looking to use posts_per_page instead of showposts.
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$the_query = new WP_Query( array(
'posts_per_page' => 5,
'post_type' => 'post',
'paged' => $paged
));
Add this code to functions.php
function posts_nav_link( $query_object, $show_previous_posts_link = false, $show_next_posts_link = false ) {
if( is_singular() )
return;
/** Stop execution if there's only 1 page */
if( $query_object->max_num_pages <= 1 )
return;
$paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
$max = intval( $query_object->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() && $show_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() && $show_next_posts_link )
printf( '<li>%s</li>' . "\n", get_next_posts_link() );
echo '</ul></div>' . "\n";
}
And use posts_nav_link($the_query):
<?php
/*
Template Name: Posts Template
*/
?>
//header...
<?php $the_query = new WP_Query( array('showposts' => 5, 'post_type' => 'post')); ?>
<?php while ($the_query -> have_posts()) : $the_query -> the_post(); ?>
<h1><?php echo the_title(); ?></h1>
<?php endwhile; ?>
<?php posts_nav_link($the_query); ?>
//footer