I have custom post types called clients, need to display 5 clients per page with pagination. The page what i have is page-clients.php
I have used the wp_pagenavi plugin.
I get a perfect navigation list 1,2,3 etc etc but on clicking them takes me to page not found
My Code
$args = array(
'posts_per_page' => 5,
'post_type' => 'clients',
'paged' => get_query_var('page')
);
query_posts($args);
<?php while ( have_posts() ) : the_post(); ?>
.....
<?php endwhile; // end of the loop. ?>
<?php wp_pagenavi(); ?>
<?php wp_reset_query();?>
Heres the way you can do it without pagination plugin :) using WP_QUERY instead of query_posts
$paged = get_query_var( 'paged' ) ? get_query_var( 'paged' ) : 1; // setup pagination
$the_query = new WP_Query( array(
'post_type' => 'clients',
'paged' => $paged,
'posts_per_page' => 5)
);
while ( $the_query->have_posts() ) : $the_query->the_post();
echo '<div>' . get_the_title() . '</div>';
the_content();
endwhile;
echo '<nav>';
echo '<div>'.get_next_posts_link('Older', $the_query->max_num_pages).'</div>'; //Older Link using max_num_pages
echo '<div>'.get_previous_posts_link('Newer', $the_query->max_num_pages).'</div>'; //Newer Link using max_num_pages
echo "</nav>";
wp_reset_postdata(); // Rest Data
Pagination Like : Prev 1 2 3 Next
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$data= new WP_Query(array(
'post_type'=>’YOUR_POST_TYPE’, // your post type name
'posts_per_page' => 5, // post per page
'paged' => $paged,
));
if($data->have_posts()) :
while($data->have_posts()) : $data->the_post();
// Your code
endwhile;
$total_pages = $data->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 »'),
));
}
?>
<?php else :?>
<h3><?php _e('404 Error: Not Found', ''); ?></h3>
<?php endif; ?>
<?php wp_reset_postdata();?>
Would you please try above code?
Related
How can I run a loop within a loop in WordPress?
My attempt is this so far but it seems to crash the server each time.
<?php $loop = new WP_Query( array('post_type' => 'careers', 'posts_per_page' => '10', 'meta_key' => 'company', 'meta_value' => get_field('company') )); ?>
<?php $loop2 = new WP_Query( array('post_type' => 'jobs', 'posts_per_page' => '-1', 'meta_key' => 'company', 'meta_value' => get_field('company') ));?>
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php $i = 0; while ( $loop2->have_posts() ) : $loop->the_post(); $i++; ?>
<?php echo $i ?>
<?php endwhile; wp_reset_query(); update_field('jobscounter', $i); ?>
<?php endwhile; wp_reset_query(); ?>
In PHP, you can open a code section by <?php and close it later with ?>. With that in mind, you can place all your code in multiple lines without opening and closing the <?php ?> block over and over.
Example:
<?php
$loop = new WP_Query(array(
'post_type' => 'careers',
'posts_per_page' => '10',
'meta_key' => 'company',
'meta_value' => get_field('company')
));
$loop2 = new WP_Query(array(
'post_type' => 'jobs',
'posts_per_page' => '-1',
'meta_key' => 'company',
'meta_value' => get_field('company')
));
while ($loop->have_posts()):
$loop->the_post();
$i = 0;
while ($loop2->have_posts()):
$loop2->the_post();
$i++;
echo $i;
endwhile;
wp_reset_query();
update_field('jobscounter', $i);
endwhile;
wp_reset_query();
?>
Note: you where missing a semicolon ; after the echo $i
Note 2: the second loop should use $loop2-> instead of $loop->
Then, you should do something with the data you process in each iteration. If you don't want to just count the total entries. :)
For example:
while ($loop2->have_posts()):
$loop2->the_post();
$i++;
$post_title = $loop2->the_title(); // <--- Getting the title :)
echo $post_title . "\n";
endwhile;
I trying to add pagination to my WordPress website, it's working fine with blog page, but with custom category when click any link of pagination URL change, but content still the same.
Custom category code
<?php query_posts('category_name=sport');
if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php get_template_part('loop'); ?>
<?php endwhile; // конец цикла
else: echo '<p>Blank.</p>'; endif; ?>
<?php pagination(); ?>
Pagintion in function.php
function pagination() {
global $wp_query;
$big = 999999999;
$links = paginate_links(array(
'base' => str_replace($big,'%#%',esc_url(get_pagenum_link($big))),
'format' => '?paged=%#%',
'current' => max(1, get_query_var('paged')),
'type' => 'array',
'prev_text' => 'Назад',
'next_text' => 'Вперед',
'total' => $wp_query->max_num_pages,
'show_all' => false,
'end_size' => 4,
'mid_size' => 4,
'add_args' => false,
'add_fragment' => '',
'before_page_number' => '',
'after_page_number' => ''
));
if( is_array( $links ) ) {
echo '<ul class="pagination">';
foreach ( $links as $link ) {
if ( strpos( $link, 'current' ) !== false ) echo "<li class='active'>$link</li>";
else echo "<li>$link</li>";
}
echo '</ul>';
}
}
Please help
instead of: query_posts('category_name=sport');
try this:
$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'category_name' => 'sport',
'posts_per_page' => 6,
'paged' => $page
);
query_posts($args);
For further customization check the List of Query Vars
Try this:
<?php
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$args = array(
'posts_per_page' => 3,
'paged' => $paged,
'category_name' => 'sport'
);
$the_query = new WP_Query( $args );
?>
Hope this helps :)
Please check below code:
$page = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array(
'post_type' => 'work',
'categories'=>'sports',
'posts_per_page' => 2,
'paged' => $page,
);
query_posts($args);
if (have_posts()) : while (have_posts()) : the_post(); ?>
<h3><?php the_title(); ?></h3>
<?php the_content(); ?>
<?php endwhile;
else: echo '<p>Blank.</p>'; endif; ?>
<?php pagination(); ?>
You have to mention post type also as per the example
Hope this work for you.
I'm trying to make pagination for result of two post types which is one custom post type question and the normal post
first of all when I show all result without pagination using 'posts_per_page' => -1; the function works fine
but the problem happen when I try to make pagination, as you can see pagination function works normally except the some last pages of the pagination (I guess which contains question post_type even recent post type of questions appears normally )
here you are my full code
<?php
$my_query = new WP_Query(
array(
'post_type' => array('question', 'post'),
'posts_per_page' => get_option('to_count_portfolio'), // -1 to show all results
'author' => $post->post_author,
'paged' => get_query_var('paged') ? get_query_var('paged') : 1
)
);
?>
<?php
while ( $my_query->have_posts() ) : $my_query->the_post();
?>
<span class="title">
<?php echo get_post_type(); ?>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</span>
<br />
<?php endwhile; ?>
<?php
$big = 999999999; // need an unlikely integer
echo paginate_links(
array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $my_query->max_num_pages
)
);
?>
How can I make pagination work normally and navigate to all pagination links ?
Replace your code this:
<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
if ($paged == "1") {
$args = array(
'post_type' => array('question', 'post'),
'posts_per_page' => get_option('to_count_portfolio'), // -1 to show all results
'author' => $post->post_author,
'offset' => 0
);
} else {
$offset = $paged * 5;
$offset = $offset - 5;
$args = array(
'post_type' => array('question', 'post'),
'posts_per_page' => get_option('to_count_portfolio'), // -1 to show all results
'author' => $post->post_author,
'offset' => $offset
);
}
$loop = new WP_Query($args);
?>
<?php
if ($loop->have_posts()) :while ($loop->have_posts()) : $loop->the_post();
?>
<span class="title">
<?php echo get_post_type(); ?>
<a href="<?php the_permalink(); ?>">
<?php the_title(); ?>
</a>
</span>
<br />
<?php endwhile; ?>
<div class="pagination-grp">
<?php
$big = 999999999; // need an unlikely integer
//$i=1;
echo paginate_links(array(
'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
'format' => '?paged=%#%',
'current' => max(1, get_query_var('paged')),
'prev_text' => __('<'),
'next_text' => __('>'),
'total' => $loop->max_num_pages
));
wp_reset_postdata();
endif;
?>
</div>
install this free plugin https://wordpress.org/plugins/wp-pagenavi/
then refer below code
$paged = ( get_query_var('paged') ) ? get_query_var('paged') : 1;
$args = array (
'post_type' => 'post',
'cat' => '53',
'paged' => $paged,
'posts_per_page' => '10',
);
// The Query
$query = new WP_Query( $args );
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
get_template_part('parts/bl`enter code here`ogloop');
}
if(function_exists('wp_pagenavi'))
{
wp_pagenavi( array( 'query' => $query ) );
}
wp_reset_postdata();
} else {
echo 'no posts here';
}
This will show paging and work like a charm for you.
I guess that I figure it out
the solution is to edit the WP_Query args to be a custom number which is not same as site posts_per_page
'posts_per_page' => 11, // 13, or 20 ...
instead of
'posts_per_page' => get_option('to_count_portfolio'),
Why ? I've no idea, maybe the are some kind of conflict
someone can try it and see if that solution works with him/her same as me
I just created a new custom page template and called two posts only.
How can I add pagination so that I can the link with at least two of the lastest posts?
I tried this code but it does not work:
<?php
$paged = ( get_query_var('paged') ) ? get_query_var( 'paged' ) : 1;
query_posts(
array (
'post_type' => 'post',
'category_name' => 'news',
'category' => 1,
'posts_per_page' => 2,
'paged' => $paged )
);
// The Loop
while ( have_posts() ) : the_post();?>
<div class="news-page-content-wrapper">
<div class="news-page-content">
<h1><a class="read-more"href="<?php the_permalink(); ?>"><?php the_title();?></a></h1>
<figure><?php the_post_thumbnail(); ?></figure>
<p><?php echo get_the_excerpt();?></p>
Read More»
</div>
</div>
<?endwhile;
// Reset Query
wp_reset_query();
?>
Any help?
Since you're using "the loop" you should use the built in function for displaying pagination.
Here's some examples for you: https://codex.wordpress.org/Pagination
I've updated your sample code to show the default pagination:
<?php
$paged = ( get_query_var('paged') ) ? get_query_var( 'paged' ) : 1;
query_posts(
array (
'post_type' => 'post',
'category_name' => 'news',
'posts_per_page' => 2,
'paged' => $paged )
);
// The Loop
while ( have_posts() ) : the_post(); ?>
<div class="news-page-content-wrapper">
<div class="news-page-content">
<h1><a class="read-more"href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
<figure><?php the_post_thumbnail(); ?></figure>
<p><?php echo get_the_excerpt(); ?></p>
Read More»
</div>
</div>
<?php endwhile;
the_post_navigation();
// Reset Query
wp_reset_query();
?>
I sill faced that issue and solved it like that
Pagination function
function post_pagination($paged = '', $max_page = '') {
if (!$paged) {
$paged = (get_query_var('paged')) ? get_query_var('paged') : ((get_query_var('page')) ? get_query_var('page') : 1);
}
if (!$max_page) {
global $wp_query;
$max_page = isset($wp_query->max_num_pages) ? $wp_query->max_num_pages : 1;
}
$big = 999999999; // need an unlikely integer
$html = paginate_links(array(
'base' => str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
'format' => '?paged=%#%',
'current' => max(1, $paged),
'total' => $max_page,
'mid_size' => 1,
'prev_text' => __('« Prev'),
'next_text' => __('Next »'),
));
$html = "<div class='navigation pagination'>" . $html . "</div>";
echo $html;
}
Query on the custom template
$paged = (get_query_var('paged')) ? get_query_var('paged') : ((get_query_var('page')) ? get_query_var('page') : 1);
$args = array(
'post_type' => 'post',
'post_status' => 'publish',
'posts_per_page' => get_option('posts_per_page'),
'order' => 'DESC',
'orderby' => 'date',
'paged' => $paged
);
$loop = new WP_Query($args);
if ($loop->have_posts()) :
while ($loop->have_posts()) :
$loop->the_post();
get_template_part('content', get_post_format());
endwhile;
post_pagination($paged, $loop->max_num_pages);
endif;
wp_reset_postdata();
Using below code i'm listing all posts under custom post type people
<?php $loop = new WP_Query(array('post_type' => 'people', 'order' => 'ASC', 'orderby' => 'meta_value', 'meta_key' => 'wpcf-people-sort-order','posts_per_page' => 4, 'paged' => get_query_var('paged') ? get_query_var('paged') : 1 )
); ?>
<div>Title: <?PHP the_title(); ?></div>
<div>Description: <?php echo esc_html( get_post_meta( $postid, 'wpcf-people-desscription', true ) ); ?> </div>
<?php endwhile; ?>
Below is my pagination,
<div class="cus-pagi">
<?php
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $loop->max_num_pages
) );
?>
</div>
Also i created a plugin using above code to display list of titles in sidebar. So whenever i access mysite.com/people both(i.e list of custom posts with pagination & sidebar list of post title) of my custom query are working fine.
If i go to second page, sidebar is showing empty.
did anyone know where i'm going wrong ?
You you need to use the Loop with your custom query:
<?php
$loop = new WP_Query(
array(
'post_type' => 'people',
'order' => 'ASC',
'orderby' => 'meta_value',
'meta_key' => 'wpcf-people-sort-order',
'posts_per_page' => 4,
'paged' => get_query_var('paged') ? get_query_var('paged') : 1 )
);
if ($loop->have_posts()) : while ($loop->have_posts()) : $loop->the_post();
?>
<div>Title: <?PHP the_title(); ?></div>
<div>Description: <?php echo esc_html( get_post_meta( $postid, 'wpcf-people-desscription', true ) ); ?> </div>
<?php endwhile; endif; wp_reset_postdata(); ?>