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'
);
Related
I have to customize a function.
That fonction display custom posts on a (custom) category page.
The problem of this function concerns the absence of text when there is no results.
For instance, on any category page, if there is no available post to show in the grid, I would like to display a text such as "There is no post in this category. Please try another category."
Here is the code:
global $paged, $wp_query, $wp;
$args = wp_parse_args($wp->matched_query);
if ( !empty ( $args['paged'] ) && 0 == $paged ) {
$wp_query->set('paged', $args['paged']);
$paged = $args['paged'];
}
$cat_id = get_queried_object_id();
$temp = $wp_query;
$featuredPosts = array();
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => $classieraFeaturedAdsCounter,
'paged' => $paged,
'cat' => $cat_id,
'meta_query' => array(
array(
'key' => 'featured_post',
'value' => '1',
'compare' => '=='
)
),
);
$wp_query= null;
$wp_query = new WP_Query($args);
while ($wp_query->have_posts()) : $wp_query->the_post();
$featuredPosts[] = $post->ID;
get_template_part( 'templates/classiera-loops/loop-ivy');
endwhile;
wp_reset_postdata();
wp_reset_query();
Does anyone has an idea to include this option?
Thanks.
SOLUTION:
wp_query = new WP_Query($args);
if ( have_posts() ) :
while ($wp_query->have_posts()) : $wp_query->the_post();
$featuredPosts[] = $post->ID;
get_template_part( 'templates/classiera-loops/loop-ivy');
endwhile;
else :
echo wpautop('No result');
endif;
I'm not a WP developer, but it seems to be
if ($wp_query->have_posts()) {
while ($wp_query->have_posts()) : $wp_query->the_post();
} else {
echo 'No post here';
}
Reference link : https://codex.wordpress.org/Function_Reference/have_posts
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 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!
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 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.