我正在使用lightgallery,因此我需要先加载图像,然后再在lightgallery中调用它们。问题是图像很大,因此加载时间太长。用户单击链接时,有什么方法可以加载该特定库。
<div id="lightgallery-<?php echo get_the_ID(); ?>" class="hidelightgallery">
<?php
foreach ($files as $image) {
$image_attributes = wp_get_attachment_url( $image->ID );
$attachment_title = get_the_title($image->ID);
$caption = get_post_field('post_excerpt', $image->ID);
?>
<a class="item" href="<?php echo $image_attributes ?>" data-sub-html="<?php echo $attachment_title; ?> <?php if($caption!= '') echo ' - ' ?> <?php echo $caption ?>"><img src="<?php echo $image_attributes ?>"></a>
<?php } ?>
</div>
现在我想要的是,如果用户单击具有此ID的示例链接,然后执行foreach。那可能吗?
最佳答案
只需按照以下步骤操作,
创建新的模板/ html页面,您将在其中编写html并通过foreach循环进行填充
将您的id = lightgallery整个代码添加到该html页面
当您单击您的链接(您提到的)时,触发ajax
Ajax函数将获取一些需要显示的ID或数量的图像,或者您如何在foreach循环中填充数据的逻辑
在php中,您将获取所有相关数据,并将该数据填充到您在步骤1中创建的html文件中
php函数会将数据返回给ajax函数
Ajax函数将获取所有动态html数据
在所需的位置填充该html,或在所需的位置附加该html
一步一步走,这将解决您的问题。
关于php - foreach如果用户单击链接?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42244409/