css - 尽管链接正确,但显示的图像错误

标签 css wordpress wordpress-theming lightbox

这是我目前正在处理的一个非常奇怪的问题:我正在尝试在我的 wordpress 主题中实现灯箱效果。按照本教程 here (使用的 CSS 代码是这个:link)。我试了一下,效果很好。
但我不是要将效果应用到特色图片,而是要应用到帖子中的所有图片。我已经更改了代码以满足我的需要,如果我在帖子中只有一张图片,它就可以工作,但如果我添加另一张(或更多),那就是问题开始的时候:
当我点击一张图片时,无论是哪一张,它都只是第一张以大版本显示。但是查看源码的时候,每张图片的大图都调用了相关的图片。
这是我用于此灯箱效果的代码:

<?php
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID,
'post_mime_type'=> 'image',
'orderby'   => 'ID',
'order'     => 'ASC',
);
$attachments = get_posts( $args );
?>
<div class="nine columns">
<div class="about">
<h2><?php the_title(); ?></h2>
<?php  foreach ( $attachments as $attachment ) :
$attachments = get_posts( $args );
$small_image = wp_get_attachment_image_src($attachment->ID, 'thumbnail');
$large_image = wp_get_attachment_image_src($attachment->ID, 'full');
?>
<a href="#<?php echo $attachments ?>"><img src="<?php echo $small_image[0]; >" alt="small"></a> 
<a href="#" id="<?php echo $attachments ?>" class="pressbox"><img src="<?php echo $large_image[0]; ?>" alt="large"></a>
<?php endforeach ?>
</div>
</div> 

提前感谢您查看我的请求 亲切的问候 流浪者

最佳答案

你的代码存在三个问题:
1. 不应调用 $attachments = get_posts( $args );在foreach
2.小​​图链接href不正确
3.大图链接id不正确

试试这个:

<?php
$args = array(
'post_type' => 'attachment',
'numberposts' => -1,
'post_status' => null,
'post_parent' => $post->ID,
'post_mime_type'=> 'image',
'orderby'   => 'ID',
'order'     => 'ASC',
);
$attachments = get_posts( $args );
?>
<div class="nine columns">
<div class="about">
<h2><?php the_title(); ?></h2>
<?php  foreach ( $attachments as $attachment ) :

$small_image = wp_get_attachment_image_src($attachment->ID, 'thumbnail');
$large_image = wp_get_attachment_image_src($attachment->ID, 'full');
?>
<a href="#<?php echo $attachment->ID ?>"><img src="<?php echo $small_image[0]; ?>" alt="small"></a> 
<a href="#" id="<?php echo $attachment->ID ?>" class="pressbox"><img src="<?php echo $large_image[0]; ?>" alt="large"></a>
<?php endforeach ?>
</div>
</div>

关于css - 尽管链接正确,但显示的图像错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32257045/

相关文章:

jquery - 使用 Jquery 设置 Select 的样式

php - 在 WordPress 中将 HTML 语言从 en-US 更改为 en-GB?

css - 如何在 Wordpress 中编辑博客标题

php - substr() 无法在 wordpress 小部件中修剪 the_content()

php - WP 自定义帖子类型无法在上面的 woocommerce 3.0 中添加到购物车

javascript - 在 openshift 上托管时,WordPress(Redux 框架主题)中样式表和 Javascript 的路径无效

html - 反向排序列表的自定义编号

javascript - 使用 Animate.css 在 HTML 中制作多字动画

javascript - 通过 Javascript 在点击时更改 Accordion 颜色

wordpress - 如何在Wordpress中的front-page.php(主页)中显示最近的帖子?