【发布时间】:2016-07-01 05:17:16
【问题描述】:
我很难放弃使用CMB2 file_list 字段我的画廊,但它没有选项(内置)分页(如 ACF)并且我的 php 技能缺乏。我可以用 jQuery 做到这一点,但 我想要真正的页面。
这已经接近我的需要,它是从另一个问题拼凑而成的。它从 $files (get_post_meta) 中拆分出 5 个结果并创建页面,但图像在所有页面上都是相同的图像。我已经超出了我大脑的极限。
$attachment_id => $attachment_url 是从媒体库中选择的单个图像。
$files as $attachment_id => $attachment_url
这是我目前所拥有的(你可能想放弃它以支持更好的东西):
function gallery_loop() {
if( get_query_var('page') ) {
$page = get_query_var( 'page' );
} else {
$page = 1;
}
$img_size = 'portfolio-catalog';
$files = get_post_meta(get_the_ID(), '_cmb_gallery_images', true);
$limit = 5;
$total = count( $files );
$pages = ceil( $total / $limit );
$curr_page = isset($_GET['page']);
$offset = ($curr_page - 1) * $limit;
$items_array = array_chunk((array) $files, $limit, true);
$files_array = array_slice($items_array, $offset, true); // this is showing the same 5 items on all the pages
foreach ($files_array as $files) {
echo '<div style="border:1px solid red;">'; //BEGIN FAKE "page" so I can see if they are splitting correctly
foreach ($files as $attachment_id => $attachment_url) {
$page=1;
echo '<div class="file-list-image">';
echo wp_get_attachment_image($attachment_id, $img_size);
echo '</div>';
$page++;
} // end $files as $attachment_id => $attachment_url
echo '</div>'; //END "page" so I can see if they are splitting correctly
} // end foreach $files_array as $files
//the correct amount of pages are showing up but the items are all the same
echo paginate_links( array(
'base' => get_permalink() . '%#%' . '/',
'format' => '?page=%#%',
'current' => $page,
'total' => $pages
) );
}
// end function
回答 cmets 中的问题:
这是一个名为gallery-page.php 的页面模板。该页面有一个名为 file_list 的 CMB2 字段类型,这是一个附加图像的地方(它们不是附加到页面,而是附加到该字段,因此您也可以抓取任何内容并上传到它)。
当我从$files = get_post_meta(get_the_ID() , '_cmb_gallery_images', true); 执行 print_r 时,我得到:
Array( [956] => http://mydevserver.dev/wp-content/uploads/2016/02/bamboo-logo.jpg [960] => http://mydevserver.dev/wp-content/uploads/2016/02/tampa_guitar_logo.jpg [958] => http://mydevserver.dev/wp-content/uploads/2016/02/CNG-refueling.jpg [974] =>
等等。
【问题讨论】:
-
这是单页吗
-
因为你已经从 get_post_meta(get_the_ID(), '_cmb_gallery_images', true);因此,要么所有需要使用的图像都在一个页面中,要么您为此创建了图库帖子类型。请澄清?
-
那么你是如何循环函数的?假设你想要这个单页,你打电话给
get_post_meta(get_the_ID(), '_cmb_gallery_images', true),它只是获取该页面的帖子元数据。当您在定义变量的正下方执行print_r($files);时,您会得到什么? -
@dingo_d - 我得到了图片的所有网址(这是它的样子,但还有更多): Array( [956] => christinacreativedesign.dev/wp-content/uploads/2016/02/… [960] => christinacreativedesign.dev/wp-content/uploads/2016/02/… [ 958] => christinacreativedesign.dev/wp-content/uploads/2016/02/… [974] =>
-
@PieterGoosen 这是 WordPress 中的一个页面模板,它有一个 CMB2 file_list 类型字段,我可以将媒体库中的任何图像附加到该字段(并上传),然后您可以制作一个图像列表在页面上。
标签: php wordpress serialization foreach meta-boxes