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
Related
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(); ?>
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.
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));
}
I got one "front-page.php" that is a static one side page. If I use the Wordpress loop to see my latest posts at the front-page.php they all shown up.
Now I want to create a news page so I created a file "page-news.php". Removed the loop code from front-page and pasted it into page-news. Though, nothing happens.
Loop code:
<?php get_header();?>
<?php
if (have_posts()):
while (have_posts()): the_post();?>
<?php the_title();?>
<?php the_content();?>
<?php
endwhile;
else: echo '<p>no posts were found</p>';
endif;
?>
<?php get_footer();?>
What have I missed?
you need to add wp_Query
the main page is consider a blog page so it have the Query default .
$args = array (
/*'cat' => $catNum,*/
'post_type' => 'post',
'pagination' => false,
'posts_per_page' => '-1',
'ignore_sticky_posts' => false,
'order' => 'DESC',
'orderby' => 'date',
);
// The Query
$query = new WP_Query( $args );
you should add this code before
if (have_posts()):
while (have_posts()): the_post();?>
the this part about have_posts() will be
// The Loop
if ( $query->have_posts() ) { ?>
<?php while ( $query->have_posts() ) {
$query->the_post();
dont forget to add wp_reset_postdata(); at the end so you can use many Query in one page .
I have a specific category-slug.php page.
I want to display the first category "banner-ads" with only 1 post per page and then below that, display 3 posts per page from the category "featured."
I don't want to use:
**query_posts( 'posts_per_page=4' );**
I've tried the pre_get_posts function but can't seem to get it working.
Right now the number of posts per page that's displaying is the number I assigned in Settings->Reading
Here's my current code:
$args1 = array(
'category_name' => 'banner-ads',
'posts_per_page' => 1
);
$the_query = new WP_Query( $args1 );
while ( $the_query->have_posts() ) :
$the_query->the_post();
ar2_render_posts( null, array ( 'type' => 'node' ), true );
endwhile;
wp_reset_postdata();
$args2 = array(
'category_name' => 'featured',
'posts_per_page' => 3
);
$query2 = new WP_Query( $args2 );
while( $query2->have_posts() ):
$query2->next_post();
ar2_render_posts( null, array ( 'type' => 'traditional' ), true );
endwhile;
wp_reset_postdata();
You haven't made reset: wp_reset_postdata();
Useful about categories: http://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
Here is mine for one of pages
First loop:
<?php
$postq1 = new WP_Query(
array(
'post_type' => array('post','yourcustom'),
'posts_per_page' => 1,
'category_name'=>'banner-ads')
);
if($postq1->have_posts()):
while ( $postq1->have_posts() ) :
$postq1->the_post();?>
<article id="post-<?php the_ID();?>">....</article>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
Second loop:
<?php
$postq2 = new WP_Query(
array(
'post_type' => array('post','yourcustom'),
'posts_per_page' => 3,
'category_name'=>'featured')
);
if($postq2->have_posts()):
while ( $postq2->have_posts() ) :
$postq2->the_post();?>
<article id="post-<?php the_ID();?>">....</article>
<?php
endwhile;
endif;
wp_reset_postdata();
?>
... rest of code with query_posts();
Okay so I finally figured it out. If you want to force it to take certain number of posts then add the filter with its function then at the end of the loop remove it. I realized for my second loop that the ar2_render_posts() function was conflicting with the code so I'm opting to redoing that function from scratch since its basically a layout function.
add_filter('post_limits', 'my_post_limits');
function my_post_limits( $limit ) {
if ( in_category('banner-ads') ) {
return 'LIMIT 0, 3';
}
return $limit;
}
$args1 = array(
'category_name' => 'banner-ads'
);
$the_query = new WP_Query( $args1 );
while ( $the_query->have_posts() ) :
$the_query->the_post();
ar2_render_posts( null, array ( 'type' => 'node' ), true );
endwhile;
wp_reset_postdata();
remove_filter('post_limits', 'my_post_limits');
it is pretty simple, just alter query_args in ar2_render_posts
ar2_render_posts( null, array (
'query_args' => array ( 'posts_per_page' => 1 ),
), true );