The pagination on a site I am working on is not working; I really can't work out why as it is the same as the other templates within the site. I am wondering if there is a problem with the loop, perhaps where I have specified what category I want it to pull.
This is the page I am having trouble with:
<div class="container content newspage">
<div class="two_third newsarticles">
<?php /* Start the Loop */ ?>
<?php $catquery = new WP_Query( 'cat=388' ); while($catquery->have_posts()) : $catquery->the_post(); ?>
<?php get_template_part( 'content-archive', get_post_format() ); ?>
<?php endwhile; ?>
<nav id="nav-below">
<div class="nav-previous">
<?php next_posts_link( __( '<span class="meta-nav">←</span> Older posts', 'anariel' ) ); ?>
</div>
<div class="nav-next">
<?php previous_posts_link( __( 'Newer posts <span class="meta-nav">→ </span>', 'anariel' ) ); ?>
</div>
</nav>
<!-- end nav-below -->
</div>
<!-- end two_third -->
<div class="sidebar">
<?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Default Sidebar') ) : ?>
<?php endif; ?>
</div>
</div>
<!-- end container -->
<?php get_footer(); ?>
The template that does work correctly is here:
<div class="container content newspage">
<div class="two_third newsarticles">
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php /* Display navigation to next/previous pages when applicable */ ?>
<?php if ( $wp_query->max_num_pages > 1 ) : ?>
<nav id="nav-below">
<div class="nav-previous">
<?php next_posts_link( __( '<span class="meta-nav">←</span> Older posts', 'anariel' ) ); ?>
</div>
<div class="nav-next">
<?php previous_posts_link( __( 'Newer posts <span class="meta-nav">→ </span>', 'anariel' ) ); ?>
</div>
</nav>
<!-- end nav-below -->
<?php endif; ?>
</div>
<!-- end two_third -->
<aside>
<div class="one_third lastcolumn newssidebar">
<?php get_sidebar(); ?>
</div>
</aside>
</div>
<!-- end container -->
<?php get_footer(); ?>
I have tried copying this over into the template that doesn't work without success.
When you're using a custom query, you have to add the page number to your arguments.
<?php /* Start the Loop */ ?>
<?php
$paged = get_query_var('paged') ? get_query_var('paged') : 1;
$catquery = new WP_Query( array('cat' => 388, 'paged' => $paged) ); while($catquery->have_posts()) : $catquery->the_post(); ?>
You should also pass the number of pages to get_next_posts_link
next_posts_link( __( '<span class="meta-nav">←</span> Older posts', 'anariel' ), $catquery->max_num_pages );
Related
In my wordpress I have many posts, every page I am showing 5 posts.
Bottom of my page I have next and prev button. When I click one the next button it will go to /page/2/ link but this page title is showing Page not found. And it's not showing other posts in page 2.
My next and prev code :
<div class="prev-next-btn">
<?php next_posts_link( __( 'next', 'themename' ) ); ?>
</div>
<div class="prev-next-btn">
<?php previous_posts_link( __( 'prev', 'themename' ) ); ?>
</div>
My index.php code :
<div class="center-content">
<ul>
<?php query_posts('posts_per_page=5'); ?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<li>
<div class="date">In
<?php
$category = get_the_category();
if(!empty($category))
echo ''.$category[0]->cat_name.'';
?>
- <?php the_time('d M, Y') ?>
</div>
<!--<div class="auther">by <?php the_author(); ?> <span> - <?php comments_number( 'no comments', 'one comment', '% comments' ); ?> Reply</span></div>-->
<div class="title clear-both"><h2><?php the_title(); ?></h2></div>
<div class="details"><p><?php the_excerpt(); ?></p></div>
<div class="readmore">Read more</div>
<br>
</li>
<?php endwhile; ?>
</ul>
</div>
<div class="pagination">
<?php
if(function_exists('wp_pagenavi')) {
wp_pagenavi();
}
else {
?>
<div class="prev-next-btn">
<?php next_posts_link( __( 'next', 'themename' ) ); ?>
</div>
<div class="prev-next-btn">
<?php previous_posts_link( __( 'prev', 'themename' ) ); ?>
</div>
<?php } ?>
</div>
<?php else : ?>
404 Nothing here. Sorry.
<?php endif; ?>
</div>
You do have a few issues here
NEVER EVER use query_posts. It is slow, reruns queries, breaks and fails silently with pagination and worse of all, it breaks the main query object. If you break the main query object, you break page functions. So, please never use query_posts. Mke if it never existed
You have replaced the main query loop with a custom query, which you must not do. If you need to show a different amount of posts on a particular page ( not on page templates and a static front page though as this will not work there ), then use pre_get_posts. You need to go and read how helpful that action is and how to use it
Remove this line
<?php query_posts('posts_per_page=5'); ?>
Then add the following in your functions file
add_action( 'pre_get_posts', function ( $query )
{
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'posts_per_page', 5 );
}
});
<?php
// set the "paged" parameter (use 'page' if the query is on a static front page)
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
// the query
$the_query = new WP_Query( 'cat=1&paged=' . $paged );
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php
// the loop
while ( $the_query->have_posts() ) : $the_query->the_post();
?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php
// next_posts_link() usage with max_num_pages
next_posts_link( 'Next', $the_query->max_num_pages );
previous_posts_link( 'Previous' );
?>
<?php
// clean up after the query and pagination
wp_reset_postdata();
?>
<?php endif; ?>
Just add below code: I added 'paged' variable and set in next_posts_link() and previous_posts_link()..please see below code:
<div class="center-content">
<ul>
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts(array('posts_per_page'=>5,'paged'=>$paged )); ?>
<?php if (have_posts()) : while (have_posts()) :the_post(); ?>
<li>
<div class="date">In
<?php
$category = get_the_category();
if(!empty($category))
echo ''.$category[0]->cat_name.'';
?>
- <?php the_time('d M, Y') ?>
</div>
<!--<div class="auther">by <?php the_author(); ?> <span> - <?php comments_number( 'no comments', 'one comment', '% comments' ); ?> Reply</span></div>-->
<div class="title clear-both"><h2><?php the_title(); ?></h2></div>
<div class="details"><p><?php the_excerpt(); ?></p></div>
<div class="readmore">Read more</div>
<br>
</li>
<?php endwhile; ?>
</ul>
</div>
<div class="pagination">
<?php
global $wp_query;
if(function_exists('wp_pagenavi')) {
wp_pagenavi();
}
else { echo 'test';
?>
<div class="prev-next-btn">
<?php echo next_posts_link( __( 'next', 'themename' ) , $wp_query->max_num_pages ); ?>
</div>
<div class="prev-next-btn">
<?php echo previous_posts_link( __( 'prev', 'themename' ) , $wp_query->max_num_pages ); ?>
</div>
<?php } ?>
</div>
<?php else : ?>
404 Nothing here. Sorry.
<?php endif; ?>
</div>
I'm currently developing my own wordpress theme and so far so good. The problem is I'm using a permalink and a category. The category is inside the permalink. The following code is what I'm using (content.php):
<?php if ( has_tag ('half_half') ) : ?>
<div class="gridHalves">
<article class="project" >
<a class="projectLink" href="<?php the_permalink(); ?>">
<div class="projectwrapper">
<div class="projectOverlayInset">
<div class="projectOverlay">
<div class="projectInfo">
<h4 class="categorie"><?php the_category(', '); ?></h4>
<h2 class="onderwerp"><?php the_title(); ?></h2>
<p class="writer">Written by <strong><?php the_author(); ?></strong></p>
</div>
</div>
</div>
<?php if ( has_post_thumbnail() ) {
the_post_thumbnail();
} ?>
</div>
</a>
</article>
</div>
<?php endif; ?>
The following is happening when I leave the_category in there:
If i leave the_category tag out everything is working fine. I've already checked the function in wp-includes of the_category but couldn't find anything about the permalink. So any suggestions? My loop is the following:
<?php get_header(); ?>
<main id="projectengrid" class="gridmenu-closed">
<section class="gridRightSmall">
<?php if ( have_posts() ) : ?>
<?php
// Start the loop.
while ( have_posts() ) : the_post();
get_template_part( 'content', get_post_format() );
// End the loop.
endwhile;
// Previous/next page navigation.
the_posts_pagination( array(
'prev_text' => __( 'Previous page', 'twentyfifteen' ),
'next_text' => __( 'Next page', 'twentyfifteen' ),
'before_page_number' => '<span class="meta-nav screen-reader-text">' . __( 'Page', 'twentyfifteen' ) . ' </span>',
) );
// If no content, include the "No posts found" template.
else :
get_template_part( 'content', 'none' );
endif;
?>
</section>
</main><!-- .site-main -->
<?php get_footer(); ?>
I left the post navigation in there because I want to use it later on.
Hope someone knows the solution to this! Seems like I'm having some nested loops in there!
Kind regards,
Wouter
If you need any additional info please let me know!
I'm trying to use WP pagination on post archive page but exclude posts from one category to be shown there.
When I add this to my code the page2,3,4... of the archive display the same first 10 posts:
<?php query_posts('cat=-4');?>
This is the whole code of my page template so I would be grateful for all your help:
<?php
/*
Template Name: Post archive
*/
?>
<?php get_header(); ?>
<div class="container">
<div class="content col-md-9">
<div class="home-content">
<!-- Show posts -->
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'paged'=> $paged,
'posts_per_page'=> 10
);
query_posts($args); ?>
<?php query_posts('cat=-4');?>
<?php if ( have_posts() ) : while (have_posts()) : the_post(); ?>
<div style="float:left; margin:1%;">
<?php
if ( has_post_thumbnail() ) { // check if the post has a Post Thumbnail assigned to it.
the_post_thumbnail( 'thumbnail', array( 'class' => 'img-post')); // show featured image
}
?>
</div>
<h1 class="post-thumb"><?php the_title(); ?></h1>
<h4>Category: <?php the_category(', '); ?></h4>
<p><?php the_excerpt(); ?></p>
<hr style="margin-bottom:5%">
<?php endwhile; ?>
<!-- pagination -->
<div class="nav-previous alignleft" style="margin-top:-1%"><?php next_posts_link( 'See older posts' ); ?></div>
<div class="nav-next alignright" style="margin-top:-1%"><?php previous_posts_link( 'See newer posts' ); ?></div>
<?php else : ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
</div>
</div>
<div class="col-md-3 sidebar unstyled">
<?php dynamic_sidebar( 'home1' ); ?>
</div>
<div class="col-md-3 sidebar unstyled sidebar-space">
<?php dynamic_sidebar( 'home2' ); ?>
</div>
<div class="col-md-3 sidebar unstyled sidebar-space">
<?php dynamic_sidebar( 'articles1' ); ?>
</div>
</div>
</div>
</body>
</html>
<?php get_footer(); ?>
This code fixed my page:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'cat'=> -4,
'paged'=> $paged,
'posts_per_page'=> 10
);
query_posts($args); ?>
<?php if ( have_posts() ) : while (have_posts()) : the_post(); ?>
Modifying to reflect OP's solution for benefit of future readers
Change query() slightly as shown below
$args = array(
'cat'=> -4,
'posts_per_page'=> 10,
'paged'=> $paged
);
In my blog older post and newer post link not working properly
When user press on the link it Just reloads to the same page itself
the URL creates is http://www.website.com/blog/page/2/
I believe the correct URL should be http://www.website.com/blog/2014/05/page/2/
This is my code in loop.php
<?php /* Display navigation to next/previous pages when applicable */ ?>
<?php if ( $wp_query->max_num_pages > 1 ) : ?>
<div id="nav-below" class="navigation">
<div class="nav-previous"><?php next_posts_link( __( ' Older posts', 'twentyten' ) ); ?></div>
<div class="nav-next"><?php previous_posts_link( __( 'Newer posts', 'twentyten' ) ); ?></div>
</div><!-- #nav-below -->
<?php endif; ?>
Thanks
I dont know what are you doing in starting with loop.php
This code could be helpful.
<?php
//The Query
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$wp_query = new WP_Query();
$wp_query->query( 'showposts=10&cat='.$category_id.'&paged='.$paged );
//The Loop
while ($wp_query->have_posts()) : $wp_query->the_post();
// display posts as you need
endwhile;
// pager Method -1
<?php if ( $wp_query->max_num_pages > 1 ) : ?>
<div id="nav-below" class="navigation">
<div class="nav-previous"><?php next_posts_link( __( ' Older posts', 'twentyten' ) ); ?></div>
<div class="nav-next"><?php previous_posts_link( __( 'Newer posts', 'twentyten' ) ); ?></div>
</div><!-- #nav-below -->
//Or you can use Paged Method -2
// pager
if($wp_query->max_num_pages>1){?>
<p class="pager">
<?php
for($i=1;$i<=$wp_query->max_num_pages;$i++){?>
<a href="<?php echo get_category_link($category_id).'page/'.$i;?>" <?php echo ($paged==$i)? 'class="active"':'';?>><?php echo $i;?></a>
<?php
}
if($paged!=$wp_query->max_num_pages){?>
Category
<?php } ?>
</p>
<?php } ?>
I have my theme page.php as:
<?php /* The loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?> >
<header class="entry-header">
<div class="hd"><?php the_title(); ?></div>
<?php if ( has_post_thumbnail() && ! post_password_required() ) : ?>
<div class="video"><?php the_post_thumbnail(); ?></div>
<?php endif; ?>
<div class="hd"><?php //the_title(); ?></div>
</header><!-- .entry-header -->
<?php the_content(); ?>
<?php wp_link_pages( array( 'before' => '<div class="page-links"><span class="page-links-title">' . __( 'Pages:', 'twentythirteen' ) . '</span>', 'after' => '</div>', 'link_before' => '<span>', 'link_after' => '</span>' ) ); ?>
<!-- .entry-content -->
<footer class="entry-meta">
<?php edit_post_link( __( 'Edit', 'twentythirteen' ), '<span class="edit-link">', '</span>' ); ?>
</footer><!-- .entry-meta -->
</article><!-- #post -->
<?php // comments_template( '', true ); ?>
<?php endwhile; ?>
And I have made three page in wordpress blog, image and news and i also assigned them category for each. now i have installed php-exec plugin . Now I am writing some php code in page editor to retrieve blog data...
It working fine but it fetching data twice and now got it is becouse of page.php .
So can i have some condition on page.php if i am trying to fetch some data by cotegory then page.php data would not be display...
here is my code which i applied on blog page editor
<?php if (query_posts('cat=63&showposts=5')) : ?>
<?php while (have_posts()) : the_post();
// do whatever you want
?>
<div class="gallery_views">
<div class="hd"><?php the_title(); ?></div>
<?php // get_template_part( 'content', get_post_format() ); ?>
<?php // cup_post_nav(); ?>
<?php the_post_thumbnail(); ?>
<?php comments_template(); ?>
<b><?php the_title(); ?></b>
</div>
<?php endwhile; ?>
<?php else : ?>
Thanks in advance..
<?php if (query_posts('cat=63&showposts=5')) : ?>
<?php while (have_posts()) : the_post();
// do whatever you want
?><div class="gallery_views">
<div class="hd"><?php the_title(); ?></div>
<?php // get_template_part( 'content', get_post_format() ); ?>
<?php // cup_post_nav(); ?>
<?php the_post_thumbnail(); ?>
<?php comments_template(); ?>
<b><?php the_title(); ?></div>
<?php
break;
endwhile;
?>
<?php else : ?>
Add a break in your while, it'll stop after the first loop.