Wordpress custom pagination shows same posts on every page - php

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>

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>

Pagination for Custom Loop (loop with foreach)

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/

WordPress Pagination Problems - disappearing next_posts_link

I was hoping someone could help me address an issue I've been having trying to paginate a custom template in WordPress. I've scoured through Stackoverflow and other website forums looking for answers related to this, but no one seems to have problems exactly like the one I'm having.
I am currently making changes to a WordPress site built by a predecessor and on one of the pages shows our company portfolio. This showed all the posts we have published (as work) until I recently changed it to 12 per page. However, I can't seem to get the pagination to work or the posts_links top even appear on the page.
The page in question is http://thefabl.com/our-work and the code I currently have on the page (just from the top of the loop to the footer), is as stands below:
<?php
$cases = 1;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'work',
'posts_per_page' => 12,
'post_status' => 'publish',
'paged' => $paged,
'orderby' => 'date',
);
if ( isset( $_GET[ 'project' ] ) ) {
$args[ 'project' ] = $_GET[ 'project' ];
}
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post();
?>
<li <?php if ( ( $cases % 3 ) == 0 ) { echo ' class="end"'; } ?>>
<div class="overlay absolute-full">
</div>
<figure>
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail( 'work_list' );
} else {
echo '<img src="http://placehold.it/298x175" alt="Placeholder"/>';
}
?>
</figure>
<div class="post-meta">
<p class="title">
<?php the_title(); ?>
</p>
<span class="work-type">
<?php the_field( 'work_type' ); ?>
</span>
</div>
</li>
<?php
$cases++;
endwhile; ?>
<div class="main" id="post-pagination">
<?php if( has_previous_posts() ){ ?><div class="prev"><?php echo previous_posts_link( 'Previous Page' ); ?></div><?php } ?>
<?php if( has_next_posts() ){ ?><div class="next"><?php echo next_posts_link( 'Next Page' ); ?></div><?php } ?>
</div>
<?php endif; ?>
</ul>
</div>
<a class="back-to-top bottom center"></a>
Perhaps echo causing the problem. Please use following lines.
<div class="next"><?php next_posts_link('« Older Entries') ?></div>
<div class="prev"><?php previous_posts_link('Newer Entries »') ?></div>
Hope it will work for you.
and if you are showing posts on homepage, you may 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;
}

How do I reverse the order in PHP when Echoing

The goal here is to reverse the echoed php results so that the very first is the very last number.
I want the weeks to go 3-1 instead of 1-3 but for new posts to follow as well.
<?php
/* Get posts for home page */
if(is_page_template('template-home.php')):
$args = array(
'post_type'=>'post'
);
if(is_front_page()){
$args['paged'] = get_query_var('page');
global $paged;
$paged = $args['paged'];
} else {
$args['paged'] = get_query_var('paged');
}
$wp_query = new WP_Query($args);
endif;
?>
<div id="scroll-menu" class="scroll-menu"> <span class="week">WEEK</span>
<?php if ( have_posts() ) : $i=0; while ( have_posts() ) : the_post(); $i++; ?>
<div id="scroll-post-<?php the_ID(); ?>" data-id="post-<?php the_ID(); ?>" class="scroll-post">
<span><?php echo $i; ?></span>
<div class="scroll-menu-title"><?php the_title(); ?></div>
</div>
<?php endwhile; ?>
<?php endif;?>
<?php wp_reset_query(); ?>
</div>
Use
$args = array(
'post_type'=>'post'
'order' => 'ASC',
'orderby' => 'date'
);

Pagination is not working in static blog page in Wordpress

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

Categories