Pagination is not working in static blog page in Wordpress - php

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);
}

Related

Wordpress second page pagination not showing posts

I've been trying to add numeric pagination to my custom wordpress theme. I have run into a problem where I can't see any posts on second(or third page). I have my page-archive.php file, index.php, single.php files all set up. It should be everything that a blog site needs? I'm a bit confused what am I missing here? I've been trying multiple different options and I tried to modify my page-archive.php page but no luck.
index.php
<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', // 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 // post content goes here ?>
<?php endwhile; ?>
<?php my_pagination(); ?>
<?php else : ?>
<?php // no posts found message goes here ?>
<?php wp_reset_query(); // add this ?>
<?php endif; ?>
</div>
</div>
functions.php
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;
page-archive.php
<?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(); ?>
It turns out that I don't need the first half of the code. Feeling dumb. Anyways here is the answer.
<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>

Wordpress custom pagination shows same posts on every page

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>

Pagination and custom WP Query

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,

Pagination does not work on custom post types

I have created custom post type 'portfolio' in my web-site. I am able to create categories,posts... etc. I have created category page which displays all the post according to the category and I have done it by using following chunk of code in archive.php
<?php $cat = get_query_var('cat');?>
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
<?php query_posts('showposts=3&post_type=portfolio&category_name=web-design&order=ASC&paged='.$paged); ?>
<div id="page" class="category_post grid_12">
<?php while (have_posts() ) : the_post(); ?>
<div class="grid_3 single-post">
<div class="list-post">
<a class="read-more" href="<?php echo get_permalink()?>"><?php echo the_post_thumbnail(array(213,185));?></a>
</div>
<p>
<?php $content=trim(get_field("description"));
echo $half_content=substr($content,0,88)."..." ?>
<a class="category-link" href="<?php echo get_permalink()?>">Read more</a>
</p>
</div>
<?php endwhile; ?>
<?php if(function_exists('wp_pagenavi')) { wp_pagenavi(); } ?>
Also I am using wp_pagenavi plugin for pagination. Now my problem is that when I click on page 2 i.e next page, it does not display the newer posts on it. Kindly tell me what's wrong with the above code.
Do your custom post type has has_archive property setted to true? Do you use archive template or tring to query posts in a template code?
Add custom_pagination function in your function.php file
`function custom_pagination($numpages = '', $pagerange = '', $paged='') {
if (empty($pagerange)) {
$pagerange = 2;
}
global $paged;
if (empty($paged)) {
$paged = 1;
}
if ($numpages == '') {
global $wp_query;
$numpages = $wp_query->max_num_pages;
if(!$numpages) {
$numpages = 1;
}
}
$pagination_args = array(
'base' => get_pagenum_link(1) . '%_%',
'total' => $numpages,
'current' => $paged,
'show_all' => False,
'end_size' => 1,
'mid_size' => $pagerange,
'prev_next' => False,
'prev_text' => __('«' , 'swift'),
'next_text' => __('»' , 'swift'),
'type' => 'array',
'add_args' => false,
'add_fragment' => ''
);
$paginate_links = paginate_links($pagination_args);
echo '<div class="Pagination-Num"><ul>';
foreach ( $paginate_links as $paginate_link ) {
echo "<li> $paginate_link </li>";
}
echo '</ul></div>';
}
`
And In your Custom page template add the following code...
<?php
/**
* Template Name: Custom Page
* The custom page template file
*/
?>
<?php get_header(); ?>
<h2>Posts</h2>
<?php
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$custom_args = array(
'post_type' => 'post',
'posts_per_page' => 2,
'paged' => $paged
);
$custom_query = new WP_Query( $custom_args ); ?>
<?php if ( $custom_query->have_posts() ) : ?>
<!-- the loop -->
<?php while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
<article class="loop">
<h3><?php the_title(); ?></h3>
<div class="content">
<?php the_excerpt(); ?>
</div>
</article>
<?php endwhile; ?>
<!-- end of the loop -->
<!-- pagination here -->
<?php
if (function_exists(custom_pagination)) {
custom_pagination($custom_query->max_num_pages,"",$paged);
}
?>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
<?php get_footer(); ?>
I am sure it will be work.

Wordpress query_posts('category_name='.get_the_title(). won't return posts for category with spaces

I have manipulated a wordpress "blog" template to show category specific posts by using the page title as reference. That way I can create a 'News' page and it auto populates the page with all the 'News' Category posts.
This code works perfectly as written on single word Titles, however it doesn't call any posts on multi word titles. ie: 'In The News'. The page doesn't break, it just says "Sorry, no posts matched your criteria".
Any thoughts?
<?php
/**
* Template Name: Category Posts Ultimate Template
*
* This template is the default page template. It is used to display content when someone is viewing a
* singular view of a page ('page' post_type) unless another page template overrules this one.
* #link http://codex.wordpress.org/Pages
*
* #package WooFramework
* #subpackage Template
*/
get_header();
global $woo_options;
/**
* The Variables
*
* Setup default variables, overriding them if the "Theme Options" have been saved.
*/
$settings = array(
'thumb_w' => 505,
'thumb_h' => 284,
'thumb_align' => 'alignleft',
'post_content' => 'excerpt'
);
$settings = woo_get_dynamic_values( $settings );
?>
<!-- #content Starts -->
<div id="content" class="page col-full">
<?php woo_main_before(); ?>
<section id="main" class="col-left">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
<h1><?php the_title(); ?></h1>
<?php the_content(); ?>
<?php endwhile; else: endif; ?>
<?php woo_loop_before(); ?>
<!-- Does the blog need 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_args = array(
'post_type' => 'post',
'paged' => $paged
);
remove_filter( 'pre_get_posts', 'woo_exclude_categories_homepage' );
// The Query
query_posts('category_name='.get_the_title().'&post_status=publish,future&paged=' . get_query_var('paged'));
if (have_posts()) {
$count = 0;
while (have_posts()) { the_post(); $count++;
?>
<!-- Post Starts -->
<article <?php post_class(); ?>>
<header>
<h1><?php the_title(); ?></h1>
</header>
<?php woo_post_meta(); ?>
<section class="entry fix">
<?php if ( isset( $settings['post_content'] ) && $settings['post_content'] != 'content' ) { woo_image( 'width=' . $settings['thumb_w'] . '&height=' . $settings['thumb_h'] . '&class=thumbnail ' . $settings['thumb_align'] ); } ?>
<?php global $more; $more = 0; ?>
<?php if ( isset( $settings['post_content'] ) && $settings['post_content'] == 'content' ) { the_content(__( 'Read More...', 'woothemes' ) ); } else { the_excerpt(); } ?>
</section>
<footer class="post-more">
<?php if ( isset( $settings['post_content'] ) && $settings['post_content'] == 'excerpt' ) { ?>
<span class="read-more"><?php _e( 'Continue Reading', 'woothemes' ); ?></span>
<?php } ?>
</footer>
</article><!-- /.post -->
<?php
} // End WHILE Loop
} else {
?>
<article <?php post_class(); ?>>
<p><?php _e( 'Sorry, no posts matched your criteria.', 'woothemes' ); ?></p>
</article><!-- /.post -->
<?php } // End IF Statement ?>
<?php woo_loop_after(); ?>
<?php woo_pagenav(); ?>
<?php wp_reset_postdata(); ?>
<?php wp_reset_query(); ?>
</section><!-- /#main -->
<?php woo_main_after(); ?>
<?php get_sidebar(); ?>
</div><!-- /#content -->
<?php get_footer(); ?>
It's not working because you can't have a space in your query. Instead of getting posts by category name, it would be better to use something like the category slug. This would not have any spaces in it and for something like 'In The News' it would probably be 'in-the-news'.
This will definitely depend on how your category slugs are named in comparison to the titles, but it's something that I have used with success in the past. So before executing your query we need to take the title and convert it to a new 'slug' that matches a category:
// Replace any spaces in the title with dashes
$title_slug = strtolower(str_replace( " ", "-" , the_title_attribute('echo=0') ) );
// Get the category object by using our new title slug
$id_obj = get_category_by_slug($title_slug);
// Get the category id so that we can use that in the query
$cat_id = $id_obj->term_id;
Then you would just change your query_posts to use the category id instead of the name:
query_posts( 'cat='.$cat_id.'&post_status=publish,future&paged='.get_query_var('paged') );
Again, this may or may not require a little tweaking of your title names and/or category slugs, but should work for you!

Categories