Pagination in wordpress show the same post - php

I have a realy big problem with pagination. When i click next post - wordpress show me the same 9 post. What is bad with this? This is meybe on $row array? I need custome post view, and pagination.
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'posts_per_page' => 9,
'paged' => $paged
);
$query = new WP_Query ( array( $args ) );
$row = array(6, 3, 3, 4, 4, 4, 3, 3, 6);
$i = 0;
if ( $query->have_posts() ) : while ( $query->have_posts() ) : $query->the_post();
$thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id ( $the_query->ID ), 'thumbnail' );
?>
<div class="col-md-<?php echo $row[$i] ?> col-xs-6">
<div class="blog-item scrollpoint sp-effect2">
<div class="cover" style="background-image: url('<?php echo $thumbnail[0] ?>')">
<div class="mask">
<div class="post">
<span class="kategoria"></span>
<div class="tresc">
<h1> <?php the_title(); ?></h1>
<div class="wypis"><?php echo get_the_excerpt(); ?></div>
</div>
</div>
</div>
</div>
</div>
</div>
<?php
$i++;
endwhile; ?>
<!-- pagination -->
<?php previous_posts_link('« Newer posts') ?>
<?php next_posts_link('Older posts »') ?>
<?php wp_reset_postdata();?>
<?php else : ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>

The previous_posts_link and next_posts_link are designed to show the next or former post from the currently displayed post. You're displaying a loop.
If, as this seems, you are just making a blog index page and requesting regular posts, then you just need to make links that append /{paged}/ to the end, like so:
Next
<?php if($paged > 1): ?>
Previous
<?php endif;?>
Where blogpage is the page this is supposed to appear (if the home, just do / )
If you're doing something more complicated, like a custom post type with a custom permalink structure, look into query_vars and the Rewrite API.

Related

Hide loop if no posts exist

I simply have a single loop that's pulling through a CPT, but I would like to hide the whole container div of the loop, if it has no posts...
I'm trying various iterations of this if statement surrounding the container:
<?php if( have_posts() ): ?>
<?php endif; ?>
But I can't seem to get it to work properly... The full code blog is here:
<?php if( have_posts() ): ?>
<div class="container default-strip-section">
<h2><?php the_field('vacancies_title'); ?></h2>
<div class="row justify-content-center">
<?php
$args = array(
'post_type' => 'vacancies',
'posts_per_page' => 9999
// 'orderby' => 'title',
// 'order' => 'ASC'
);
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="col-md-12">
<a href="<?php the_permalink(); ?>">
<p><?php the_title() ?></p>
</a>
</div>
<?php endwhile; wp_reset_postdata(); endif; ?>
</div>
</div>
<?php endif; ?>
Can anyone point out what I'm doing wrong? I'm sure I'm missing something simple! Thanks for looking!! :)
#rank's answer is nearly correct -- it needs the object instance before the_post() again:
<?php
$args = array(
'post_type' => 'vacancies',
'posts_per_page' => 9999, // If you're wanting to show all posts, use -1 here.
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
?>
<div class="container default-strip-section">
<h2><?php the_field('vacancies_title'); ?></h2>
<div class="row justify-content-center">
<?php
while ( $the_query->have_posts() ) :
$the_query->the_post();
?>
<div class="col-md-12">
<p><?php the_title(); ?></p>
</div>
<?php
endwhile;
?>
</div>
</div>
<?php
endif;
wp_reset_postdata();
?>
You need to put the html of the container after the if have_posts(), where you check if there are any posts. This way the whole container is not displayed when the if is not true.
But you are using a ACF field of the post where you are showing this list of vacancies. So we save the id into a variable to be able to view the value inside of your custom query called the_query (this is not a self-explanatory / good name). Why not call it vacancies_query to have a more readable code. Putting it together it should look like this:
<?php
$current_post = get_the_ID();
$args = array(
'post_type' => 'vacancies',
'posts_per_page' => 9999
);
$vacancies_query = new WP_Query( $args );
if ( $vacancies_query->have_posts() ) : ?>
<div class="container default-strip-section">
<h2><?php the_field('vacancies_title', $current_post); ?></h2>
<div class="row justify-content-center">
<?php while ( $vacancies_query->have_posts() ) : the_post(); ?>
<div class="col-md-12">
<a href="<?php the_permalink(); ?>">
<p><?php the_title() ?></p>
</a>
</div>
<?php endwhile; ?>
</div> <!-- row -->
</div> <!-- container -->
<?php else :
/* nothing to see here */
endif;
wp_reset_postdata();
?>

Multiple loops: exclude a post returned in loop 1 from loop 2

I have two wp_query loops.
The first loop looks for a featured post and formats it differently than the rest of the posts.
Then the second loop displays the rest of the posts.
I want to exclude this first, featured post from the rest of the loop.
Right now, the post ID of the first post is saved as a variable.
I want to use that variable in the second loop, with the wp_query exclude argument.
But as far as I understand, that variable dies when my first loop ends. It's then a null variable in the second loop and so nothing is excluded.
<!-- Here's the query for the featured post -->
<?php
// the arguments
$args = array(
'posts_per_page' => '1',
'orderby' => '',
'meta_key' => 'featured_post',
'meta_value' => '1'
); ?>
<?php $the_query = new WP_Query($args); ?>
<?php if ($the_query->have_posts()) : ?>
<?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
<!-- I'm storing the ID so I can exclude it from the rest of the loop, but I know this doens't work right now -->
<?php $postid = get_the_ID(); ?>
<div class="small-12 columns entry" >
<div class="text-center" id="featured-thumbnail">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('featured'); ?>
</a>
<h2>
<?php the_title(); ?>
</h2>
<p>
<?php
$content = get_the_content();
echo wp_trim_words($content, '75');
?>
</p>
</div>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<!-- If there is no featured post, pull in the most recent post and make it big -->
<?php else: ?>
<?php
// the arguments
$args = array(
'posts_per_page' => '1',
'orderby' => '',
); ?>
<?php $the_query = new WP_Query($args); ?>
<?php if ($the_query->have_posts()) : ?>
<?php while ($the_query->have_posts()) : $the_query->the_post(); ?>
<!-- I'm storing the ID so I can exclude it from the rest of the loop, but I know this doens't work right now -->
<?php $postid = get_the_ID(); ?>
<div class="small-12 columns entry" >
<div class="text-center" id="featured-thumbnail">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('featured'); ?>
</a>
<h2>
<?php the_title(); ?>
</h2>
<p>
<?php
$content = get_the_content();
echo wp_trim_words($content, '75');
?>
</p>
</div>
</div>
<!-- End of the nested loop (most recent posts) -->
<?php endwhile; ?>
<?php endif; ?>
<!-- End of the featred post query loop-->
<?php endif; ?>
And here's the main loop:
<?php get_template_part('parts/content', 'featured-post'); ?>
<!-- This ends the logic for the featured post. Now we show the rest of the posts, excluding the first one we showed -->
<?php get_template_part('parts/content', 'category-filter'); ?>
<?php
// the arguments
$args=array(
'paged' => $paged,
'posts_per_page' => 9,
'post__not_in' => array($postid)
); ?>
<?php $the_query = new WP_Query( $args ); ?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- Start row that holds blocks -->
<div class="row small-up-1 medium-up-2 large-up-3">
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="column entry" >
<?php if( get_the_post_thumbnail() ): ?>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail('blog'); ?>
</a>
<?php else : ?>
<?php endif; ?>
<h5><?php the_title(); ?></h5>
<?php
$excerpt = get_the_excerpt();
echo wp_trim_words( $excerpt , '10', '');
?>
</div>
<?php endwhile; ?>
</div>
<?php wp_reset_postdata(); ?>
<?php else: ?>
<p>Sorry, no more posts.</p>
<?php endif; ?>
How exactly do I set up a variable to use in the second loop?

WP-pagination and excluding category from page

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

How to create separate pagination for mutiple loops?

I have created two loops with pagination (first loop loops through CAT'S category and second loops through DOG'S category), but now I am stuck:(
The problem: After I click "Next entry" on my site (CAT'S category) it goes to second entry in that category BUT it also goes to my DOG'S category second entry (I don't want THAT!! ). It also happens vice versa...
What I like to do is this: I click on "Next Entry" on my CAT'S category and it goes only to next post in THAT category (CAT'S) but NOT to second post in my DOG'S category, or another way around: I click on "Next Entry" on my DOG'S category and it goes only to next post in THAT category (DOG'S) but NOT to second post in my CAT'S category .
Can someone help me please? I have asked for help on
wordpress.stackexchange.com a while ago but I didn't get any answer so I am asking question here.
Index php looks like this:
<?php get_header(); ?>
<?php get_sidebar(); ?>
<div id="blog">
<?php
$args = array(
'category_name' => 'cats'
);
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$the_query = new WP_query($args . '&paged=' . $paged . '&cat=-3');
while( $the_query -> have_posts()) : $the_query -> the_post();
?>
<div class="post">
<div class="post_title">
<h3><?php the_title(); ?></h3>
</div>
<div class="entry">
<?php the_post_thumbnail(); ?>
<?php the_content('Read on...'); ?>
<p class="postmetadata">
<?php _e('Filed under:'); ?> <?php the_category(', ') ?> <?php _e('by'); ?> <?php the_author(); ?><br />
<?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?> <?php edit_post_link('Edit', ' | ', ''); ?>
</p>
</div>
</div>
<?php endwhile;?>
<?php wp_reset_postdata();?>
<div class="navigation">
<div style="float:left;" class="alignleft"><?php previous_posts_link('« Previous Entries') ?></div>
<div style="float:right;" class="alignright"><?php next_posts_link('Next Entries »',$the_query->max_num_pages) ?></div>
</div>
</div>
<div id="blogs">
<?php
$args = array(
'category_name' => 'dogs'
);
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$the_query = new WP_query($args . '&paged=' . $paged . '&cat=-10');
while( $the_query -> have_posts()) : $the_query -> the_post();
?>
<div class="post">
<div class="post_title">
<h3><?php the_title(); ?></h3>
</div>
<div class="entry">
<?php the_post_thumbnail(); ?>
<?php the_content('Read on...'); ?>
<p class="postmetadata">
<?php _e('Filed under:'); ?> <?php the_category(', ') ?> <?php _e('by'); ?> <?php the_author(); ?><br />
<?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?> <?php edit_post_link('Edit', ' | ', ''); ?>
</p>
</div>
</div>
<?php endwhile;?>
<?php wp_reset_postdata();?>
<div class="navigation">
<div style="float:left;" class="alignleft"><?php previous_posts_link('« Previous Entries') ?></div>
<div style="float:right;" class="alignright"><?php next_posts_link('Next Entries »',$the_query->max_num_pages) ?></div>
</div>
</div>
<?php get_footer(); ?>
You need 2 different paging values so add some new ones and rewrite rules which look for them (they're really just to make the urls neater looking). The rewrite rules and the pagination link format mean you can page through one category while the other category page doesn't change.
In functions.php:
function add_new_rules()
{
// new 'paged' variables
global $wp;
$wp->add_query_var('paged_cats');
$wp->add_query_var('paged_dogs');
// rewrite rules
add_rewrite_rule('page/cats/(\d+)/dogs/(\d+)', 'index.php?paged_cats=$matches[1]&paged_dogs=$matches[2]', 'top');
add_rewrite_rule('page/cats/(\d+)/dogs/?$', 'index.php?paged_cats=$matches[1]&paged_dogs=1', 'top');
if( !array_key_exists('page/cats/(\d+)/dogs/(\d+)', (array)get_option('rewrite_rules')) )
{
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
}
add_filter('init', 'add_new_rules');
Check for the new query vars in index.php and use them for each WP_Query and the related pagination links.
<div id="blog">
<?php
$paged_cats = (get_query_var('paged_cats')) ? get_query_var('paged_cats') : 1;
$paged_dogs = (get_query_var('paged_dogs')) ? get_query_var('paged_dogs') : 1;
$cats = new WP_query(array(
'category_name' => 'cats',
'paged' => $paged_cats,
'posts_per_page' => 1
));
while( $cats->have_posts() ) : $cats->the_post();
?>
<div class="post">
<div class="post_title">
<h3><?php the_title(); ?></h3>
</div>
<div class="entry">
<?php the_post_thumbnail(); ?>
<?php the_content('Read on...'); ?>
<p class="postmetadata">
<?php _e('Filed under:'); ?> <?php the_category(', ') ?> <?php _e('by'); ?> <?php the_author(); ?><br />
<?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?> <?php edit_post_link('Edit', ' | ', ''); ?>
</p>
</div>
</div>
<?php endwhile; ?>
<?php wp_reset_postdata(); ?>
<?php if ( $cats->max_num_pages > 1 ) : ?>
<div class="navigation">
<?php
echo paginate_links(array(
'base' => home_url("page/cats/%#%/dogs/{$paged_dogs}"),
'format' => '%#%',
'current' => $paged_cats,
'total' => $cats->max_num_pages,
));
?>
</div>
<?php endif; ?>
</div>
<hr>
<div id="blogs">
<?php
$dogs = new WP_query(array(
'category_name' => 'dogs',
'paged' => $paged_dogs,
'posts_per_page' => 1
));
while( $dogs->have_posts() ) : $dogs->the_post();
?>
<div class="post">
<div class="post_title">
<h3><?php the_title(); ?></h3>
</div>
<div class="entry">
<?php the_post_thumbnail(); ?>
<?php the_content('Read on...'); ?>
<p class="postmetadata">
<?php _e('Filed under:'); ?> <?php the_category(', ') ?> <?php _e('by'); ?> <?php the_author(); ?><br />
<?php comments_popup_link('No Comments »', '1 Comment »', '% Comments »'); ?> <?php edit_post_link('Edit', ' | ', ''); ?>
</p>
</div>
</div>
<?php endwhile;?>
<?php wp_reset_postdata();?>
<?php if ( $dogs->max_num_pages > 1 ) : ?>
<div class="navigation">
<?php
echo paginate_links(array(
'base' => home_url("page/cats/{$paged_cats}/dogs/%_%"),
'format' => '%#%',
'current' => $paged_dogs,
'total' => $dogs->max_num_pages,
));
?>
</div>
<?php endif; ?>
</div>
Your problem is this:
On pagination wordpress sends page id or count to get the next enteries and the name of the variable that goes in postback is same for both lists. When it goes to the server both lists get a request to go to next page by looking at the post variables.
The solution mentioned by jho1086 is to create paged variable as a custom variable for both lists and assign it. this would then send a different variable for each list and you can move next or previous as you wish.
You need to do both add a paged variable and add it to your pagination as well. In jho1086's solutuin see $args1 and $pag_args1 both have reference to $paged2 to make this happen.
When you select page 2 for cats it should send a catpage=2 to the server
When you select page 2 for Docs it should send a dogpage=2 to the server
If you can solve this for pagination links and pass the args then you can do following
When server gets the list of cats use catpage as a paging param
When server gets the list of dogs use dogpage as a paging param
This is in theory and will surely work. You can test variables going in and out of the request via firebug and comeback with what other issues you have but the reason your loops go to page 2 for clicking on one is that both loops are paging on the same post variable.
I find answer here https://wordpress.stackexchange.com/questions/47259/multiple-wp-query-loops-with-pagination, the choice answer is working with me. It use format.
<!-- Cats -->
<div class="animals">
<?php
$paged1 = isset( $_GET['paged1'] ) ? (int) $_GET['paged1'] : 1;
$paged2 = isset( $_GET['paged2'] ) ? (int) $_GET['paged2'] : 1;
// Custom Loop with Pagination 1
// http://codex.wordpress.org/Class_Reference/WP_Query#Usage
$args1 = array(
'paged' => $paged1,
'posts_per_page' => 2,
);
$query1 = new WP_Query( $args1 );
while ( $query1->have_posts() ) : $query1->the_post();
the_title();
echo '<br>';
the_category(' ');
the_excerpt();
echo '<hr>';
endwhile;
// http://codex.wordpress.org/Class_Reference/WP_Query#Pagination_Parameters
$pag_args1 = array(
'format' => '?paged1=%#%',
'current' => $paged1,
'total' => $query1->max_num_pages,
'add_args' => array( 'paged2' => $paged2 )
);
echo paginate_links( $pag_args1 );
?>
</div>
<!-- Dogs -->
<div class="animals">
<?php
// Custom Loop with Pagination 2
$args2 = array(
'paged' => $paged2,
'posts_per_page' => 2,
);
$query2 = new WP_Query( $args2 );
while ( $query2->have_posts() ) : $query2->the_post();
the_title();
echo '<br>';
the_category(' ');
the_excerpt();
echo '<hr>';
endwhile;
$pag_args2 = array(
'format' => '?paged2=%#%',
'current' => $paged2,
'total' => $query2->max_num_pages,
'add_args' => array( 'paged1' => $paged1 )
);
echo paginate_links( $pag_args2 );
?>
</div>
And since it is generating a un-clean url you can add a rel="nofollow" for SEO purposes. Here is the instruction how to do add rel="nofollow"
the functions next_post_link() and previous_post_link() has a third argument called 'in_same_cat' - You will need to set it to TRUE.
Read the codex page (click on the functions name in the answer .
From codex :
<?php next_post_link('%link', 'Next post in category', TRUE); ?>
They even have a fourth argument 'excluded_categories' which you can also use for achieving the same thing, or combining both to get even more sophisticated results.

Wordpress add pagination for custom loop that show subpages

I have one page (not an article) with n subpages.
In the main page, I need to show max 3 titles of the subpages and insert a pagination for the other.
How can I do that?
This is my simple code now:
<?php
$parent_id = 14; //main page id
$pages = get_pages( array( 'sort_column' => 'menu_order', 'numberposts' => 3, 'child_of' => $parent_id ) );
foreach ( $pages as $page ) : ?>
<div class="item">
<div class="item-title">
<h2><?php echo $page->post_title; ?></h2>
</div>
</div>
<?php endforeach; ?>
Thanks.
I solved by myself, the solution is to use wp_query() to create a new loop insted of using get_pages().
Here the new code for page title and contentwith pagination by Preeti Dua from Avigma Technology:
<?php
// Pagination variable
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
// The Query
$the_query = new WP_Query( array( 'post_parent' => 782, 'post_type' => 'page', 'paged' => $paged) );
// The Loop
if($the_query->have_posts()) : while($the_query->have_posts()) : $the_query->the_post();
global $post;
$thePostID = $post->ID; /* this variabled is used if you need to get custom fields of the subpage */
?>
<div id="side-post-title">
<?php the_title(); ?>
</div>
<div id="side-post-excerpt">
<?php the_excerpt(); ?>
<a href="<?php echo get_permalink( $page->ID ); ?>"> <div id="read-more">
<img src="/wp-content/uploads/2012/10/read-more-btn.png"/></div> </a>
</div>
<?php endwhile; endif; ?>
<nav class="navigation">
<div style="float:left;"> <?php next_posts_link('Show older', $the_query->max_num_pages) ?></div>
<div style="float:right;"> <?php previous_posts_link('Show newer') ?></div>
</nav>
not sure,
but try following plugin
http://wordpress.org/extend/plugins/wp-pagenavi/
If you'd like to add subPage title, description or even thumbnail in pagination button you can use the ACP free Wordpress plugin: http://wordpress.org/plugins/advanced-content-pagination/

Categories