I allow my visitors to order posts by a price filter on my category.php. They have 2 options: "Max Price" and "Min Price". When a filter is selected the query works fine. Pagination appears but it doesn't work correctly. It always shows the first post on my "page/2/", "page/3/"...
I don't know why it doesn't work Can you help me?
<?php get_header(); ?>
<section id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<header class="pages-header col-md-12">
<h1 class="page-title"> <?php single_cat_title(); ?> </h1>
<?php echo get_field('petite_description_cat', 'category_' . $cat);?>
</header><!-- .page-header -->
<?php
//Display form for the query
if($_GET['minprice'] && !empty($_GET['minprice']))
{
$minprice = $_GET['minprice'];
} else {
$minprice = 0;
}
if($_GET['maxprice'] && !empty($_GET['maxprice']))
{
$maxprice = $_GET['maxprice'];
} else {
$maxprice = 999999;
}
?>
<form method="get">
<label>min:</label>
<input type="number" name="minprice" value="<?php echo $minprice; ?>">
<label>max:</label>
<input type="number" name="maxprice" value="<?php echo $maxprice; ?>">
<button type="submit" name="">Filter</button>
</form>
<?php
// set up or arguments for our custom query
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array(
'cat' => $cat,
'post_type' => 'post',
'posts_per_page' => 5,
'meta_query' => array(
array(
'key' => 'prix',
'type' => 'NUMERIC',
'value' => array($minprice, $maxprice),
'compare' => 'BETWEEN'
),
)
);
// create a new instance of WP_Query
$the_query = new WP_Query($args);
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); // run the loop ?>
<?php
//on récupère le template
get_template_part( 'content-category', get_post_format() ); ?>
<?php endwhile; ?>
<?php if ($the_query->max_num_pages > 1) { // check if the max number of pages is greater than 1 ?>
<div class="clearfix"></div>
<?php //pagination
bootstrap_pagination();?>
<?php } ?>
<?php else: ?>
<?php get_template_part( 'no-results', 'archive' ); ?>
<?php endif; ?>
<?php
wp_reset_query();
//display description only on page 1
$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
if ($page ==1) { echo category_description ( get_category_by_slug ( 'category-slug' )-> term_id );
} ?>
</main><!-- #main -->
</section><!-- #primary -->
<?php get_footer(); ?>
Solution is only this in $args !
'paged' => $paged,
Related
I have been trying to add custom numeric pagination to my custom Wordpress theme. Everything seems good so far but the problem is that every page shows the same 3 posts. Is there something I should consider doing while building my own Wordpress blog theme. Right now I have my page-archive.php and single.php file there, do I need something else for this to work? Also filtering with category isn't working, it keeps sending me back to index.php
Code in my index.php file
<div class="blogitem a">
<?php
//PRINT ONLY Tutvustus
$lastBlog = new WP_Query('type=post&posts_per_page=3');
if( $lastBlog->have_posts() ):
while( $lastBlog->have_posts() ): $lastBlog->the_post(); ?>
<?php get_template_part('page-archive',get_post_format()); ?>
<?php endwhile;
endif;
wp_reset_postdata();
?>
<div class="pagination">
<?php
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(array(
'post_type' => 'post',
'paged' => $paged,
'posts_per_page' => 2
));
if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php?>
<?php endwhile; ?>
<?php my_pagination(); ?>
<?php else : ?>
<?php ?>
<?php endif; ?>
</div>
</div>
Code in my functions.php file
if ( ! function_exists( 'my_pagination' ) ) :
function my_pagination() {
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
) );
}
endif;
I modified my page-archives.php file to this code.
<?php
/*
Template Name: Archives
*/
get_header(); ?>
<div id="container">
<div id="content" role="main">
<?php the_post(); ?>
<h1 class="entry-title"><?php the_title(); ?></h1>
</div><!-- #content -->
</div><!-- #container -->
<?php get_footer(); ?>
Now my filtering with category is working but if I choose second page from the pagination it doens't show any posts.
I think you need wp_reset_query... some helpful links:
https://developer.wordpress.org/reference/functions/wp_reset_query/
https://developer.wordpress.org/reference/functions/query_posts/
see example here:
<div class="blogitem a">
<?php
//PRINT ONLY Tutvustus
$lastBlog = new WP_Query('type=post&posts_per_page=3');
if( $lastBlog->have_posts() ):
while( $lastBlog->have_posts() ): $lastBlog->the_post(); ?>
<?php get_template_part('catalog',get_post_format()); ?>
<?php endwhile;
endif;
wp_reset_postdata();
?>
<div class="pagination">
<?php
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(array(
'post_type' => 'post',
'paged' => $paged,
'posts_per_page' => 2
));
if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php?>
<?php endwhile; ?>
<?php my_pagination(); ?>
<?php else : ?>
<?php ?>
<?php endif; ?>
<?php wp_reset_query(); // add this ?>
</div>
</div>
It turns out I am silly and I don't need the first half of the code anyways.
<div class="pagination">
<?php
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(array(
'post_type' => 'post', // You can add a custom post type if you like
'paged' => $paged,
'posts_per_page' => 2
));
if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part('catalog',get_post_format()); ?> // added template part here and voila it works
<?php endwhile; ?>
<?php my_pagination(); ?>
<?php else : ?>
<?php ?>
<?php wp_reset_query(); // add this ?>
<?php endif; ?>
</div>
I'm trying to add pagination to a custom loop, but I can't figure out how to do it. When I manage to add "previews" and "next" buttons it always shows the same 10 posts. I found some solutions for a while loop but not for a foreach loop (which I actually never used before).
This is the loop (is get_posts a problem?) :
<?php
$news = get_posts(array('posts_per_page' => 10));
$news['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; ?>
<?php foreach ($news as $article): ?>
<div class="col-md-4">
<h3><?php echo $article->post_title ?></h3>
<hr>
<p class="desc"><?php echo $article->post_excerpt ?></p>
<?php echo get_the_post_thumbnail($article->ID,'thumbnail'); ?>
<p class="btn_text"> Ler mais</p>
</div>
<?php endforeach; ?>
<?php previous_posts_link( '<<' );
next_posts_link( '>>', $custom_query->max_num_pages ); ?>
<?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;
}
$custom_query_args = array(
'post_type' => 'post',
'posts_per_page' => get_option('posts_per_page'),
'paged' => $paged,
'post_status' => 'publish',
'ignore_sticky_posts' => true,
//'category_name' => 'custom-cat',
'order' => 'DESC', // 'ASC'
'orderby' => 'date' // modified | title | name | ID | rand
);
$custom_query = new WP_Query( $custom_query_args );
if ( $custom_query->have_posts() ) :
while( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
<article <?php post_class(); ?>>
<h3><?php the_title(); ?></h3>
<small><?php the_time('F jS, Y') ?> by <?php the_author_posts_link() ?></small>
<div><?php the_excerpt(); ?></div>
</article>
<?php
endwhile;
?>
<?php if ($custom_query->max_num_pages > 1) : // custom pagination ?>
<?php
$orig_query = $wp_query; // fix for pagination to work
$wp_query = $custom_query;
?>
<nav class="prev-next-posts">
<div class="prev-posts-link">
<?php echo get_next_posts_link( 'Older Entries', $custom_query->max_num_pages ); ?>
</div>
<div class="next-posts-link">
<?php echo get_previous_posts_link( 'Newer Entries' ); ?>
</div>
</nav>
<?php
$wp_query = $orig_query; // fix for pagination to work
?>
<?php endif; ?>
<?php
wp_reset_postdata(); // reset the query
else:
echo '<p>'.__('Sorry, no posts matched your criteria.').'</p>';
endif;
?>
Source : http://web-profile.net/wordpress/themes/wordpress-custom-loop/
I'm displaying blog posts on a page and have enabled pagination. However, the pagination doesn't seem to quite work.
When you navigate to the second page it displays the same posts as on the first page.
This is my code:
<div class="container-max">
<?php $paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : '1'; ?>
<?php
$args = array( 'numberposts' => 5, 'offset' => 2, 'post_status'=>"publish",'post_type'=>"post",'orderby'=>"post_date",'cat'=>'-8, -9, -7, -6, -5, -4','paged'=> $paged);
$postslist = get_posts( $args );
echo '<div class="latest_new_posts main-news">';
?>
<?php
$wp_query = new WP_Query($args);
?>
<?php if ( $wp_query->have_posts() ) : ?>
<?php while ( $wp_query->have_posts() ) : the_post(); ?>
<?php foreach ($postslist as $post) : setup_postdata($post); ?>
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12" style="position:relative; margin:45px 0;" >
<div class="blog-date-news main-news-date"><span><?php the_date('d/m/Y'); ?></span></div>
<div class="blog-container main-news-container">
<div class="news-blog-title"><span><?php the_title(); ?></span></div>
<div class="news-blog-excerpt"> <?php echo excerpt(500); ?> </div>
<div class="news-blog-more main-news-blog-more"> <img src="http://www.mariadev.co.uk/wp-content/uploads/2017/05/read-more.png"/></div>
</div>
</div>
<?php endforeach; ?>
<?php endwhile; ?>
<div class="blog-page-next"><?php next_posts_link( 'Next', $wp_query->max_num_pages );
?> </div>
<?php wp_reset_postdata(); ?>
<?php else : ?>
<p><?php __('No News'); ?></p>
<?php endif; ?>
</div>
</div>
Any ideas what I'm doing wrong?
Replace 'numberposts' => 5 with 'posts_per_page' => 5
Can you use query_posts() ?
$postslist = get_posts( $args );
replace
$postslist = query_posts( $args );
I'm using this code in different sections of my website and I want to display different posts in each section of the front page according to their category.
Can someone please help em to add a category there one is "surf" , I've tried everything.
Or maybe a different way to do it?
Thank you.
<div class="posts">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$total_post_count = wp_count_posts();
$published_post_count = $total_post_count->publish;
$total_pages = ceil( $published_post_count / $posts_per_page );
if ( "1" < $paged ) : ?>
<div class="page-title section small-padding">
<h4 class="section-inner"><?php printf( __('Page %s of %s', 'radcliffe'), $paged, $wp_query->max_num_pages ); ?></h4>
</div>
<div class="clear"></div>
<?php endif; ?>
<?php while (have_posts()) : the_post(); ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php get_template_part( 'content', get_post_format() ); ?>
</div> <!-- /post -->
<?php endwhile; ?>
<?php if ( $wp_query->max_num_pages > 1 ) : ?>
<div class="archive-nav">
<?php echo get_next_posts_link( '« ' . __('Posts Antigos', 'radcliffe')); ?>
<?php echo get_previous_posts_link( __('Posts Recentes', 'radcliffe') . ' »'); ?>
<div class="clear"></div>
</div> <!-- /post-nav archive-nav -->
<?php endif; ?>
<?php endif; ?>
</div> </div> <!-- /posts -->
If you are trying to display category by ID , Then
global $post;
$args = array( 'category' => '12' );
$cat_post= get_posts( $args );
foreach( $cat_post as $post ) : setup_postdata($post); ?>
<li class="testimonial"><?php the_content(); ?></li><br/>
<?php endforeach; ?>
Note: In $args = array( 'category' => '12' ); 12 is the ID of
the category
But if you want to display category by Name, Then
global $post;
$args = array( 'category_name' => 'uncatogerized' );
$cat_post= get_posts( $args );
foreach( $cat_post as $post ) : setup_postdata($post); ?>
<li class="testimonial"><?php the_content(); ?></li><br/>
<?php endforeach; ?>
Here, uncategorized is a category name
I am using the default theme in Wordpress. When I set my blog page to static and select my Blog List template as the page template, it will not navigate to other pages using the paginated links.
The URL shows that it moved to page two, but it is showing the same page (i.e. not the next x number of posts).
I have Googled but I did not find a satisfactory answer. Some posts suggest trying some code. I tried what they suggested but nothing works for me,
My blog list template code is below:
<?php
/*
Template Name: Blog List
*/
?>
<?php get_header(); ?>
<div id="container">
<div class="main<?php if ( is_active_sidebar( 'home-sidebar-small' ) ) : ?> small-sidebar<?php endif; ?>">
<?php if ( is_active_sidebar( 'home-sidebar-small' ) ) : ?>
<div class="sidebar-small">
<?php dynamic_sidebar( 'home-sidebar-small' ); ?>
</div><!-- Sidebar Small -->
<?php endif; ?>
<div class="content">
<div class="warp">
<?php if(bdayh_get_option('article_crumbs') == 1) { ?>
<div class="pp-breadcrumbs bottom10">
<?php bd_breadcrumbs() ?>
</div><!--//end breadcrumbs-->
<hr class="bottom15">
<?php } ?>
<img alt="Amir Anzur" src="http://amiranzur.com/images/Capture.PNG"/>
<br/><br/><br/>
<?php
if(bdayh_get_option('disable_custom_template_blog') == 1) {
query_posts(
array(
'cat' => bdayh_get_option('custom_template_blog_category'), // Enter your ID number
'paged' => get_query_var('paged'),
'post_type' => 'post',
)
);
} else {
query_posts('posts_per_page=3&paged=' . $paged);
}
//rewind_posts();
get_template_part( 'loop-archive', 'category' );
if ($wp_query->max_num_pages > 1) bd_pagenavi();
if (comments_open() && !post_password_required()) {
comments_template('', true);
}
?>
</div>
</div><!-- content -->
</div>
</div>
<!-- container -->
<div id="sidebar">
<?php if (function_exists('dynamic_sidebar') && dynamic_sidebar('Page Sidebar')){ }else { ?>
<?php get_sidebar(); ?>
<?php } ?>
</div><!-- sidebar /-->
<?php get_footer(); ?
>
You use the variable $paged here yet I don't see where it's defined. Try changing this:
if ( get_query_var( 'paged' ) ) {
$paged = get_query_var( 'paged' );
} elseif ( get_query_var( 'page' ) ) {
$paged = get_query_var( 'page' );
} else {
$paged = 1;
}
if(bdayh_get_option('disable_custom_template_blog') == 1) {
query_posts(
array(
'cat' => bdayh_get_option('custom_template_blog_category'), // Enter your ID number
'paged' => $paged,
'post_type' => 'post',
)
);
} else {
query_posts('posts_per_page=3&paged=' . $paged);
}