WordPress Pagination Failing To Display - php

I'm working on adding pagination to a page and I am following along on the official codex but I am not getting results. Right now I want to populate pagination after 1 post per page (for testing purposes). No pagination shows, the h3 tags return empty. I have tried multiple queries other than the one below to no avail. Help is sorely needed and appreciated.
<?php
$paged = (get_query_var ('paged')) ? get_query_var ('paged') : 1;
$args = array(
'post_type' => array('post'),
'posts_per_page' => 1,
'paged' => $paged,
'cat' => 5,
'tag__not_in' => 22
);
$the_query = new WP_Query ( $args );
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<!-- THE CONTENT -->
<?php endwhile; ?>
<h3><?php next_posts_link('Next'); ?></h3>
<h3><?php previous_posts_link('Previous');?></h3>
<?php endif; ?>
<?php wp_reset_postdata(); ?>

Related

Wordpress blog pagination

I have a posts loop in home.php:
<?php
$args = array(
'post_type' => 'post',
'posts_per_page' => 2
);
$the_query = new WP_Query( $args );
if ( $the_query->have_posts() ) {
while ( $the_query->have_posts() ) {
$the_query->the_post();
the_title();
the_content();
}
wp_reset_postdata();
} ?>
How can I add pagination (numbers)? I looked for a tutorial. I tried a lot of functions but none worked.
Thank You
Should be possible using this code:
<?php the_posts_pagination( array( 'mid_size' => 2 ) ); ?>
mid_size defines how many page numbers will be displayed on either side of the current page in that line.
For more details including individual texts for the previous/next page links see:
https://codex.wordpress.org/Function_Reference/the_posts_pagination

Iterating over correct number of wordpress posts from category + custom post type

Our wordpress post loop combines and displays posts from a specific category and a custom post type. This works, but is not displaying all posts. I believe that the post loop is iterating over the number of posts in the specific category, not the number of posts in the specific category + the number of posts in the custom post type. How can I ensure that the correct number of posts are being displayed?
<?php
/*
Template Name: Articles & Cases
*/
get_header(); ?>
<div class="center-holder">
<div id="content">
<?php while ( have_posts() ) : the_post(); ?>
<?php the_title( '<h1>', '</h1>' ); ?>
<?php the_post_thumbnail( 'full' ); ?>
<?php the_content(); ?>
<?php if ( $cats = get_field( 'category' ) ) : ?>
<?php
$args = array(
'post_type' => array( 'post' ),
'category__in' => $cats,
'fields' => 'ids',
);
$articles = new WP_Query( $args );
wp_reset_postdata();
$args = array(
'post_type' => array( 'case_study' ),
'fields' => 'ids',
);
$case_study = new WP_Query( $args );
wp_reset_postdata();
$all_posts_ids = array_merge( $articles->posts, $case_study->posts );
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$args = array(
'post_type' => array( 'post', 'case_study' ),
'post__in' => $all_posts_ids,
'paged' => $paged,
);
query_posts( $args );
?>
<?php if ( have_posts() ) : ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'blocks/content', get_post_type() ); ?>
<?php endwhile; ?>
<?php get_template_part( 'blocks/pager' ); ?>
<?php else: ?>
<?php get_template_part( 'blocks/not_found' ); ?>
<?php endif; wp_reset_query(); ?>
<?php endif; ?>
<?php endwhile; ?>
</div>
<?php get_sidebar( 'blog' ); ?>
</div>
<?php get_footer(); ?>
There is a number of things you're doing wrong.
You are using a page template, and then are looping using the loop.
This probably pulls all posts, as the last fallback for page template is index.php (as seen in the diagram here).
Then you are making 3 additional queries while in the loop (so for every post you loop you make 3 extra queries).
And the last query, you are using query_posts() which is overriding the main query. Just don't ever use that.
So the plan of attack should be:
What do I want to show?
How and where do I want to show it?
What do I need to do to achieve this?
Start writing things needed to achieve this.
???
Profit!!!
If you want to control specific taxonomy, use $taxonomy.php template (with $taxonomy being the name of the taxonomy.).
Hope this helps.

Multiple Wordpress loops, hide first loop on second page

I've construced two Wordpress loops with numeric pagination as follows:
<div class="container" style="background:#ccc">
<?
$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' => false,
'category_name' => 'latest',
'posts_per_page' => 1,
);
$query1 = new WP_Query( $args1 );
while ( $query1->have_posts() ) : $query1->the_post();
the_title();
the_category(' ');
the_excerpt();
endwhile;
?>
<!-- second -->
<?
$args2 = array(
'paged' => $paged2,
'category_name' => 'uncategorized',
'posts_per_page' => 2,
);
$query2 = new WP_Query( $args2 );
while ( $query2->have_posts() ) : $query2->the_post();
the_title();
the_category(' ');
the_excerpt();
endwhile;
$pag_args2 = array(
'format' => '?paged2=%#%',
'current' => $paged2,
'total' => $query2->max_num_pages,
'add_args' => array( 'paged1' => $paged1 )
);
echo paginate_links( $pag_args2 );
?>
</div>
<!-- container -->
Everything works fine, but the paginatin page also shows the 'latest' post loop, although I've set the pagination to false.
How could I hide the first loop on the pagination pages?
Advice appreciated.
use wp_reset_postdata() function after your loop end
follow this code, may be it'll help you
<?php
// example args
$args = array( 'posts_per_page' => 3 );
// the query
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : ?>
<!-- start of the loop -->
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<?php the_title(); ?>
<?php the_excerpt(); ?>
<?php endwhile; ?><!-- end of the loop -->
<!-- put pagination functions here -->
<?php wp_reset_postdata(); ?>
<?php endif; ?>

WordPress wp_pagenavi for CPT using custom wp_query() on another CPT single page

I know there were a lot of questions about how to use wp_pagenavi with custom wp_query() for CPTs, and I can handle it easily. But I've encountered a bit more complicated situation.
I have a CPT named 'project' and CPT named 'avtoritet_audiotape'. What I need to do - is to show all CPT's 'avtoritet_audiotape' using pagination, but on the single 'project' CPT - it's a kind of relation (for example while I am on page http://simpex/project/radioprogramma-avtoritet, where 'project' is my CPT name).
My code, which is working perfectly on any archive pages, or even single pages is:
<?php while ( have_posts() ) : the_post(); ?>
<?php the_title(); ?>
Information related to my single CPT instance 'project'
<?php endwhile; ?>
<?php $loop = new WP_Query(
array(
'post_type' => 'avtoritet_audiotape',
'posts_per_page' => 6,
'paged' => get_query_var('paged'),
'meta_key' => 'audiotape_date',
'orderby' => 'meta_value_num',
) );
?>
<?php if ( $loop->have_posts() ): ?>
#Some stuff
<?php wp_pagenavi( array( 'query' => $loop ) ); ?>
<?php endif; ?>
So, pagination shows pagination links correctly, but when trying to access http://simpex/project/radioprogramma-avtoritet/page/2 - it redirects to http://simpex/project/radioprogramma-avtoritet
Is it possible in anyway? Thank you
plz try this way.
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$postArray = array('post_type' => 'room', 'posts_per_page' => 5, 'order' => 'DESC', 'paged' => $paged);
$roomPost = new WP_Query($postArray);
global $post;
if ($roomPost->have_posts()) {
while ($roomPost->have_posts()) : $roomPost->the_post(); ?>
<div class="post_title"> <?php the_title(); ?></div>
<?php
endwhile;
wp_pagenavi(array('query' => $roomPost));
}

Custom wordpress loop pagination not working

I have a website and i have a few loops from different categories on one page. the posts display like i want them to. At the bottom of the page i want to display all of the posts from the site. i have also got this working correctly. however the posts wont paginate, when i click on "newer posts" i am taken from this url: sitename/blog/ to sitename/blog/page/2/ but there is nothing found there and im getting an error
"Oops! That page can’t be found . which is found in my 404 page."
for my first two loops i have used the following code (with different queries)
<?php query_posts('cat=2&showposts=3'); ?>
however from research i have found this is the wrong way to do it and i should be querying posts like:
$the_query = new WP_Query($query_args);
So i have changed the query at the bottom of the page to this format, and temporarily removed the top two (wrong) queries. However im still getting the same error.
Could someone please help. My php knowledge is limited and this is stretching me. the full code i am using to display the post and paginate is:
<?php
$query_args = array(
'cat' => '3',
'post_type' => 'post',
'posts_per_page' => 6,
'paged' => $paged
);
// Get current page and append to custom query parameters array
$query_args['paged'] = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1;
$the_query = new WP_Query($query_args);
// Pagination fix
$temp_query = $wp_query;
$wp_query = NULL;
$wp_query = $the_query;
?>
<?php if ( $the_query->have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<div class="articleThirdBlock">
<div class="post__thumbnail post__thumbnail--marginTop">
<?php the_post_thumbnail(array(400,250)); ?>
</div><!-- /.post__thumbnail -->
<h2 class="other__title"><?php the_title(); ?></h2>
</div><!-- /.articleThirdBlock -->
<?php endwhile; endif; ?>
<?php wp_reset_postdata();
// 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;
?>
Custom Pagination Like « prev 1 2 3 next »
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type'=>'post', // Your post type name
'posts_per_page' => 6,
'paged' => $paged,
);
$loop = new WP_Query( $args );
if ( $loop->have_posts() ) {
while ( $loop->have_posts() ) : $loop->the_post();
?>
<div class="articleThirdBlock">
<div class="post__thumbnail post__thumbnail--marginTop">
<?php the_post_thumbnail(array(400,250)); ?>
</div><!-- /.post__thumbnail -->
<h2 class="other__title"><?php the_title(); ?></h2>
</div><!-- /.articleThirdBlock -->
<?php
endwhile;
$total_pages = $loop->max_num_pages;
if ($total_pages > 1){
$current_page = max(1, get_query_var('paged'));
echo paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => '/page/%#%',
'current' => $current_page,
'total' => $total_pages,
'prev_text' => __('« prev'),
'next_text' => __('next »'),
));
}
}
wp_reset_postdata();
?>

Categories