首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在循环中每第n次分发画廊图像

在循环中每第n次分发画廊图像
EN

WordPress Development用户
提问于 2019-11-08 18:02:53
回答 2查看 96关注 0票数 0

我增加了一个acf图片库的存档条款,我想采取这些图像,并插入一个图片从画廊每3个帖子在这个术语存档。这是我到目前为止的情况,但我还没有让他们按顺序分发每3个帖子。

1-2-3张贴图片1 4-5-6张贴图片2 7-8-9张贴图片3

代码语言:javascript
复制
// get the current taxonomy term
$queried_object = get_queried_object(); 
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id; 
$images = get_field('term_gallery', $taxonomy .'_' . $term_id); //image ids
$size = 'full'; // (thumbnail, medium, large, full or custom size)

$counter = 1;

if ( have_posts() ) : 
while ( have_posts() ) : the_post(); 

get_template_part( 'template-parts/content', get_post_type() );
?>

与以上的整个画廊是显示每3个帖子。如图所示

1-2-3张贴图像1+图像2+图像3 4-5-6张贴图像1+图像2+图像3

UPDATE不知道我是怎么得到这个的,但是它部分地工作了,只是第一页之后的分页不起作用吗?没有任何图片在下一页显示,当我期望得到下3张图片的画廊。

代码语言:javascript
复制
// get the current taxonomy term
$queried_object = get_queried_object(); 
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id; 

$images = get_field('term_gallery', $taxonomy .'_' . $term_id);
$size = 'full'; // (thumbnail, medium, large, full or custom size)
$images_len = count($images); // max

$counter = 1;
$img_ct = 0;

if ( have_posts() ) : 
while ( have_posts() ) : the_post(); 

get_template_part( 'template-parts/content', get_post_type() );

?>
EN

回答 2

WordPress Development用户

回答已采纳

发布于 2019-11-11 11:42:26

我找到了一个类似的解决方案

代码语言:javascript
复制
// get the current taxonomy term
$queried_object = get_queried_object(); 
$taxonomy = $queried_object->taxonomy;
$term_id = $queried_object->term_id; 

$images = get_field('term_gallery', $taxonomy .'_' . $term_id);
$size = 'full'; // (thumbnail, medium, large, full or custom size)

$limit = 3; //how many gallery images per page

$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

$offset = ($paged - 1) * $limit;
$images = array_slice($images, $offset, $limit);

$counter = 1; //post counter
$img_ct = 0; //image counter

if ( have_posts() ) : 
while ( have_posts() ) : the_post(); 

get_template_part( 'template-parts/content', get_post_type() );

?>
票数 1
EN

WordPress Development用户

发布于 2019-11-11 01:36:09

正如我在评论中所指出的,例如,如果您每页显示9篇文章,这意味着每页将显示3幅图片库图像,并且希望第1页显示图像1-3,第2页显示图像4-6,等等,那么您需要对图片库图像进行分页:

代码语言:javascript
复制
// the number 3 below is the position where the image is to be added to
$paged = max( 1, get_query_var( 'paged' ) ); // get the current page number
$per_page = floor( max( 1, get_query_var( 'posts_per_page' ) ) / 3 );

$counter = 1;
$img_ct = ( $paged - 1 ) * $per_page; // start showing images of this index

这将取代这一部分:

代码语言:javascript
复制
$counter = 1;
$img_ct = 0;

另外,您应该使用if ( ! empty( $images ) ),而不是只使用if ( $images )

票数 1
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/352198

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档