使用 get_post_meta 设置每页的帖子

问题描述 投票:0回答:1

我有代码可以在每页显示

get_meta_post
这是我的代码

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;

所以这段代码将使用

显示我的图像

wp_get_attachment_image($key_val[$page],"cherry-thumb-a")

并将使用链接分页

https://yourpage.com/post-name/2

但是这段代码每页只显示 1 张图片,我的问题是如何每页显示 4 张图片,以及如何将链接分页更改为

https://yourpage.com/post-name/page/2

如果有人能帮助我,我会很高兴

更新

谢谢您的关注,我尝试添加

array_chunk
,并添加分页,但它仍然不起作用,我的完整代码:

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

非常感谢您的关注

php wordpress pagination post-meta
1个回答
1
投票

使用 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;

为单个帖子页面添加分页

将此函数放入您的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;
}
© www.soinside.com 2019 - 2024. All rights reserved.