Wordpress paginate custom post type - php

I have a page called News (using page template page-newslist.php), which is supposed to display posts from the custom post type also named News. I realize that having both with the same name causes issues, so when registering the custom post type, I have a rewrite to differentiate it from the page:
'rewrite' => array('slug' => 'news-article', 'with_front' => true),
I can get the query working and displaying the posts properly, but after all of the articles and posts I've read, I cannot get the pagination to work. Nothing ever shows up for the pagination.
After no success with using the page template query, I tried the archive-news.php method, where it would automatically display the posts from the custom post type. The pagination does work there. The downside of using this method is that there isn't a 'physical' page to tie it to (which would also have custom fields, ability to be nicely added (not hard-coded) into menus, etc.)
Here is the stripped-down code registering the custom post type:
register_post_type('news', array(
'label' => 'News',
'capability_type' => 'post',
'hierarchical' => false,
'rewrite' => array('slug' => 'news-article', 'with_front' => true),
'query_var' => true,
'has_archive' => true,
));
And then the code for the page template:
$paged = 1;
if ( get_query_var('paged') ) $paged = get_query_var('paged');
if ( get_query_var('page') ) $paged = get_query_var('page');
$args = array(
'post_type' => 'news',
'post_status' => 'publish',
'posts_per_page' => 1,
'paged' => $paged
);
$my_query = null;
$my_query = new WP_Query($args);
if($my_query->have_posts()):
while ($my_query->have_posts()) : $my_query->the_post();
...
endwhile;
endif;
wp_reset_query();
// Attempt method 1
posts_nav_link(' — ', __('« Newer Posts'), __('Older Posts »'));
// Attempt method 2
previous_posts_link('« Newer');
next_posts_link('Older »');
Any ideas what's wrong?

Have a look at next page link page, the example here will help. codex.wordpress.org/Template_Tags/next_posts_link
<?php next_posts_link('Older Entries »', 0); ?>
Wordpress codex example.
<?php
// set the "paged" parameter (use 'page' if the query is on a static front page)
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
// the query
$the_query = new WP_Query( 'cat=1&paged=' . $paged );
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php
// the loop
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php
// next_posts_link() usage with max_num_pages
next_posts_link( 'Older Entries', $the_query->max_num_pages );
previous_posts_link( 'Newer Entries' );
?>
<?php
// clean up after our query
wp_reset_postdata();
?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

Pagination Like : Prev 1 2 3 Next
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$news= new WP_Query(array(
'post_type'=>'post',
'posts_per_page' => 3,
'paged' => $paged,
));
if($news->have_posts()) :
while($news->have_posts()) : $news->the_post();
the_title();
endwhile;
$total_pages = $news->max_num_pages;
if ($total_pages > 1){
$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_text' => __('« prev'),
'next_text' => __('next »'),
));
}
?>
<?php else :?>
<h3><?php _e('404 Error: Not Found', ''); ?></h3>
<?php endif; ?>
<?php wp_reset_postdata();?>

Related

Custom wp_query with working pagination on a page template

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

Wordpress post query not working

I am trying to figure out a way to make the_posts_pagination or any alternative output numeric post navigation for a custom query.
but it doesn't seem to work, I don't know if I am doing anything wrong would appreciate suggestions and solutions thanks in advance
My Code
<?php global $query_string; // required
$posts = query_posts($query_string.'&posts_per_page=3&order=ASC'); ?>
<div class="main-post-loop">
<div class="big-thum-section img-is-responsive">
<?php if ( has_post_thumbnail() ) : ?>
<?php the_post_thumbnail('small-block-thumb'); ?>
<?php endif; ?>
</div>
<div class="squiggle-post-meta-section clearfix">
<h2> <?php the_title(); ?> </h2>
<div class="excerpt-post"><?php the_excerpt(); ?></div>
</div>
<div class="continue-reading-section">
Continue reading <i class="fa fa-chevron-right"></i>
</div>
<div class="squiggly-line"></div>
</div>
<?php
the_posts_pagination( array(
'mid_size' => 2,
'prev_text' => esc_html( '←' ),
'next_text' => esc_html( '→' ),
) );
?>
<?php wp_reset_query(); // reset the query ?>
In order to do that there are a few steps you need to do, first of all i suggest you use the wp-pagenavi plugin, it handles lots of things for you.
Any way, i explain both ways, with and without the plugin,
first we write our query and set the paged attribute according to paged query var, so when the user navigates to for example page 3, the query filters posts and shows the third page posts:
$paged = (int) ( get_query_var( 'paged' ) ?: ( get_query_var( 'page' )?: 1 ) );
$my_query = new WP_Query( array(
'posts_per_page' => 10,
'paged' => $paged // This is important for pagination links to work
) );
Now if you have decided to use the wp-pagenavi plugin, it's quite easy to make paginations with custom queries, all you need to do is :
<?php if( function_exists('wp_pagenavi') ) wp_pagenavi( array( 'query' => $my_query ) ); ?>
But if you wish to use the_posts_pagination() function, i'm not sure if it supports custom queries, but since it's using the paginate_links() function , it should work with this arguments
$args = array(
'current' => max( 1, $paged ), // $paged is what we defined earlier or you can use just get_query_var('paged')
'total' => $my_query->max_num_pages
)
if it doesn't, you can use the paginate_links() function itself with the same above arguments.
See also : this answer
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts(array('post_type' => 'post', 'order' => 'ASC', 'paged' => $paged, 'posts_per_page' => 12 ));
if( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php the_title();?>
<?php the_excerpt(); ?>
<?php endwhile; ?>
<div class="pagination"><?php my_pagination(); ?></div>
<?php endif; ?>
<?php wp_reset_query(); ?>
In your functions.php add,
if ( ! function_exists( 'my_pagination' ) ) :
function my_pagination() {
global $wp_query;
$big = 999999999; // need an unlikely integer
echo 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
) );
}
endif;
OR
Try this: http://www.wpbeginner.com/wp-themes/how-to-add-numeric-pagination-in-your-wordpress-theme/

Custom wordpress loop pagination not working

I have a website and i have a few loops from different categories on one page. the posts display like i want them to. At the bottom of the page i want to display all of the posts from the site. i have also got this working correctly. however the posts wont paginate, when i click on "newer posts" i am taken from this url: sitename/blog/ to sitename/blog/page/2/ but there is nothing found there and im getting an error
"Oops! That page can’t be found . which is found in my 404 page."
for my first two loops i have used the following code (with different queries)
<?php query_posts('cat=2&showposts=3'); ?>
however from research i have found this is the wrong way to do it and i should be querying posts like:
$the_query = new WP_Query($query_args);
So i have changed the query at the bottom of the page to this format, and temporarily removed the top two (wrong) queries. However im still getting the same error.
Could someone please help. My php knowledge is limited and this is stretching me. the full code i am using to display the post and paginate is:
<?php
$query_args = array(
'cat' => '3',
'post_type' => 'post',
'posts_per_page' => 6,
'paged' => $paged
);
// Get current page and append to custom query parameters array
$query_args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$the_query = new WP_Query($query_args);
// Pagination fix
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $the_query;
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="articleThirdBlock">
<div class="post__thumbnail post__thumbnail--marginTop">
<?php the_post_thumbnail(array(400,250)); ?>
</div><!-- /.post__thumbnail -->
<h2 class="other__title"><?php the_title(); ?></h2>
</div><!-- /.articleThirdBlock -->
<?php endwhile; endif; ?>
<?php wp_reset_postdata();
// Custom query loop pagination
previous_posts_link( 'Older Posts' );
next_posts_link( 'Newer Posts', $custom_query->max_num_pages );
// Reset main query object
$wp_query = NULL;
$wp_query = $temp_query;
?>
Custom Pagination Like « prev 1 2 3 next »
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type'=>'post', // Your post type name
'posts_per_page' => 6,
'paged' => $paged,
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div class="articleThirdBlock">
<div class="post__thumbnail post__thumbnail--marginTop">
<?php the_post_thumbnail(array(400,250)); ?>
</div><!-- /.post__thumbnail -->
<h2 class="other__title"><?php the_title(); ?></h2>
</div><!-- /.articleThirdBlock -->
<?php
endwhile;
$total_pages = $loop->max_num_pages;
if ($total_pages > 1){
$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_text' => __('« prev'),
'next_text' => __('next »'),
));
}
}
wp_reset_postdata();
?>

Wordpress pagination links reload page

I am trying to build a paginated archive sidebar using WP_Query. As the same sidebar is used on both single.php and news.php, the arguments used for the WP_Query are different.
On news.php, the pagination works perfectly, but on single.php the pagination links are present, and have the correct href values, but simply reload the page when clicked, i.e. anything beyond page 1 is inaccessible.
Update: Just in case this is relevant, single.php makes use of the Share Buttons by AddToAny, and Social (by MailChimp) plugins. I have deactivated both, but this has not fixed the issue with pagination.
Can anyone shed some light on this for me? Code here:
<aside>
<header>
<h1 class="column-title">Archive</h1>
<hr>
</header>
<div>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
// remove first four posts if this is the news main page
if ( ! is_single() ) {
// Save first four posts
$first_four = new WP_Query ('post_type=post&orderby=date&order=desc&posts_per_page=4');
if ( $first_four->have_posts() ) : while ( $first_four->have_posts() ) : $first_four->the_post();
$skipIDs[] = $post->ID;
endwhile; endif;
wp_reset_postdata();
// Save all posts
$args = array(
'post_type' => 'post',
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page' => -1
);
$all_posts = new WP_Query($args);
while ( $all_posts->have_posts() ) : $all_posts->the_post();
// Skip first four posts and save rest
if ( in_array($post->ID,$skipIDs) ) { continue; };
$offset_array[] = $post->ID;
endwhile;
wp_reset_postdata();
// Final arguments for WP_Query
$args = array(
'post__in' => $offset_array,
'paged' => $paged,
'posts_per_page' => 5
);
} else {
// Args for single.php
$args = array(
'post_type' => 'post',
'orderby' => 'date',
'order' => 'DESC',
'posts_per_page' => 5,
'paged' => $paged
);
}
$article_archive = new WP_Query($args);
$max_pages = $article_archive->max_num_pages;
if( $article_archive->have_posts() ) : while( $article_archive->have_posts() ) : $article_archive->the_post();
$has_image = get_the_post_thumbnail($post->ID,'thumbnail'); ?>
<article class="group">
<a class="anchor-overlay" href="<?php the_permalink(); ?>"></a>
<?php if( $has_image ) { echo $has_image; } else { echo "<img src='/wp-content/themes/DCSFC/images/news-calendar/news/no-image.jpg' alt='No image' />"; } ?>
<div>
<h3><?php the_title(); ?></h3>
<time datetime="dd/MM/YYYY"><?php echo get_the_date(); ?></time>
</div>
</article>
<?php endwhile; endif;
wp_reset_postdata();
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $max_pages
) );
?>
</div>

adding pagination to a custom post type in wordpress

I am facing a little issue while adding pagination to a custom post type which I've created inside Wordpress. The pagination links are appearing on the template where I've all the posts, inside the custom post type, listed but when I click on the link to view Older posts, it opens the second page but the same posts from the first page are displayed there. Moreover, on the second page, the 'Older posts' link doesn't update to '../page/3', instead it stays '../page/2'. I followed the steps specified here (https://stackoverflow.com/a/18325002/2115001) and modified my code according to the information listed under 'Option 2'. Here's what my code currently looks like:
<?php
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('showposts=3&post_type=medals'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();
// loops code here
endwhile;
echo '<nav>';
echo previous_posts_link('« Newer');
echo next_posts_link('Older »');
echo '</nav>';
$wp_query = null;
$wp_query = $temp; // Reset
?>
Do I need to add some code to the functions.php file in order for this to function properly or there's something wrong in my original code? Thanks.
Try below code
$paged = get_query_var('page') ? get_query_var('page') : 1;
$args = array('posts_per_page' => 3,
'paged'=> $paged,
'post_type' => 'medals');
$wp_query = new WP_Query($args);
while ($wp_query->have_posts()) : $wp_query->the_post();
// loops code here
endwhile;
global $wp_query;
$big = 999999999;
echo 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,
'next_text' => __('Next »'),
));
Try this:
<?php
$type = 'portfolio';
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args=array(
'post_type' => $type,
'post_status' => 'publish',
'paged' => $paged,
'posts_per_page' => 1,
'caller_get_posts'=> 1
);
$temp = $wp_query; // assign original query to temp variable for later use
$wp_query = null;
$wp_query = new WP_Query();
wp_query->query($args);
?>
then put your permalinks to the default structure and back to how the structure was.
<?php while ( $query->have_posts() ) : $query->the_post(); ?>
<article class="">
<div class="">
<h4><?php the_time( 'm' ); ?></h4>
<h4><?php the_time( 'd' ); ?></h4>
</div>
<div class="">
<h5><?php the_title(); ?></h5>
<p><?php the_excerpt(); ?></p>
</div>
</div>
</a>
</article>
<?php endwhile; // End the loop. Whew. ?>
<?php
global $wp_query;
$big = 999999999; // need an unlikely integer
//echo esc_url( get_pagenum_link());
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $query->max_num_pages,
'type'=>'list',
'prev_text' => __('<'),
'next_text' => __('>'),
) );
?>
Hope it will work for you.

Categories