【问题标题】:Wrong image displayed, despite the correct link尽管链接正确,但显示的图像错误
【发布时间】:2015-11-22 06:55:37
【问题描述】:

这是我目前正在处理的一个非常奇怪的问题:我正在尝试在我的 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> 

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

【问题讨论】:

  • 你在编辑什么文件?能否提供您页面的链接?

标签: css wordpress wordpress-theming lightbox


【解决方案1】:

您的代码存在三个问题:
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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-06
    • 1970-01-01
    • 1970-01-01
    • 2012-05-16
    • 2015-07-24
    • 1970-01-01
    相关资源
    最近更新 更多