我正在使用wp_get_attachemnt_image获取图像,然后我使用 jQuery 检查它是纵向还是横向以添加相应的类......这种工作但效果不佳,有时它不适用于某些图像。
有没有办法在调用图像之后但在输出之前添加类?
我正在使用此代码来获取图像:
<?php $args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'posts_per_page' => -1,
'orderby'=> 'menu_order',
'order'=>'ASC',
'post_parent' => $post->ID,
);
$images = get_posts( $args );
foreach($images as $image):
echo wp_get_attachment_image($image->ID, 'large');
endforeach;
?>
和这段代码添加类:
<script>
jQuery(document).ready(function($){
$('#journal img').each(function(){
$(this).addClass(this.width > this.height ? 'landscape' : 'portrait');
});
});
</script>
我也尝试在回显wp_get_attachment_image($image->ID,'large')之前回显脚本;但这似乎没有区别。有什么建议吗?
您最初可能会生成 HTML 代码,每个涉及的图像都具有用于display: none;
的类(或直接style
)。
然后更改您的JS部分,以便在设置所需的类时显示图像:
$(this).addClass(this.width > this.height ? 'landscape' : 'portrait').show();