create pagination post meta_post - wordpress

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;

Related

nav pagination not show

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

set post per page using get_post_meta

I have code to show get_meta_post per page this my code
if ( have_posts() ) :
while ( have_posts() ) : the_post();
$metas = get_post_meta(get_the_ID(),'value_gallery',false);
//var_dump($metas); ==> will output array(1) { [0]=> string(29) "1434,1402,1434,1435,1398,1434" }
foreach ($metas as $key ) {
$key_val = explode(",", $key);
$page = get_query_var('page');
$page = $page > 1 ? $page - 1 : 0 ;
if (isset($key_val[$page])) {
echo "<div class='col-lg-4'>".
wp_get_attachment_image($key_val[$page],"cherry-thumb-a") ."</div>";
}
}
endwhile;
else :
_e('Sorry, no posts matched your criteria.');
endif;
so this code will show my image using
wp_get_attachment_image($key_val[$page],"cherry-thumb-a")
and will paginate with link
https://yourpage.com/post-name/2
but this code just show image 1 per page , my problem is how to show 4 image per page , and how to change link paginate to
https://yourpage.com/post-name/page/2
i will hapyy if anyone can help me
Update
thankyou for attention , i was try adding array_chunk , and adding pagination , but it still not work , my full code:
$paged = ( get_query_var( 'paged' ) ) ? absint( get_query_var( 'paged' ) ) : 1;
$args = array('posts_per_page' => 1, 'paged' => $paged );
$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, 4);
$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;
$big = 999999999;
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
) );
endif;
many thank for attention
use array_chunk()
if ( have_posts() ) :
while ( have_posts() ) : 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, 4);
$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;
endif;
Adding pagination for single post pages
Place this function in your functions.php
add_action('redirect_canonical', 'single_pagination_fix');
public static function single_pagination_fix( $redirect_url ) {
if ( is_paged() && is_singular() ) {
$redirect_url = false;
}
return $redirect_url;
}

Wordpress - List all posts by year with pagination

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; ?>

"Page not found" HTML title when I click the next link of wordpredd blog pagination

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 );

Wordpress custom wp_pagenavi does not work on custom page with custom query

I am trying to do some custom page and I am not using the wp_pagenavi plugin but a custom function with my custom pages, right now it only works on index.php pages but it was working fine some days ago before I've added more query elements.
//custom pagepavi function
function my_pagenavi( $the_query = false ){
global $wp_query;
$query = ($the_query) ? $the_query : $wp_query;
$max = $query->max_num_pages;
$current_page = max(1, get_query_var('paged'));
$big = 999999999;
if ( $max > 1 ) {
echo "<div class='pagination' style='height:auto'>";
echo paginate_links(array(
'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
'format' => '?paged=%#%',
'current' => $current_page,
'show_all' => false,
'total' => $max,
'type' => 'list',
'prev_text' => __('PREV','dnp_theme'),
'next_text' => __('NEXT','dnp_theme'),
));
echo "</div>";
}
}
Now here is the loop from my template page.
//some query stuff
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$query = 'offset=0&paged='.$paged;
$blogs = new WP_Query($query);
if ( $blogs->have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( $blogs->have_posts() ) : $blogs->the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php my_pagenavi( array('query' => $blogs) ); ?>
<?php endif; ?>
Why is not loading anything? What is going on??
The query is not correct that is why it's not working, at least it was not, not I have it working fine :)
instead of
$blogs = new WP_Query($query);
and all that should be
$blogs = query_posts( 'post_type=post&posts_per_page=4&paged='.get_query_var('paged') );
the loop is just like any other and works perfect
if ( have_posts() ) : ?>
<?php /* Start the Loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'content', get_post_format() ); ?>
<?php endwhile; ?>
<?php my_pagenavi(); ?>
<?php endif; ?>
after endwhile write this code
global $wp_query;
$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' => $wp_query->max_num_pages
) );

Resources