I need to add pagination to archive page. Show 10 months per page.
wp_get_archives('type=monthly&show_post_count=1');
Try this
<?php
$catnam = '1';
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
// the total of registers to show on each page
$showp = 4;
wp_get_archives("type=monthly&showposts=$showp&paged=$paged");
while (have_posts() ) : the_post();?>
<h4><?php the_title();?></h4>
<?php endwhile;?>
<?php
global $wp_rewrite;
$paginate_base = get_pagenum_link(1);
if (strpos($paginate_base, '?') || ! $wp_rewrite->using_permalinks()) {
$paginate_format = '';
$paginate_base = add_query_arg('paged', '%#%');
} else {
$paginate_format = (substr($paginate_base, -1 ,1) == '/' ? '' : '/') .
user_trailingslashit('page/%#%/', 'paged');;
$paginate_base .= '%_%';
}
echo '<div id="pagination">';
echo paginate_links( array(
'base' => $paginate_base,
'format' => $paginate_format,
'total' => $wp_query->max_num_pages,
'mid_size' => 10,
'current' => ($paged ? $paged : 1),
'type' => '',
'prev_text' => __('<p class="readmore">« Previous</p>', 'default'),
'next_text' => __('<p class="readmore">Next »</p>', 'default'),
));
echo "</div>";
?>
Related
I have a problem , my code is make pagination post_meta , its work , but link to paginate still use https://yourweb.com/post-title/2 not use paginate link https://yourweb.com/post-title/page/2 , and if i type this link https://yourweb.com/post-title/page/2 this will return to https://yourweb.com/post-title/ , and nav pagination not show
this my code
$paged = (get_query_var('paged')) ? absint( get_query_var('paged')) : 1 ;
$ids = get_the_ID();
$args = array('paged' => $paged , 'post__in' => array($ids));
$the_query = new WP_Query($args);
if ($the_query->have_posts()) :
while ($the_query->have_posts()) : $the_query->the_post();
$metas = get_post_meta(get_the_ID(),'value_gallery',false);
foreach ($metas as $meta) {
$key_val = explode(",", $meta);
$image_chunk = array_chunk($key_val, 3);
$page = get_query_var('page');
$page = $page > 1 ? $page - 1 : 0 ;
if (isset($key_val[$page])) {
foreach ($image_chunk[$page] as $image) {
echo "<div class='col-lg-4'>".
wp_get_attachment_image($image,"cherry-thumb-a") ."</div>";
}
}
}
endwhile;
the_posts_pagination( array(
'mid_size' => 2,
'prev_text' => __( '<', 'textdomain' ),
'next_text' => __( '>', 'textdomain' ),
) );
wp_reset_postdata();
endif;
i will happy if anyone can help me :) ,many thanks for attention
I am trying to make a pagination from meta_post (meta_key,meta_value) ==> (name_student,array(john,mike,natasha,joan,buck)) but i dont know how to make it, from codex just make pagination post , not a meta_post , and my meta_post in 1 array , i was find a similar question 1 , 2 , but I still haven't found the answer for my problem , if anyone can help me to find sollution for my problem I will be greatly helped , thankyou for read my problem , hopefully can help me
this my code :
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$ids = get_the_ID();
$args = array('paged' => $paged , 'post__in' => array($ids));
$the_query = new WP_Query($args);
if (have_posts()) :
while (have_posts()) : the_post();
$metas = get_post_meta(get_the_ID(),'name_student',false);
foreach ($metas as $meta) {
$key_val = explode(",", $meta);
$image_chunk = array_chunk($key_val, 3);
$page = get_query_var('page');
$page = $page > 1 ? $page - 1 : 0 ;
if (isset($key_val[$page])) {
foreach ($image_chunk[$page] as $image) {
echo "<div class='col-lg-4'>".
wp_get_attachment_image($image,"thumbnail") ."</div>";
}
}
}
endwhile;
$big = 9999;
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $the_query->max_num_pages
) );
wp_reset_postdata();
endif;
Right now I'm using the following code to display all posts by year:
index.php
<?php foreach(posts_by_year() as $year => $posts) : ?>
<h2><?php echo $year; ?></h2>
<ul>
<?php foreach($posts as $post) : setup_postdata($post); ?>
<li>
<?php the_title(); ?>
</li>
<?php endforeach; ?>
</ul>
<?php endforeach; ?>
functions.php
function posts_by_year() {
// array to use for results
$years = array();
// get posts from WP
$posts = get_posts(array(
'numberposts' => -1,
'orderby' => 'post_date',
'order' => 'ASC',
'post_type' => 'post',
'post_status' => 'publish'
));
// loop through posts, populating $years arrays
foreach($posts as $post) {
$years[date('Y', strtotime($post->post_date))][] = $post;
}
// reverse sort by year
krsort($years);
return $years;
}
And right now I have set at the reading options in the admin panel that I only want to show 3 posts per page. With this new code, the posts by year, it displays all posts and in the pagination buttons there are 3 pages. When switching to another page, it displays the same content all over again.
How can I include the pagination now that I have posts by year? My pagination code is:
<?php if(function_exists('wpex_pagination')) { wpex_pagination(); } ?>
Which calls the function:
if ( !function_exists( 'wpex_pagination' ) ) {
function wpex_pagination() {
$prev_arrow = is_rtl() ? '→' : '←';
$next_arrow = is_rtl() ? '←' : '→';
global $wp_query;
$total = $wp_query->max_num_pages;
$big = 999999999; // need an unlikely integer
if( $total > 1 ) {
if( !$current_page = get_query_var('paged') )
$current_page = 1;
if( get_option('permalink_structure') ) {
$format = 'page/%#%/';
} else {
$format = '&paged=%#%';
}
echo paginate_links(array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => $format,
'current' => max( 1, get_query_var('paged') ),
'total' => $total,
'mid_size' => 3,
'type' => 'list',
'prev_text' => $prev_arrow,
'next_text' => $next_arrow,
) );
}
}
}
The idea is that I'm able to set a number of posts to be displayed. Not by year, but by posts. So if I have 30 from 2014 and 20 from 2013 and have the limit set to 5, it'll only show the first 5 from 2014.
Thanks in advance!
EDIT: Also found the following code, which works, but only on the first page. Meaning that in the first page it shows the date once, but in the following pages it shows the date for each post.
<?php
$date = 0;
$newDate = true;
if (have_posts()) : while (have_posts()) :
the_post();
if ($date == 0)
$date = the_date('Y');
else if ($date != the_date('Y')) {
$date = the_date('Y');
$newDate = true;
}
if ($newDate)
echo $date . ' ';
$newDate = false; ?>
<?php get_template_part('content'); ?>
<?php endwhile; endif; ?>
I want to show the category name and category thumbnail, it need to show the 5 category per page and rest of in other respective pages. please any one have solution for this.
here is the code:
<?php
$posts_per_page = 4;
$paged = ( get_query_var( 'paged' ) ) ? get_query_var( 'paged' ) : 1;
$offset = ( $paged - 1 );
global $paged;
$curpage = $paged ? $paged : 1;
$args = array(
'child_of' => 4,
'order_by' => 'name',
'paged' => $paged
);
$categories = get_categories( $args );
foreach($categories as $category) {
echo '' . $category->name.'';
if(has_category_thumbnail($category->cat_ID)) {
the_category_thumbnail($category->cat_ID);
}
}
echo '
<div id="wp_pagination">
<a class="previous page button" href="'.get_pagenum_link(($curpage-1 > 0 ? $curpage-1 : 1)).'">‹</a>';
for($i=1;$i<=$categories->max_num_pages;$i++)
echo '<a class="'.($i == $curpage ? 'active ' : '').'page button" href="'.get_pagenum_link($i).'">'.$i.'</a>';
echo '<a class="next page button" href="'.get_pagenum_link(($curpage+1 <= $categories->max_num_pages ? $curpage+1 : $categories->max_num_pages)).'">›</a>
</div>
';
Finally I got the solution for the above problem. I hope it helpful for all others having a same problem.
Try this code:
<?php
$args = array(
'child_of' => 4,
'orderby' =>'date',
'order' =>'ASC'
);
$categories = get_categories($args);
$numOfItems = 4;
$page = isset( $_GET['cpage'] ) ? abs( (int) $_GET['cpage'] ) : 1;
$to = $page * $numOfItems ;
$current = $to - $numOfItems;
$total = sizeof($categories);
?>
<div id="ns-main">
<?php
for ($i=$current; $i<$to; ++$i) {
$category = $categories[$i];
?>
<div class="ns-wrap">
<?php
if ($category->name) {
if(has_category_thumbnail($category->cat_ID)) {
the_category_thumbnail($category->cat_ID);
}
echo '' . $category->name.'';
}
?>
</div>
<?php
}
?>
</div>
<div id="wp_pagination">
<?php
unset($category);
echo paginate_links( array(
'base' => add_query_arg( 'cpage', '%#%' ),
'format' => '',
'prev_text' => __('«'),
'next_text' => __('»'),
'total' => ceil($total / $numOfItems),
'current' => $page
));
?>
</div>
I have implemented a pagination links at the end of my wordpress blog page. In a one page I have 3 posts and when I click the next link of my pagination links it take me to the next page which contains the next 3 posts.
I retrieve posts from only one category. But when I go to the next page the html title of that page is "Page not found"
My code is like below:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('cat'=> 4, 'posts_per_page' => 3, 'paged' => $paged );
query_posts($args);
if(have_posts()) :
print ('<div class="row">');
while (have_posts()) : the_post();
$excerpt = get_the_excerpt();
//My post contents
endwhile;
if (function_exists("pagination")) {
pagination();
}
print ('<!-- end main row --></div>');
endif;
}
My pagination function is:
function pagination() {
/*
post: retun the pagination post cout and next previous buttons
*/
global $paged;
if(empty($paged)) $paged = 1;
if($pages == '') {
global $wp_query;
$pages = $wp_query->max_num_pages;
if(!$pages) {
$pages = 1;
}
}
if(1 != $pages) {
print('<section >');
if ($paged < $pages)
echo "NEXT";
if($paged <= $pages && $paged > 1)
echo "<a href='".get_pagenum_link($paged - 1)."'>BACK</a>";
echo "<p >Page ".$paged." of ".$pages."</p>";
echo "</section>";
}
}
Please help me for this
Try this insteatd
<?php
$big = 999999999; // need an unlikely integer
echo paginate_links( array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => max( 1, get_query_var('paged') ),
'total' => $the_query->max_num_pages
) );
?>
Try to add this
paginate_links(array(
'base' => get_pagenum_link(1) . '%_%',
'format' => 'page/%#%',
'current' => $current_page,
'total' => $total_pages,
'cat' => 4,
));
You missed category ID (cat=>4)
also change:
ADMIN PANEL -> SETTING -> READING -> Blog pages show at most : 3
as per $args = array('cat'=> 4, 'posts_per_page' => 3, 'paged' => $paged );