I am trying to create a paging within the page.php my thema and is not showing up ! How do?
I'm doing as follows:
Loop:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$query_prof = array('post_type' => 'Professor', 'posts_per_page' => 10, 'paged'=>$paged);
$profLoop = new WP_Query($query_prof);
if( $profLoop->have_posts()) : while( $profLoop->have_posts()) : $profLoop->the_post();
get_template_part('partials/content','professores'); ?>
<?php endwhile;
?>
calling on page:
<?php wp_pagenavi( array( 'type' => 'Professor' ) ); ?>
Paging appears , no more list is the first direct
Related
I'm trying to add a custom query to a WordPress template and include pagination but my pagination isn't appearing, for example's sake I'm trying to add this to page.php.
I have the following markup which works perfectly when place inside a category template like category.php, the pagination shows up and functions just fine. The issue is that the pagination doesn't appear when the same code is place in page.php or any custom page template.
The query:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$loop = new WP_Query(
array(
'post_type' => 'post',
'posts_per_page' => 10,
'paged' => $paged,
'orderby' => 'desc',
)
);
?>
<?php if ($loop->have_posts()): while ($loop->have_posts()) : $loop->the_post(); ?>
// Loop Markup goes here.
<?php endwhile; ?>
<?php endif; ?>
<?php wp_reset_query(); ?>
<?php pagination(); ?>
Pagination() as defined in functions.php:
function pagination() {
global $wp_query;
$big = 999999999;
echo paginate_links(array(
'base' => str_replace($big, '%#%', get_pagenum_link($big)),
'format' => '?paged=%#%',
'current' => max(1, get_query_var('paged')),
'total' => $wp_query->max_num_pages
));
}
add_action('init', 'pagination');
I've seen a few posts requesting help on the same subject, but I didn't come across an elegant solution.
Any advice would be much appreciated! My knowledge of PHP is pretty limited, I took the pagination() function from the HTML5 Blank theme by Todd Motto so I don't 100% understand what that function defines.
Managed to find a solution by merging my code from the original post with the following code from this tutorial:
I'm posting a complete example of a simple page.php for anyone lost and needing more context to implement this, this works perfectly for me with no broken aspects like each page number returning the same posts or anything.
<?php get_header(); ?>
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
<div class="the_loop">
<?php
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) { // 'page' is used instead of 'paged' on Static Front Page
$paged = get_query_var('page');
} else {
$paged = 1;
}
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$loop = new WP_Query(
array(
'post_type' => 'post',
'posts_per_page' => get_option('posts_per_page'),
'paged' => $paged,
'post_status' => 'publish',
'orderby' => 'desc',
'orderby' => 'date' // modified | title | name | ID | rand
)
);
?>
<?php if ($loop->have_posts()): while ($loop->have_posts()) : $loop->the_post(); ?>
// Loop code goes here.
<?php endwhile; ?>
<?php if ($loop->max_num_pages > 1) : // custom pagination ?>
<?php
$orig_query = $wp_query; // fix for pagination to work
$wp_query = $loop;
$big = 999999999;
echo paginate_links(array(
'base' => str_replace($big, '%#%', get_pagenum_link($big)),
'format' => '?paged=%#%',
'current' => max(1, get_query_var('paged')),
'total' => $wp_query->max_num_pages
));
$wp_query = $orig_query; // fix for pagination to work
?>
<?php endif; ?>
<?php wp_reset_postdata(); else: echo '<p>'.__('Sorry, no posts matched your criteria.').'</p>'; endif; ?>
</div>
<?php endwhile; ?>
<?php endif; ?>
<?php get_template_part('footer'); ?>
I am trying to assign serial no in my WordPress Post Category summary Page. For this I have written following code. It is working fine but problem is in the case of paging.
When I am going to 2nd page or 3rd page it assigns the no from 1 again. Currently I am showing 5 posts/per page. Then in 2nd page it should show post no starting from 6 but it starts from 1 again. Same to 3rd and other pages. How can I show it in 1st page from 1-5 and in 2nd page 6-10 etc?
Below is my code:
<?php
// args
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array(
'paged' => $paged,
'post_type' => 'post',
'cat' => 5,
);
$the_query = new WP_Query( $args );
$postNumber = 1;// to give serial no to post
?>
<?php if($the_query->have_posts() ): ?>
<?php while( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php echo $postNumber++; ?> <?php the_title(); ?>
<?php the_excerpt(); ?>
<?php endwhile; ?>
<?php custom_numeric_posts_nav(); ?>
<?php endif; ?>
<?php wp_reset_query();
Taking help from this topic https://wordpress.stackexchange.com/questions/155903/show-number-of-posts-and-number-on-current-page I have been able to solve my problem. Below is my working code:
<?php
// args
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$ppp = get_query_var('posts_per_page');
$end = $ppp * $paged;
$args = array(
'paged' => $paged,
'post_type' => 'post',
'cat' => 5,
);
$the_query = new WP_Query( $args );
$postNumber = $end - $ppp + 1;// to give serial no to post
?>
I've set up a basic homepage to show 3 articles per page with pagination to navigate through these pages. At the moment it'll only show pagination for pages 1 & 2 and no more, even though I have 12 articles which result in 4 pages. I'm not quite sure where I'm going wrong here:
<?php
$paged = (get_query_var('paged'))? get_query_var('paged') : '1';
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'paged' => $paged,
'posts_per_page' => 2
);
$the_query = new WP_Query( $args );
while ($the_query -> have_posts()) : $the_query -> the_post();
include(locate_template('content-post.php' ));
endwhile;
?>
<?php the_posts_pagination( array('mid_size' => 3) ); ?>
the_posts_pagination use default WP query so it not work here. Can you please try below code:
<?php
if ( get_query_var('paged') ) { $paged = get_query_var('paged'); } else if ( get_query_var('page') ) {$paged = get_query_var('page'); } else {$paged = 1; }
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'paged' => $paged,
'posts_per_page' => 2
);
$temp = $wp_query;
$wp_query= null;
$wp_query = new WP_Query($args);
while ($wp_query -> have_posts()) : $wp_query -> the_post();
include(locate_template('content-post.php' ));
endwhile;
the_posts_pagination( array('mid_size' => 3) );
$wp_query = null;
$wp_query = $temp;
wp_reset_query();
?>
Code is tested in work perfect.
Your code looks alright to me. I was having the same issue, I resolved it by matching the posts_per_page with the Blog pages show at most field located in Settings->Reading in the admin dashboard
example:
"posts_per_page" => 6 then Blog pages show at most should be also 6
I hope this helps.
Use this plugin Click here
and use this shortcode for pagination <?php wp_pagenavi(); ?>
and I use exact following loop for my project and its working.
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args= array(
'cat' => '',
'orderby'=> 'date',
'order'=> 'DESC',
'paged' => $paged ,
'posts_per_page' => 3
);
query_posts($args);
if (have_posts()) :
while (have_posts()):
the_post();
endwhile;
endif;
?>
Please try it. Hope it will work for you also.
And If you are displaying your posts on home page, you need to replace
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
with
<?php
if ( get_query_var('paged') )
{
$paged = get_query_var('paged');
}
else if ( get_query_var('page') )
{
$paged = get_query_var('page');
}
else
{
$paged = 1;
}
The problem is that $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; retrieves the data of the main query object but not for the custom one.
I found a solution here https://wordpress.stackexchange.com/questions/120407/how-to-fix-pagination-for-custom-loops
I have now this piece of code to show on each page 10 posts but know I want still the same but the maximum of posts in total can only be 30.
How do I do this?
<?php query_posts('showposts=10&paged='.$paged);?>
This is code where you want to display the post:
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array( 'post_type' => 'post', 'posts_per_page' => 10,'paged' => $paged);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
print_r($post); ?>
<h1> <?php the_post_thumbnail(); ?></h1>
<h2><?php the_title(); ?></h2>
<h3><?php the_content(); ?></h3>
<?php endwhile; ?>
This is footer where you need to display pagination.
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
<?php if($loop->max_num_pages>1){?>
<ul class="pager">
<?php for($i=1;$i<=$loop->max_num_pages; $i++){ ?>
<li> <?php echo $i; ?></li>
<?php } ?>
</ul>
You should use wp_query to retrieve the posts, like Pieter Goosen said, is more efficient. Also wp_query have a property called $max_num_page that you can use to limit the number of pages you get.
You can modify your pagination to something like this:
global $wp_query;
if ( $wp_query->max_num_pages > 1 ){
$current_page = max( 1, get_query_var('paged') );
$max_pages = 3;
$args = array(
'base' => #add_query_arg('paged','%#%'),
'format' => '/paged/%#%',
'current' => $current_page,
'total' => $max_pages,
'show_all' => false,
'type' => 'array',
'paged' => 1
);
$pages = paginate_links( $args );
if (is_array($pages)) {
$paged = ( get_query_var('paged') == 0) ? 1 : get_query_var('paged');
foreach( $pages as $page) {
echo "$page";
}
}
}
I have also found this question. This code seems to work and you don't need to change any of your current code:
add_filter('pre_get_posts', 'limit_pages');
function limit_pages($query) {
$query->max_num_pages = 3;
if ($query->query_vars['paged'] > 3) {
$query->query_vars['paged'] = 3;
$query->query['paged'] = 3;
}
return $query;
}
I'm having trouble figuring out this pagination issue on my site. The problem is that page 2 shows the same content as page 1. It's not supposed to do that.
<?php
$args = array( 'post_type' => 'baseball-news', 'posts_per_page' => 5 );
$baseball_loop = new WP_Query( $args );
while ( $baseball_loop->have_posts() ) : $baseball_loop->the_post();
?>
<?php
if ( get_post_type() == 'baseball-news' ) : ?>
<?php include( TEMPLATEPATH . '/includes/show-baseball-posts.php' ); ?>
<?php endif; ?>
<?php endwhile; ?>
<?php kriesi_pagination($baseball_loop->max_num_pages); ?>
<?php wp_reset_query(); ?>
This is the site for Kriesi pagination.
Site.
You are not using pagination parameter paged in your query. You use it like this:
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
$args = array( 'post_type' => 'baseball-news', 'posts_per_page' => 5 , 'paged' => $paged );