set post per page using get_post_meta

2019-08-30 17:26发布

问题:

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

回答1:

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