Pagination on custom Wordpress template - php

I have problem to change content on page when for ex. url change from
/?page_id=55&paged=1 to /?page_id=55&paged=2...
So I have problem to display next/previous page content, it just show first 4 post on page and when I click next nothing hapend with content just url change.
Here is my blog template:
<?php
/*
Template Name: Blog
*/
?>
<?php get_header(); ?>
<div class="row main-part">
<?php
$args = array(
'category_name' => 'blog',
'paged' => $paged,
'posts_per_page' => 4
);
$args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$custom_query = new WP_Query( $args );
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $custom_query;
?>
<?php
if ( $custom_query->have_posts() ) :
while ( $custom_query->have_posts() ) :
$custom_query->the_post();
?>
<div class="col-xs-12 col-md-3">
<h1><?php the_title(); ?></h1>
<p>
<?php the_excerpt(); ?>
</p>
<p><a class="btn btn-primary" href="<?php the_permalink() ?>" role="button">View details »</a></p>
</div>
<?php endwhile;endif; ?>
<?php
wp_reset_postdata();
wp_reset_query();
// Custom query loop pagination
previous_posts_link( 'Older Posts' );
next_posts_link( 'Newer Posts', $custom_query->max_num_pages );
// Reset main query object
$wp_query = NULL;
$wp_query = $temp_query;
?>
</div>
<?php get_footer(); ?>

Related

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/

Pagination on blog page shows same posts for each page

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

WP_Query Pagination not working with custom query on custom post type

I have read as many other problems like mine and not been able to find a fix. For some reason my custom query is not paginating and I can not work out why. It is probably a silly reason but hope you can help.
Here is my code.
<?php
global $wp_query;
$title = $wp_query->post->post_title;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$query = new WP_Query(
array(
'paged' => $paged,
'wpse18703_title' => "$title",
'posts_per_page' => 10,
'post__not_in' => array($post->ID),
'post_type' => array('post', 'features'),
)
);
if ($query->have_posts()){
while ( $query->have_posts() ) { $query->the_post(); ?>
<a href="<?php echo get_permalink(); ?>">
<div class="small-12 large-12 gdb-news-container">
<div class="small-12 large-4 columns gdb-news-image">
<?php the_post_thumbnail('game-news-thumb'); ?>
</div>
<div class="small-12 large-8 columns game-title">
<h5><?php the_title(); ?></h5>
<?php echo content(20); ?>
</div>
<div class="clearfix"></div>
</div>
</a>
<?php
} //end while
wp_reset_query();
} //end if
?>
<?php if ( function_exists('reverie_pagination') ) { reverie_pagination(); } else if ( is_paged() ) { ?>
<nav id="post-nav">
<div class="post-previous"><?php next_posts_link( __( '← Older posts', 'reverie' ) ); ?></div>
<div class="post-next"><?php previous_posts_link( __( 'Newer posts →', 'reverie' ) ); ?></div>
</nav>
<?php } ?>
<?php
if(isset($_GET['paged']) && !empty($_GET['paged'])):endif;
global $wp_query;
$temp = $wp_query;
$wp_query = null;
$title = $wp_query->post->post_title;
$args = array(
'paged' => $paged,
'wpse18703_title' => "$title",
'posts_per_page' => 10,
'post__not_in' => array($post->ID),
'post_type' => array('post', 'features')
);
$wp_query = new WP_Query($args);
if($wp_query->have_posts()){
while($wp_query->have_posts()){
$wp_query->the_post(); ?>
<a href="<?php echo get_permalink(); ?>">
<span class="small-12 large-12 gdb-news-container">
<span class="small-12 large-4 columns gdb-news-image">
<?php the_post_thumbnail('game-news-thumb'); ?>
</span>
<span class="small-12 large-8 columns game-title">
<h5><?php the_title(); ?></h5>
<?php echo content(20); ?>
</span>
</span>
</a>
<?php }
}
$wp_query = null;
$wp_query = $temp;
if(function_exists('reverie_pagination')){reverie_pagination();} else if (is_paged()){ ?>
<nav id="post-nav">
<div class="post-previous"><?php next_posts_link( __( '← Older posts', 'reverie' ) ); ?></div>
<div class="post-next"><?php previous_posts_link( __( 'Newer posts →', 'reverie' ) ); ?></div>
</nav><?php
}
?>
I changed you div's with the classes "small-12" to span's because it is invalid W3C code to put a div inside an anchor tag, so you might have to modify your css file, depending on how it was coded.
I think you are using your $paged variable wrong (i am guessing you are using it inside "pages" not "posts"). This should work both on pages and posts:
if ( get_query_var( 'paged' ) ) {
$paged = absint( get_query_var( 'paged' ) );
} elseif ( get_query_var( 'page' ) ) {
$paged = absint( get_query_var( 'page' ) );
} else {
$paged = 1;
}

Can someone help me to add a category to this Post?

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

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