Pagination doesn't work on a single page of WP.
In the code below, everything looks great except the pagination shows the same contents on the next page.
I tried every possible solution I found on the internet, but none of them worked. I'm relatively new to WP and php, so if you could pinpoint the code that might be wrong, that'd be really helpful.
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged'
) : 1;
$args = array(
'posts_per_page' => 3,
'paged' => $paged,
'post_type' => 'book',
'offset' => 1,
'tax_query' => array(
array(
'taxonomy' => 'news_cat',
'field' => 'slug',
'terms' => array( 'disney' )
),
),
);?>
<?php $the_query = new WP_Query( $args );
?>
<?php if ($the_query->have_posts()) ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<a href="<?php echo ('/test/'.get_the_ID()); ?> "ontouchstart="" >
<li>
<div>
<figure><img src="<?php the_field('thumb'); ?>" alt="">
</figure>
</div>
<span><em><?php the_field('category'); ?></em><?php the_field('date'); ?></span>
<p><?php the_title(); ?></p>
</li>
</a>
<?php endwhile; ?>
<?php $GLOBALS['wp_query']->max_num_pages = $the_query->max_num_pages;
$args = array (
'prev_text' => '',
'next_text' => '',
'show_all' => false,
'mid_size' => 1,
'type' => 'list'
);
the_posts_pagination($args);?>
I expect the pagination to work correctly instead of showing the same contents on all pages.
As you see, I want to make the pagination work only on the posts with "disney" slug.
You can replace the code instead of your old code
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged'
) : 1;
$args = array(
'posts_per_page' => 3,
'paged' => $paged,
'post_type' => 'book',
'offset' => 1
),
);?>
<?php $the_query = new WP_Query( $args );
?>
<?php if ($the_query->have_posts()) ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<a href="<?php echo ('/test/'.get_the_ID()); ?> "ontouchstart="" >
<li>
<div>
<figure><img src="<?php the_field('thumb'); ?>" alt="">
</figure>
</div>
<span><em><?php the_field('category'); ?></em><?php the_field('date'); ?></span>
<p><?php the_title(); ?></p>
</li>
</a>
<?php endwhile; ?>
<?php $GLOBALS['wp_query']->max_num_pages = $the_query->max_num_pages;
$args = array (
'prev_text' => '',
'next_text' => '',
'show_all' => false,
'mid_size' => 1,
'type' => 'list'
);
the_posts_pagination($args);?>
Now, you can check it that pagination is working or not.
Related
I have the following set up in my template file:
<div class="flex" id="allPosts">
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array (
'posts_per_page' => 12,
'paged' => $paged,
'order' => 'DESC',
);
$wp_query = new WP_Query( $args ); ?>
<?php if ( $wp_query->have_posts() ) : ?>
<?php while ($wp_query -> have_posts()) : $wp_query -> the_post(); ?>
<div class="blog-entries">
<?php if (has_post_thumbnail()) {
$featuredImg = get_the_post_thumbnail_url();
} else {
$featuredImg = get_stylesheet_directory_uri() . '/img/blk-fri.jpg';
} ?>
<a class="blog-module" href="<?php the_permalink(); ?>">
<div class="blog-img" style="background-image: url('<?php echo $featuredImg; ?>');">
<div>
<div class="author">By <?php the_author(); ?></div>
<div class="date">
<p><?php echo get_the_date('M'); ?></p>
<p><?php echo get_the_date('d'); ?></p>
</div>
</div>
</div>
<div class="blog-content">
<h4><?php the_title(); ?></h4>
<?php the_excerpt(); ?>
</div>
</a>
</div>
<?php endwhile; ?>
<?php endif; ?>
</div>
<div class="pagination">
<?php
echo paginate_links( array(
'base' => str_replace( 999999999, '%#%', esc_url( get_pagenum_link( 999999999 ) ) ),
'total' => $wp_query->max_num_pages,
'current' => max( 1, get_query_var( 'paged' ) ),
'format' => '?paged=%#%',
'show_all' => false,
'type' => 'plain',
'end_size' => 2,
'mid_size' => 1,
'prev_next' => true,
'prev_text' => sprintf( '<i></i> %1$s', __( 'Newer Posts', 'text-domain' ) ),
'next_text' => sprintf( '%1$s <i></i>', __( 'Older Posts', 'text-domain' ) ),
'add_args' => false,
'add_fragment' => '',
) );
?>
</div>
<?php wp_reset_postdata(); ?>
This functionality is working, except on page one. Every other page limits the amount of posts seen to 12 as specified in the 'posts_per_page', but page 1 is just showing every post. Ive tried searching around for a similar problem, but have come up short. I would appreciate any and all guidance on what might be happening here, thanks!
You can view the issue here: http://listingmirror.devsite.work/blog/
try in that form
// WP_Query arguments
$args = array(
'nopaging' => false,
'paged' => '12',
'posts_per_page' => '12',
'posts_per_archive_page' => '12',
'order' => 'DESC',
'orderby' => 'title',
);
// The Query
$paged = new WP_Query( $args );
// The Loop
if ( $paged->have_posts() ) {
while ( $paged->have_posts() ) {
$paged->the_post();
// do something
}
} else {
// no posts found
}
// Restore original Post Data
wp_reset_postdata();
The imported posts from the client were all marked 'sticky' in Wordpress. This was overwriting the post_per_page argument.
I have created an archive template within my bespoke Wordpress site but I need to add pagination - I have tried different plugins/code but nothing seems to work. I currently have 6 posts setup on the backend but I have limited it to 3 posts per page for testing purposes. Existing code below:
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'ASC',
'posts_per_page' => 3,
'paged' => $paged
);
$posts_query = new WP_Query($args);
if($posts_query->have_posts()) { ?>
<div class="index-grid">
<div class="grid-column">
<?php while($posts_query->have_posts()) {
$posts_query->the_post(); ?>
<div class="related-item">
<a href="<?php the_permalink(); ?>">
<img src="<?php the_field('header_image'); ?>" />
<h3 class="title"><?php the_title(); ?></h3>
<p class="blurb"><?php the_field('introduction'); ?></p>
</a>
</div>
<?php } ?>
</div>
</div>
<?php wp_reset_postdata();
}
?>
</section>```
Any help would be greatly appreciated!
Thanks
Try this:
<div class='pagination'>
<?php
$big = 999999999;
echo paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $posts_query->max_num_pages
));
?>
<div>
I've a custom loop which should display 3 projects per category in a random order.
The problem is, that the loop always shows all projects in that category.
I've tested the the code on a single page and it works how it should and only showed 3 projects.
So I guess it has something to do with the archive page?!
Here's ist the loop itself:
<?php
$args_thema = array (
'post_type' => array( 'project' ),
'orderby' => 'rand',
'posts_per_page' => 3,
'paged' => $paged,
//'ignore_sticky_posts' => 1,
'tax_query' => array(
array(
'taxonomy' => 'project-category',
'field' => 'slug',
'terms' => $category->slug,
),
),
);
$query_thema = new WP_Query( $args_thema ); if ( $query_thema->have_posts() ) : ?>
<ul class="">
<?php $i = 0; while ( $query_thema->have_posts() ) : $query_thema->the_post(); $i++; // echo $i; ?>
<li class="">
<a class="url uid" href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php else : ?>
<?php endif; wp_reset_postdata(); ?>
And here's the full code (I guess not that relevant):
<?php
$args = array('hide_empty' => '0', 'taxonomy' => 'project-category', 'orderby' => 'name', 'order' => 'ASC', 'parent' => 0 );
$categories = get_categories($args);
foreach($categories as $category) {
?>
<div class="row">
<div class="col-lg-4">
<?php
$term_child = get_term_by('slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
$args_child = array('hide_empty' => '0', 'taxonomy' => 'project-category', 'orderby' => 'name', 'order' => 'ASC', 'parent' => $category->term_id );
?>
<h4><?php echo $category->name; ?></h4>
</div>
<div class="col-lg-8">
<?php
$args_thema = array (
'post_type' => array( 'project' ),
'orderby' => 'rand',
'posts_per_page' => 3,
'paged' => $paged,
//'ignore_sticky_posts' => 1,
'tax_query' => array(
array(
'taxonomy' => 'project-category',
'field' => 'slug',
'terms' => $category->slug,
),
),
);
$query_thema = new WP_Query( $args_thema ); if ( $query_thema->have_posts() ) : ?>
<ul class="">
<?php $i = 0; while ( $query_thema->have_posts() ) : $query_thema->the_post(); $i++; // echo $i; ?>
<li class="">
<a class="url uid" href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
</li>
<?php endwhile; ?>
</ul>
<?php else : ?>
<?php endif; wp_reset_postdata(); ?>
</div>
</div>
<?php } ?>
Sorry, it was an extra function which sets the archive posts to 99.
Istead of is_post_type_archive I used is_archive
i want to get archives of multipal custom post types can u help me guys here is my code which is not working
<?php $args = array(
'type' => 'yearly',
'limit' => '',
'format' => 'custom ',
'before' => '',
'after' => '',
'show_post_count' => false,
'echo' => 1,
'order' => 'DESC',
'post_type' => array('news','update')
);
wp_get_archives( $args );
Try This:
<?php
$args = array(
'post_type' => array('news','update'),
'post_status' => 'archive',
'type' => 'monthly',
'echo' => 0,
'order' => 'DESC'
);
$query = new WP_Query( $args );
// Check that we have query results.
if ( $query->have_posts() ) {
// Start looping over the query results.
while ( $query->have_posts() ) {
$query->the_post();
?>
<article id="post-<?php the_ID(); ?>" >
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php post_thumbnail( 'thumbnail' );?>
</a>
<a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
<?php the_excerpt(); ?>
</article>
<?php
}
}
// Restore original post data.
wp_reset_postdata();
Here is the setup for my radio button:
I can't seem to get the posts that are valued at yes to display in the loop..
Here is my loop:
<?php
$args = array(
'numberposts' => 1,
'post_type' => 'event',
'posts_per_page' => '1',
'meta_key' => 'sponsored_event',
'meta_value' => 'yes'
);
$the_query = new WP_Query( $args );
?>
<?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="sponsored-event">
<div class="sponsored-image" style="background-image: url(<?php the_field( 'event_image' ); ?>);"></div>
<div class="sponsored-info">
<h2>Sponsored Event</h2>
<h1><strong><?php the_title(); ?></strong></h1>
<p><strong>Date</strong></p><br>
<p class="place"><?php the_field( 'event_location' ); ?></p>
<p class="time"><?php the_field( 'event_time' ); ?></p>
<p><?php the_field( 'excerpt' ); ?></p>
</div>
</div>
<?php endwhile; else: ?>
<?php endif; ?>
This is what worked for me:
<?php
$args = array(
'post_type' => 'event',
'showposts' => 1,
'orderby' => 'date',
'meta_query' => array(
array(
'key' => 'sponsored_event',
'value' => 1,
'compare' => 'LIKE'
)
)
);
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<a href="<?php the_permalink(); ?>"><div class="sponsored-event">
<div class="sponsored-image" style="background-image: url(<?php the_field( 'event_image' ); ?>);">
</div>
<div class="sponsored-info">
<h2>Sponsored Event</h2>
<h1><strong><?php the_title(); ?></strong></h1>
<p><strong>Date</strong></p><br>
<p class="place"><?php the_field( 'event_location' ); ?></p>
<p class="time"><?php the_field( 'event_time' ); ?></p>
<p><?php the_field( 'excerpt' ); ?></p>
</div>
</div></a>
<?php endwhile; else: ?>
<?php endif; ?>
if ( have_posts() ) should reference the query: if ( $the_query->have_posts() ).
Also, as has already been mentioned, you should use 'posts_per_page' => 1, instead of numberposts.
Try using a meta_query to accomplish this. Update your $args with something like the following:
$args = array(
'post_type' => 'event',
'posts_per_page' => '1',
'meta_query' => array(
array(
'key' => 'sponsored_event',
'compare' => 'LIKE',
'value' => 'yes',
),
),
);
You also need to update if ( have_posts() ) to if ( $the_query->have_posts() ).
More information about meta_query can be found here: https://codex.wordpress.org/Class_Reference/WP_Meta_Query
Instead of using both post_per_page and number posts you can use any one of your choice
Try any of the Methods as available below. As per the ACF the Method 1 is suggested but as for as the WordPress you can retrieve information based on Method 2 also.
Method:1
$args = array(
'posts_per_page' => 1,
'post_type' => 'event',
'meta_key' => 'sponsored_event',
'meta_value' => 'yes'
);
$the_query = new WP_Query( $args );
<?php if ($the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
//Your Code over here to Manipulate
<?php endwhile; else: ?>
<?php endif; ?>
Method:2
Try the meta query with compare as = operator in the meta_query.
$args = array(
'post_type' => 'event',
'posts_per_page' => '1',
'meta_query' => array(
array(
'key' => 'sponsored_event',
'compare' => '=',
'value' => 'yes',
),
),
);
I tested the code on my machine and it works fine! Just (as already mentioned from others) the if() statement is wrong, change it to:
if ( $the_query->have_posts() )
But now I have a really silly question... After creating the ACF field, have you saved some posts (events) with the meta field (yes or no) ? If not, WP wont find anything because the postmeta is not saved into the DB.
Did you took a look into the DB if the postmeta is saved correctly?