WP Pagination Loop not working in custom theme - php

Pagination loop not working in custom theme Home page template. Shows same content in all pages.
You can also suggest me some other code to fix the issue.
<?php
// clear any other queries that may be in use!
wp_reset_query();
// check for $_GET paged value
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
// setup post arguments
$args = array( 'posts_per_page' => 7, 'paged' => $paged, );
// run our query
query_posts($args);
// start loop
if (have_posts()) : while (have_posts()) : the_post();
// if you use the <!-- more --> in your posts.
global $more;
$more = 0;
?>
<div class="post">
<?php the_title(); ?>
</div>
<?php endwhile; ?>
<div class="navigation">
<?php next_posts_link('Next'); ?>
<?php previous_posts_link('Previous'); ?>
</div>
<?php else: ?>
<div><h2>Nothing found</h2><p>No posts found for that query</p></div>
<?php endif; ?>

If above answer wont work for you, use WP_Query instead of query_posts.
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'posts_per_page' => 3,
'paged' => $paged
);
$the_query = new WP_Query( $args );
?>
If the pagination is broken on a static front page you have to add the "paged" parameter this way:
if ( get_query_var( 'paged' ) ) { $paged = get_query_var( 'paged' ); }
elseif ( get_query_var( 'page' ) ) { $paged = get_query_var( 'page' ); }
else { $paged = 1; }

query_posts() isn't meant to be used by plugins or themes. Use WP_Query instead. It accepts the same parameters as query_posts does. Be aware that neither of these methods is the most efficient way to alter the default query. In fact, either method can also be responsible for breaking pagination.
If your theme is using either of these methods to query the main loop, you can replace it with the preferred way, that is to say, hooking into 'pre_get_posts' and altering the main query by using is_main_query(). This way is faster and more reliable because the query for the main loop is altered before the posts are retrieved from the database.
For example, lets say your theme queries the main loop like this on your home page and the pagination is not working:
<?php
// clear any other queries that may be in use!
wp_reset_query();
// check for $_GET paged value
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
// setup post arguments
$args = array( 'posts_per_page' => 7, 'paged' => $paged, );
// run our query
query_posts($args);
// start loop
if (have_posts()) : while (have_posts()) : the_post();
// if you use the <!-- more --> in your posts.
global $more;
$more = 0;
?>
<div class="post">
<?php the_title(); ?>
</div>
<?php endwhile; ?>
<div class="navigation">
<?php next_posts_link('Next'); ?>
<?php previous_posts_link('Previous'); ?>
</div>
<?php else: ?>
<div><h2>Nothing found</h2><p>No posts found for that query</p></div>
<?php endif; ?>
Remove the query_posts part from your code:
<?php
// clear any other queries that may be in use!
wp_reset_query();
// check for $_GET paged value
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
// setup post arguments
$args = array( 'posts_per_page' => 7, 'paged' => $paged, );
// run our query
query_posts($args);
?>
And add the query for your home page back in your theme's functions.php file:
function my_post_queries( $query ) {
// do not alter the query on wp-admin pages and only alter it if it's the main query
if (!is_admin() && $query->is_main_query()){
// alter the query for the home and category pages
if(is_home()){
$query->set('posts_per_page', 7);
}
if(is_category()){
$query->set('posts_per_page', 3);
}
}
}
add_action( 'pre_get_posts', 'my_post_queries' );
You can use conditional tags to target the pages where we want to alter the query. Like,
// alter the query for the Movies category page
if(is_home() || is_front_page){
$query->set('posts_per_page', 7);
}

<?php
// Get current page and append to custom query parameters array
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
// Define custom query parameters
$args = array( 'posts_per_page' => 5, 'paged' => $paged );
$custom_query_args = array( $args );
// 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();
?>
<div class="blog-post">
<?php the_post_thumbnail('post-thumbnail'); ?>
<?php endwhile;
endif;
// Reset postdata
wp_reset_postdata();
echo "<div class='blog-nav'>";
// Custom query loop pagination
previous_posts_link( 'Older Posts' );
next_posts_link( 'Newer Posts', $custom_query->max_num_pages );
$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' => $custom_query->max_num_pages
) );
// Reset main query object
echo "</div>";
$wp_query = NULL;
$wp_query = $temp_query;
?>

Related

Add a class to the body - PHP Wordpress

I am trying to add a class to the body in a WordPress theme so that when there are no posts on a page it will not show the search bar. I have in my functions.php right now this to look for one certain page, and if there are no posts.
<?php
add_filter( 'body_class', 'custom_class' );
function custom_class( $classes ) {
if(strpos($_SERVER['REQUEST_URI'], 'agent') !== false ){
$classes[] = 'noSearchBar';
}
if(!have_posts() ){
$classes[] = 'noSearchBar';
}
return $classes;
}
?>
Which works for most of the pages, but on some pages it is using a different template so instead of calling the posts with have_posts like this:
<?php
global $wp_query;
if ( have_posts() ) :
while ( have_posts() ) : the_post();
get_template_part('template-parts/property-for-listing');
endwhile;
wp_reset_postdata();
else:
?>
<h4><?php esc_html_e('Sorry No Results Found', 'houzez') ?></h4>
<?php
endif;
?>
Its is bringing it up like this:
<?php
global $wp_query, $paged;
if(!$fave_prop_no){
$posts_per_page = 9;
} else {
$posts_per_page = $fave_prop_no;
}
$latest_listing_args = array(
'post_type' => 'property',
'posts_per_page' => $posts_per_page,
'paged' => $paged,
'post_status' => 'publish'
);
$latest_listing_args = apply_filters( 'houzez_property_filter', $latest_listing_args );
$latest_listing_args = houzez_prop_sort ( $latest_listing_args );
$wp_query = new WP_Query( $latest_listing_args );
if ( $wp_query->have_posts() ) :
while ( $wp_query->have_posts() ) : $wp_query->the_post();
get_template_part('template-parts/property-for-listing');
endwhile;
else:
get_template_part('template-parts/property', 'none');
endif;
?>
Which is making my function not work. There is only 1 line of code in the template-parts/property-none and its just saying the same thing the other files are. So I'm not sure why the other template would not add the body class.
You can do like this example,
<?php
add_filter( 'body_class', 'custom_class' );
function custom_class( $classes ) {
if ( is_single('post') ) {
$classes[] = 'SearchBar';
}else{
$classes[] = 'noSearchBar';
}
return $classes;
}
?>
The following example will work for single post page. For more conditions.visit
Hope this will helps you.

How to show post from just two categories on Wordpress Home page?

Only two categories need to be showed in the homepage. Can anyone help.
You can use WP_Query to get your posts list, and display it with the loop
Example :
$the_query = new WP_Query( array( 'category_name' => 'staff,news' ) );
// The Loop
if ( $the_query->have_posts() ) {
echo '<ul>';
while ( $the_query->have_posts() ) {
$the_query->the_post();
echo '<li>' . get_the_title() . '</li>';
}
echo '</ul>';
/* Restore original Post Data */
wp_reset_postdata();
} else {
// no posts found
}
In your functions.php file paste the below code:
I am assuming that you want to show categories from two categories which are having ids 5 and 9.
function kiran_home_category( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'cat', '5,9');
}
}
add_action( 'pre_get_posts', 'kiran_home_category' );
Explanation:
kiran_home_category is just a custom name for the function. That can be any name. The way it works is you attach a function to the action hook pre_get_posts. So before getting the posts the function kiran_home_category will be called. And then inside the function I am changing the query here to only load categories with ID 5 and 9
In wordpress WP_query, category__in parameter used to select category with posts.
<?php
$query = new WP_Query( array( 'category__in' => array( 2, 6 ),'post_status'=>'publish','orderby'=>'menu_order','order'=>'Asc' ) );
if($query->have_posts()):
echo '<ul>';
while ( $query->have_posts() ) : the_post();
echo '<li>' . get_the_title() . '</li>';
endwhile;
echo '</ul>';
endif;
?>
For more information about wordpress query click here , you can read more information.
<?php
$args = array( 'post_type' => 'post', 'posts_per_page' => -1,'category_name' => array('Latest News','News') );
$loop = new WP_Query( $args );
if($loop->have_posts()):
?><ul>
<?php
while ( $loop->have_posts() ) : $loop->the_post();
?>
<li> <span class="date"><?php echo get_the_date( 'd F Y');?></span>
<h3><?php echo get_the_title();?></h3>
<?php echo $description = get_the_content(); ?>
</li>
<?php endwhile;?>
</ul>
<?php endif;?>
<?php wp_reset_postdata(); ?>
Do the following, usually in page.php or single.php or if you want a custom page for a category, you can do, category-samplecat.php..
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'category_name' => array('samplecat', 'anothercat'),
'paged' => $paged
);
$arr_posts = new WP_Query($args);
Then do the usual if, while statement..
if($arr_posts->have_posts() ) :
// Start the loop.
while ( $arr_posts->have_posts() ) :
$arr_posts->the_post();?>
<?php endwhile;
endif;

Adding Pagination to WordPress Page Templates

I built a wordpress theme from an existing theme (One Engine Theme). The site is launched and all seems to be working well except pagination. I have tried so many things but failed to get it to work. All the template pages are meant to paginate at some point but no one seem to work. Below is the looping code for the blog page template:
<?php
$newsposts = get_posts();
foreach($newsposts as $post) :
setup_postdata($post); ?>
//the html codes are added here
<?php endforeach; ?>
I want to make post per page to be 4 and also add the default wordpress pagination function:
<?php posts_nav_link(); ?>
The page template here is blog.php, you can see the page in action here: www.kayodeolusoji.net/blog
You help will be well appreciated!
Try this:
I add some 'paged' atrribute in $args wiht WP_Query
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$posts_per_page=4;
$args = array(
'paged' => $paged,
'posts_per_page'=>$posts_per_page
);
$newsposts = new WP_Query($args);
while($newsposts->have_posts()) :$newsposts->the_post();
echo $post->ID.'<br />';
endwhile;
wp_reset_query();
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' => $newsposts->max_num_pages
) );
I was able to fix after several trials. Here is what I did.
<?php
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('showposts=4'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
//the html codes are added here
<?php endwhile; ?>
<nav>
<?php //the pagination codes ?>
</nav>
<?php
$wp_query = null;
$wp_query = $temp; // Reset
?>

Pagination in wp_query

This may be an easy but I cannot get it figured out. I have searched for hours with no luck on my situation. I need to get pagination to work with my_query
<?php
$count = 0;
$id_suffix = 1;
$items_per_row = 4;
$quality = 90;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$wp_query = new WP_Query( array( 'posts_per_page' => '4', 'post_type' => 'portfolio') );
$grid_class = 'grid_3';
$desired_width = 220;
$desired_height = 190;
$terms = get_terms( 'portfolio_categories' );
$count_terms = count( $terms );
?>
//some php code
<?php while ( $wp_query -> have_posts()) : $wp_query -> the_post(); //query the "portfolio" custom post type for portfolio items ?>
//some more php code
<?php endwhile;?>
<div class="nav-previous"><?php next_posts_link(__('<span class="meta-nav">«</span> Older posts', 'thematic')) ?></div>
<div class="nav-next"><?php previous_posts_link(__('Newer posts <span class="meta-nav">»</span>', 'thematic')) ?></div>
</ul>
I got the pagination to display but when clicking on another page it shows the same portfolio items. Any help would be appreciated.
I got the pagination to show using wp-pagenavi but the same issue with not changing the items.
You are constructing the $paged variable but aren't using it.
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$wp_query = new WP_Query(
array(
'posts_per_page' => '4',
'post_type' => 'portfolio',
'paged' => $paged // this is the missing part
) );
WordPress uses the $wp_query variable name. You should probably use a different one.
Thank you #maiorano84 and #s_ha_dum for pointing me in the right direction. I wanted to update this post for anybody that runs into the problem. The problem I was having was making the pagination work on the a static front page. After reading in the codex I figured out my problem.
Instead of $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
I had to use
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
That one little mistake is what kept the pagination to work. so the final code looked like this.
<?php
$count = 0;
$id_suffix = 1;
$items_per_row = 4;
$quality = 90;
$paged = (get_query_var('page')) ? get_query_var('page') : 1;
$my_query = new WP_Query( array(
'posts_per_page' => 8,
'post_type' => 'portfolio',
'paged' => $paged
) );
$grid_class = 'grid_3';
$desired_width = 220;
$desired_height = 190;
$terms = get_terms( 'portfolio_categories' );
$count_terms = count( $terms );
?>
//Some php code
<?php while ( $my_query -> have_posts()) : $my_query -> the_post(); //query the "portfolio" custom post type for portfolio items ?>
(Some more php code)
<?php endwhile;
wp_pagenavi(array( 'query' => $my_query ) ); ?>
</ul><!-- END .portfolio-gallery -->
?>

Previous / Next page link using wp_query loop

I am working on a wordpress site wich has a custom post type called, let's say 'plops'. I only want to use this custom post type in the website. So in the index.php, I am looping through those with a WP_Query, here is the code :
<?php
$args = array( 'post_type' => 'plops', 'posts_per_page' => 30, 'orderby' => 'desc');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
$meta = get_post_meta($post->ID);
?>
// my template for the post...
<?php endwhile; ?>
The thing is I want to implement an infinite scroll to this page, and for that I need to have pagination links. I tried to implement those with the next_posts_link() function, but can't make it work !
It just won't display any link, and I tried a lot of stuff, nothing seems to make it work...
if i go to mysite.com/worpress/page/2, My posts do display, but I get a 404 in firebug... weird...
Any ideas ? Would really appreciate help ! Thanks a lot in advance !
The fix below might help you - this worked for me. It combines Chris Coyier's solution here with some help found in the comments. CSS Tricks Article
//Fix homepage pagination
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' => 'custom_post_type', 'paged' => $paged );
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query( $args );
while($wp_query->have_posts()) : $wp_query->the_post();
?>
<!-- LOOP: Usual Post Template Stuff Here-->
<?php endwhile; ?>
<nav>
<?php previous_posts_link('« Newer') ?>
<?php next_posts_link('Older »') ?>
</nav>
<?php
$wp_query = null;
$wp_query = $temp; // Reset
?>
It has been a long time since this question is posted, but for anyone who is looking for a way to achieve this in his/her own code:
$args = array( 'post_type' => 'plops', 'posts_per_page' => 30, 'order' => 'desc');
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) {
$loop->the_post();
$meta = get_post_meta($post->ID);
// my template for the post...
}
previous_posts_link('« Newer');
next_posts_link('Older »', $loop->max_num_pages);
wp_reset_postdata();
Note: You have to pass the max number of pages to the next_posts_link function only.

Categories