I'm building a custom page template and I can't seem to get the pagination to work. It keeps showing the same posts as on the home/first page. I have tried a lot of different code but getting the same problem with each results.
This is the current query I am using:
<?php
global $paged;
global $wp_query;
$temp = $wp_query;
$wp_query = null;
$wp_query = new WP_Query();
$wp_query->query('posts_per_page=2&post_type=post'.'&paged='.$paged);
while ($wp_query->have_posts()) : $wp_query->the_post();
?>
<div class="news-item">
<?php
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), '' );
$url = $thumb['0'];
?>
<div class="news-item-bg" style="background-image:url(<?=$url?>);"></div>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<h2><?php the_title(); ?></h2></a>
<div class="entry-meta">
<div class="meta-date"><?php the_time('d.m.Y'); ?> </div><div class="meta-seperator"> | </div><div class="meta-author">Auteur: <?php the_author(); ?></div>
</div><!-- .entry-meta -->
<div class="excerpt"><?php the_excerpt(); ?></div>
</div>
<?php endwhile; ?>
<?php previous_posts_link('« Newer') ?>
<?php next_posts_link('Older »') ?>
<?php
$wp_query = null;
$wp_query = $temp;
?>
Found the solution, this code worked:
<?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' => '2',
'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(); ?>
<div class="news-item">
<?php
if ( has_post_thumbnail() ) {
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), '' );
$url = $thumb['0'];
echo '<div class="news-item-bg" style="background-image:url(<?=$url?>);"></div>';
}
else {
}
;?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>">
<h2><?php the_title(); ?></h2></a>
<div class="entry-meta">
<div class="meta-date"><?php the_time('d.m.Y'); ?> </div><div class="meta-seperator"> | </div><div class="meta-author">Auteur: <?php the_author(); ?></div>
</div><!-- .entry-meta -->
<div class="excerpt"><?php the_excerpt(); ?></div>
<div id="single-post-container"></div>
<a class="button-1 load-more" href="<?php echo get_permalink(); ?>">Lees meer</a>
<a class="ajax-close button-1" href="#">X</a>
</div>
<?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;
?>
Try this code just copy pase
<?php
$args = array(
'post_type'=> 'post',
//'category_name' => 'blog',
'orderby' => 'post_date',
//'posts_per_page'=>'1'
'paged' => get_query_var('paged')
);
query_posts( $args );
while (have_posts()) : the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>
<?php // Wordpress Pagination
$big = 999999999; // need an unlikely integer
$links = 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,
'prev_text' => '<',
'next_text' => '>',
'type' => 'array'
) );
if(!empty($links)){ ?>
<ul class="pagination">
<?php
foreach($links as $link){
?>
<li><?php echo $link; ?></li>
<?php
}
wp_reset_query(); ?>
</ul>
<?php } ?>
Related
This is my code here -
<div class="gallery-posts">
<?php
$args = array(
'post_type' => 'post',
'order' => 'ASC',
'numberposts' => 4
);
$product_posts = get_posts( $args );
?>
<?php foreach ( $product_posts as $post ) : setup_postdata( $post ); ?>
<?php $videos = rwmb_meta( '_video-link' );
foreach ( $videos as $video ); ?>
<div
class="card-container video-link"
data-link="<?php echo $video['src']; ?>"
title="<?php the_title(); ?>"
description="<?php echo ( get_post_meta( get_the_ID(), '_description', true ) ); ?>"
thumbnail-link="<?php the_post_thumbnail_url() ?>">
<figure class="gallery-image">
<?php the_post_thumbnail(); ?>
<div class="gallery-image-text">
<p><?php the_title() ?></p>
<i class="fas fa-play"></i>
</div>
</figure>
</div>
<?php endforeach; wp_reset_postdata(); ?>
</div>
I want to set it up so that Next/Prev shows whenever there are more than 4 posts.
With the regular Word Press loop I know you can just do the_posts_pagination but that doesn't work here.
What needs to be added for pagination to work with this loop?
Change your code like this:
<div class="gallery-posts">
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'post',
'order' => 'ASC',
'posts_per_page' => 4,
'paged' => $paged
);
$product_posts = get_posts( $args );
foreach ( $product_posts as $post ) : setup_postdata( $post );
$videos = rwmb_meta( '_video-link' );
foreach ( $videos as $video ):
?>
<div class="card-container video-link" data-link="<?php echo $video['src']; ?>" title="<?php the_title(); ?>" description="<?php echo ( get_post_meta( get_the_ID(), '_description', true ) ); ?>" thumbnail-link="<?php the_post_thumbnail_url() ?>">
<figure class="gallery-image">
<?php the_post_thumbnail(); ?>
<div class="gallery-image-text">
<p><?php the_title() ?></p>
<i class="fas fa-play"></i>
</div>
</figure>
</div>
<?php
next_posts_link( 'Older Entries', $product_posts->max_num_pages );
previous_posts_link( 'Next Entries »' );
wp_reset_postdata();
endforeach;
?>
</div>
I have loop that displays posts. I want to add some html if there will be no posts. Like if there is no posts ---> show my html if/else.
<?php $args = array(
'posts_per_page' => 1000,
'post_type' => 'contract',
'author' => $current_user->ID,
'post_status' => 'publish' );
?>
<?php $get_category_posts = get_posts( $args ); ?>
<?php foreach ( $get_category_posts as $post ) : setup_postdata( $post ); ?>
<li class="jail-app-v-0-4 contract-item">
<div class="contract-item-inner">
<a class="hoverable" href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</div>
</li>
<?php endforeach; ?>
Try this code out please:
<?php $args = array(
'posts_per_page' => 1000,
'post_type' => 'contract',
'author' => $current_user->ID,
'post_status' => 'publish' );
?>
<?php $get_category_posts = get_posts( $args );
if(!empty($get_category_posts)):
?>
<?php foreach ( $get_category_posts as $post ) : setup_postdata( $post ); ?>
<li class="jail-app-v-0-4 contract-item">
<div class="contract-item-inner">
<a class="hoverable" href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</div>
</li>
<?php endforeach; ?>
<?php else: ?>
NO POSTS FOUND!
<?php endif; ?>
Newbie here,
I'm trying to display the list of my post depends on the category. But it doesn't display my post. I tried the different type of array, but no luck. I named my page taxonomy-blog_category.php
I call the page via site.com/blog_category/category
here's my current code and I know I'm so close but can't figure it out.
Here is the array:
<div class="row">
<?php $ctr = 1;
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$custom_args = array(
'post_type' => 'blog_post',
'orderby' => 'date',
'order' => 'DESC',
'post_status' => 'publish',
'posts_per_page' => 6,
'paged' => $paged,
'tax_query' => array(
array(
'taxonomy' => 'blog-category',
'field' => 'slug',
'terms' => array('business','people','technology'),
),
),
);
Here is how I display the post
$custom_query = new WP_Query( $custom_args ); ?>
<?php if ( $custom_query->have_posts() ) : ?>
<?php while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
<div class="col-md-4 text-center">
<div class="content-container">
<div class="wrap">
<figure class="tint t2">
<img src="<?php echo wp_get_attachment_url( get_post_thumbnail_id() ); ?>" width="317px" height="240">
</figure>
</div>
<h2><?php the_title(); ?></h2>
<h3>By <?php the_field('author'); ?> | <span><?php echo get_the_date(); ?></span></h3>
<?php $content = get_field('content'); echo mb_strimwidth($content, 0, 200, '...');?>
<div class="read-more-btn">
read more
</div>
</div>
</div>
<?php $ctr++; endwhile; ?>
I Don't know if this is necessary but here's my code for pagination:
<div class="pagination-holder">
<ul class="pagination">
<?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; ?>
</ul>
</div>
I'm trying to add some code to my wordpress theme to show a pagination at the bottom of the posts.
Here's my loop with the pagination:
<main id="main">
<?php
// the query
$args = array('posts_per_page' => 2 );
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) { ?>
<!-- loop -->
<?php while ( $the_query->have_posts() ) {
$the_query->the_post(); ?>
<article id="post">
<div id="thumbnail">
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail(); } ?>
</div>
<h2><?php the_title(); ?></h2>
<div class="entry">
<?php the_excerpt(); ?>
</div>
</article>
<?php } } else { ?>
<p><?php _e( 'Die Posts entsprechen nicht den Kriterien.' ); ?></p>
<?php } ?>
<!-- pagination -->
<?php
if($the_query->max_num_pages>1){?>
<p class="paged">
<?php
if ($paged > 1) { ?>
<
<?php }
for($i=1;$i<=$the_query->max_num_pages;$i++){?>
<a href="<?php echo '?paged=' . $i; ?>" <?php echo ($paged==$i)? 'class="selected"':'';?>><?php echo $i;?></a>
<?php
}
if($paged < $the_query->max_num_pages){?>
>
<?php } ?>
</p>
<?php } ?>
<!-- end pagination -->
<!-- end of the loop -->
<?php wp_reset_postdata(); ?>
</main>
When I'm looking through the source code I can't find the pagination. What am I doing wrong? Can't find an answer, no code works for me. Would be nice if someone could help me. :)
Use default WordPress pagination, here is an example:
<?php
// set the "paged" parameter
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'posts_per_page' => 5,
'paged' => $paged,
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) :
// the loop
while ( $the_query->have_posts() ) : $the_query->the_post();
the_title();
endwhile;
// Pagination
echo get_next_posts_link( 'Older', $the_query->max_num_pages );
echo get_previous_posts_link( 'Newer' );
// clean up after our query
wp_reset_postdata();
else:
_e( 'Sorry, no posts matched your criteria.' );
endif;
To display next and previous links automatically, just use these functions:
next_posts_link();
previous_posts_link();
https://codex.wordpress.org/Pagination could help with this. In custom loops it helps to pass in the $max_pages variable as zero for unlimited pages, so it'd look like:
next_posts_link("Older Posts", 0);
If you want the pagination to include page links, in the format < 1 2 3 > rather than just next/previous links, you can use the WordPress function paginate_links.
So for your example the code would look like this:
<?php
//query
$paged = (isset($_REQUEST['paged']) && $_REQUEST['paged'] > 0 ? $_REQUEST['paged'] : max( 1, get_query_var('paged') ));
$args = array(
'posts_per_page' => 2,
'paged' => $paged,
);
$the_query = new WP_Query( $args );
//loop
if ($the_query->have_posts()) {
while ($the_query->have_posts()) {
$the_query->the_post();
//display your post here
}
//pagination
if($the_query->max_num_pages > 1){ ?>
<div class="paged">
<?php
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => $paged,
'total' => $the_query->max_num_pages,
'prev_text' => __('« Previous'),
'next_text' => __('Next »'),
) );
?>
</div>
<?php }
} else {
echo '<p>'._e( 'Die Posts entsprechen nicht den Kriterien.' ).'</p>';
}
wp_reset_postdata();
?>
Here is my custom loop, I need to add a sorting option and after hours of digging I cannot find a solution.
<ul class="acapellas row">
<?php
$loop = new WP_Query( array(
'post_type' => 'acapella',
'posts_per_page' => 10,
'paged' => $paged,
'orderby' => 'date'
));
?>
<?php $paged = (get_query_var('paged')) ? get_query_var('paged') : 1; ?>
<?php if ( $loop->have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post(); ?>
<li class="post-<?php the_ID(); ?> col-md-6">
<div class="wrap">
<h2><?php the_title() ?></h2>
<?php if(pmpro_hasMembershipLevel($level_id)) { ?>
<?php the_content(); ?>
<?php } else { ?>
<div class="pro-player">
<div class="upgrade">
<a href="<?php bloginfo('url'); ?>/pro" >Upgrade to unlock</a>
</div>
</div>
<?php } ?>
<a class="download left" href="<?php the_permalink(); ?>">Download</a>
<span class="list-date right">First added: <?php the_time('F jS, Y') ?></span><br>
<?php
global $post;
$post_type = get_post_type(get_the_ID());
$post_type_taxonomies = get_object_taxonomies($post_type);
if (!empty($post_type_taxonomies)) {
echo '<ul class="details">';
foreach ($post_type_taxonomies as $taxonomy) {
$terms = get_the_term_list(get_the_ID(), $taxonomy, '', '</li><li>', '');
if ($terms) {
echo '<li>' . $terms . '</li>';
}
}
echo '</ul>';
}
?>
</div>
</li>
<?php endwhile; endif; ?>
</ul>
You must use both orderby and order, like so:
$loop = new WP_Query( array(
'post_type' => 'acapella',
'posts_per_page' => 10,
'paged' => $paged,
'orderby' => 'date',
'order' => 'asc'
));