I use WP Beginner pagination (without plugin) on my website, but I can't access to second and other pages.
<?php
$category = get_category( get_query_var( 'cat' ) );
$cat_id = $category->cat_ID;
$custom_query_args = array(
'post_type' => array( 'tutorials','post' ),
'posts_per_page' => 2,
'cat' => $cat_id,
);
// Get current page and append to custom query parameters array
$custom_query_args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
// Instantiate custom query
$custom_query = new WP_Query( $custom_query_args );
// Pagination fix
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $custom_query;
// Output custom query loop
if ( $custom_query->have_posts() ) :
while ( $custom_query->have_posts() ) :
$custom_query->the_post();
echo '<article class="other-post col-xs-12 col-sm-12 col-md-3 col-lg-3">';
echo '<div class="back-color">';
echo '<h3>';
echo the_post_thumbnail('post-thumbnail', array( 'class' => 'post-image' ));
echo '<a href="'. get_permalink() .'" title="'. get_the_title() .'">';
echo '<span><b>'. get_the_title() .'</b></span>';
echo '</a>';
echo '</h3>';
echo '</div>';
echo '</article>';
endwhile;
endif;
// Reset postdata
wp_reset_postdata();
// Custom query loop pagination
wpbeginner_numeric_posts_nav();
// Reset main query object
$wp_query = NULL;
$wp_query = $temp_query;
?>
Here is code from function.php, but I think it isn't the problem.
http://virtual-wizard.eu/function.txt
You’re missing the paged parameter in your query args.
I would also replace the page query, so instead of this:
// Get current page and append to custom query parameters array
$custom_query_args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
You have this:
// Get current page and append to custom query parameters array
if ( get_query_var( 'paged' ) ) {
$paged = get_query_var( 'paged' );
} elseif ( get_query_var( 'page' ) ) {
$paged = get_query_var( 'page' );
} else {
$paged = 1;
}
And then change your query to this:
$custom_query_args = array(
'post_type' => array( 'tutorials','post' ),
'posts_per_page' => 2,
'cat' => $cat_id,
'paged' => $paged,
);
I hope that helps.
Try using this code instead. Make sure to read through as they are explaining how to implement this function into your WP website.
http://www.kriesi.at/archives/how-to-build-a-wordpress-post-pagination-without-plugin
Function is setup inside of a functions.php and on the specific page when you want to display pagination just call the function like pagination();
Before calling out pagination check also the CSS part and structure of the pagination to get the desired effect.
Srecno!
Related
I am working with a theme that has pagination on the home page for the blog posts. When I click on pg2 (or any page link) it refreshes but shows pg1 content. Is there something missing from the code below that keeps it from working correctly?
<?php
$makenzie_lite_args = array(
'post_type' => 'post',
'paged' => $paged,
'category_name' => 'blogPost'
);
$makenzie_lite_query = new WP_Query( $makenzie_lite_args );
if ( $makenzie_lite_query->have_posts() ) :
// amount of pages
$makenzie_lite_num_pages = $makenzie_lite_query->max_num_pages;
/* Start the Loop */
while ( $makenzie_lite_query->have_posts() ) : $makenzie_lite_query->the_post();
$makenzie_lite_layout = makenzie_lite_get_theme_mod( 'posts_style_template', 'post-s1' );
get_template_part( 'template-parts/listing/' . $makenzie_lite_layout );
endwhile;
else :
get_template_part( 'template-parts/content', 'none' );
endif;
// reset query
wp_reset_postdata(); ?>
EDIT:
I added this code I found on another SE post:
if ( get_query_var('paged') ) {
$paged = get_query_var('paged');
} elseif ( get_query_var('page') ) {
$paged = get_query_var('page');
} else {
$paged = 1;
}
and it works, but now the page numbers just expand as I click thru the pages. Go to ameliaislander.com to see what I mean.
Use get_query_var like this:
$makenzie_lite_args = array(
'post_type' => 'post',
'paged' => get_query_var( 'paged' ),
'category_name' => 'blogPost'
);
Scenario
I'm trying to understand where I have gone wrong with my loop in regards to using my pagination hook. I'm using the theme Understrap, with Understrap-child, and i've made no edits to the pre-built hook which can be found here.
I've used this hook in the loop listed below, which resides in a page template file. However the pagination hook isn't being output? The rest of the loop however, working perfectly fine.
Here's the kicker, I've used exactly the same structure within my index.php file. and it works completely fine. Which is driving me to believe I may need to declare something, just not sure as to what that may be.
Question
I'm wondering if while using a page template, and new WP_Query(); would I need to declare any globals to get $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1; to work?
Code in Question
For reference I've included my loop, though I'm pretty sure it's ok.
<div class="bg-light">
<div class="container space-top-3 space-bottom-2">
<div class="row">
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'post',
'orderby' => 'post_date',
'order' => 'desc',
'perm' => 'readable',
'show_post_views' => true,
'posts_per_page' => 9,
'paged' => $paged
);
$latestArticles = new WP_Query( $args );
if( $latestArticles->have_posts() ):
/* Start the Loop */
while( $latestArticles->have_posts() ) : $latestArticles->the_post();
get_template_part( 'loop-templates/content-search', get_post_format() );
endwhile;
endif;?>
</div>
<div class="row mt-3">
<div class="col-auto">
<!-- The pagination component (currently not being output. not sure why.) -->
<?php understrap_pagination(); ?>
</div>
</div>
<?php wp_reset_query(); ?>
<?php else : ?>
<!-- do nothing -->
<?php endif; ?>
</div>
</div>
Thanks for all help, in advance.
Edit-7/11/19:
Tried passing $latestArticles->max_num_pages to understrap_pagination();as such understrap_pagination($latestArticles->max_num_pages);. - Still no luck. :|
The pagination function relies upon $wp_query->max_num_pages (i.e. the max number of pages for the main query), so the function will not work with custom queries:
function understrap_pagination( $args = array(), $class = 'pagination' ) {
if ( $GLOBALS['wp_query']->max_num_pages <= 1 ) {
return;
}
...
}
And while your approach does work, it's really not a good idea to alter the global $wp_query object because doing so is essentially the same as using query_posts() which is not recommended at all — you can see why so by checking the docs here.
However, if you don't want to edit the pagination function (or create your own), then a less risky workaround to get the pagination works with your custom query, is by temporarily changing the — and only the — $wp_query->max_num_pages:
$orig_max_num_pages = $wp_query->max_num_pages; // backup
// Change it just for the pagination.
// and $latestArticles is your custom WP_Query instance.
$wp_query->max_num_pages = $latestArticles->max_num_pages;
$paged = max( get_query_var( 'paged' ), get_query_var( 'page' ), 1 );
understrap_pagination( [
'current' => $paged,
] );
$wp_query->max_num_pages = $orig_max_num_pages; // restore
Sample Page template based on your original code.
A Better Practice: Copy/Edit the pagination function.
Since you're using a child theme, and that Understrap uses locate_template() to load the file (inc/pagination.php) which defines the pagination function, you can just copy the file to the child theme (your-theme/inc/pagination.php) and edit the function so that it works with custom WP_Query queries. Or since the function is pluggable (i.e. defined with a if ( ! function_exists() ) block), you can just copy the code to your child theme's functions.php file.
You can use this modified function (which works well for me) or use it as a reference to create your own:
function understrap_pagination( $args = array(), $class = 'pagination' ) {
$args = wp_parse_args(
$args,
array(
'mid_size' => 2,
'prev_next' => true,
'prev_text' => __( '«', 'understrap' ),
'next_text' => __( '»', 'understrap' ),
'screen_reader_text' => __( 'Posts navigation', 'understrap' ),
'type' => 'array',
'current' => max( 1, get_query_var( 'paged' ) ),
'total' => $GLOBALS['wp_query']->max_num_pages,
)
);
// Nothing to paginate, so let's bail.
if ( ! $links = paginate_links( $args ) ) {
return;
}
?>
<nav aria-label="<?php echo $args['screen_reader_text']; ?>">
<ul class="pagination">
<?php
foreach ( $links as $key => $link ) {
?>
<li class="page-item <?php echo strpos( $link, 'current' ) ? 'active' : ''; ?>">
<?php echo str_replace( 'page-numbers', 'page-link', $link ); ?>
</li>
<?php
}
?>
</ul>
</nav>
<?php
}
Then in your template, call the function like so:
$paged = max( get_query_var( 'paged' ), get_query_var( 'page' ), 1 );
understrap_pagination( [
'current' => $paged,
'total' => $latestArticles->max_num_pages,
] );
Sample Page template based on your original code.
paged vs page
paged and page are both the number of the current page, however:
paged is used with archive-based requests/URLs such as category archives and search results pages. On these requests, page is not set by default. It should also be noted that paged is used with is_paged().
page is used with singular requests/URLs such as single Post and Page pages. On these requests, paged is not set by default.
And the Codex recommends the following approach to get the proper page number on singular requests/URLs:
if ( get_query_var( 'paged' ) ) { $paged = get_query_var( 'paged' ); }
elseif ( get_query_var( 'page' ) ) { $paged = get_query_var( 'page' ); }
else { $paged = 1; }
Or simpler version: $paged = max( get_query_var( 'paged' ), get_query_var( 'page' ), 1 );
NB: That Codex article refers to static front Page, but the information also applies to any Pages/Posts.
And last but not least… with secondary WP_Query calls (like your $latestArticles = new WP_Query( $args )), you just need to call wp_reset_postdata() and no need to call wp_reset_query(). :)
So, I was able to locate this post here by CSS-Tricks which outlines a method that nukes the current $wp_query variable and replaces it with a new instance of WP_Query();
My loop now works as expected however, I was under the impression that $wp_query is reserved, is that not correct? Is it safe to use this method?
My new loop looks like this;
<div class="bg-light">
<div class="container space-top-3 space-bottom-2">
<div class="row">
<?php
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$args = array(
'post_type' => 'post',
'orderby' => 'post_date',
'order' => 'desc',
'perm' => 'readable',
'show_post_views' => true,
'posts_per_page' => $posts_per_page,
'paged' => $paged
); ?>
<?php $wp_query->query($args); ?>
<?php if ( $wp_query->have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( $wp_query->have_posts() ) : $wp_query->the_post(); ?>
<?php get_template_part( 'loop-templates/content-search', get_post_format() ); ?>
<?php endwhile; ?>
</div>
<div class="row mt-3">
<div class="col-auto">
<!-- The pagination component -->
<?php understrap_pagination(); ?>
</div>
</div>
<?php else : ?>
<!-- do nothing -->
<?php endif; ?>
<?php $wp_query = null;
$wp_query = $temp; // Reset ?>
</div>
</div>
If anyone knows of a better approach I'd be interested in hearing it. Many thanks in advance. -B.
I need a bit help from you. I have a custom search engine to search products from a post type taxonomy :
if( isset($_POST['search_products'] ) {
/// codes ....
$_SESSION['ids'] = $my_ids;
$args = array(
'post_type' => 'product',
'showposts' => -1,
'post__in' => $_SESSION['ids']
)
$posts = new Wp_Query($args);
}
This query outputs about 60 products with pagination (10 products per page), but when user visits the page without using the search engine, all products should be displayed. Instead, the $_SESSION remains and display only the previous results.
I just want the pagination working when I do search, and all products displayed when I access the page without using the search engine.
Does any Wordpress expert have an idea ?
Thank you.
if( isset($_POST['search_products'] ) {
/// codes ....
$_SESSION['ids'] = $my_ids;
$args = array(
'post_type' => 'product',
'showposts' => -1,
'post__in' => $_SESSION['ids']
)
$posts = new Wp_Query($args);
}
else {
$args = array(
'post_type' => 'product',
'showposts' => -1,
)
$posts = new Wp_Query($args);
}
simply put an else block
CODE ,
global $post;
$id = intval($_GET['cat']);
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$products = new WP_Query( array(
'post_type' => 'products',
'order' => 'ASC',
'posts_per_page' => 5,
'paged' => $paged
) );
endwhile;
HTML ,
<ul class="pagination pull-right">
<li><?php echo get_next_posts_link( 'Next Page', $products->max_num_pages ); ?></li>
<li><?php echo get_previous_posts_link( 'Previous Page' ); ?></li>
</ul>
WordPress pagination not working after first wp_query. I am performing some operations on the data returned by first wp_query object. but after the second query, pagination is not working.
<?php
$prop_no = intval( get_option('wp_estate_prop_no', '') );
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'estate_property',
'author' => $current_user->ID,
'paged' => $paged,
'posts_per_page' => $prop_no,
'post_status' => array( 'any' ),
);
$prop_selection = new WP_Query($args);
//some code
$args = array(
'post_type' => 'estate_property',
'author' => $current_user->ID,
'paged' => $paged,
'posts_per_page' => $prop_no,
'post__in' => $sorted_posts,
'orderby' => 'post__in'
);
$prop_selection = new WP_Query($args);
?>
I tried removing the 'paged' parameter from one of the queries, that gives either wrong results or no pagination. ex. this query returns 28 results first time but only returns 6 results after second query. And if I remove the 'paged' parameter from first query and add it to second query then only one page is returned when it should return 3 pages.
Create Function in function.php
function sofg_pagination($max_num_pages,$paged,$page_id){
if($max_num_pages > 1){
echo '<div class="post-wrap pgns">';
echo '<ul class="pagination_list">';
echo '<li>First</li>';
for($i=1; $i<= $max_num_pages; $i++){
if($paged==$i){
echo '<li class="active">'.$i.'</li>';
}
else{ echo '<li>'.$i.'</li>'; }
}
echo '<li>Last</li>';
echo '</ul></div>';
}
}
Get Pagination Your listing page
Define Before loop
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$page_id = get_the_ID();
Use After Loop
$max_num_pages_1 = $max_num_pages->max_num_pages;
sofg_pagination($max_num_pages_1,$paged,$page_id);
See pagination http://hiddenwhy.igexsolutions.com/blog/
I am currently getting all the post from the db via a query. The php code included is part of a custom Wordpress template. I am using custom meta boxes from: https://github.com/WebDevStudios/Custom-Metaboxes-and-Fields-for-WordPress
what do i need to do to paginate every 6 post?
<?php
//get_template_part( 'content', 'page' );
echo'<h2 class="section-header-dark">'.get_the_title().'</h2><hr>';
//adjusting the query
$args = array(
'post_type' => 'blog',
'posts_per_page' => -1,
'orderby' => 'menu_order',
'order' => ASC
);
// The Query
$latest_post = new WP_Query( $args );
// The Loop
if ( $latest_post->have_posts() )
{
while ( $latest_post->have_posts() )
{
$latest_post->the_post();
$desc = wpautop( get_post_meta( get_the_ID(), '_cw_desc', true ) );
echo'<div class=""><h5 class="">'. get_the_title(). '</h5>';
echo $desc;
echo'<h6 class="news_date f_right"><i>'. get_the_date(). '</i></h6>';
echo'</div><hr>';
}
}
else
{
// no posts do nothing
}
wp_reset_postdata();
?>
You need to set 'posts_per_page' => 6 and add the paged Paramter to the query.
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => 6,
'paged' => $paged
);
?>
Visit http://codex.wordpress.org/Pagination for more details.