我正在我正在编写的模板上查询图像附件
出于布局目的,我想对所有不是第一个的图像应用css类
这是我目前正在编写的代码,我一直在使用最后一条if语句。但是我的php知识很浅,希望有人能帮我写这篇文章。
<?php $args = array( \'post_type\' => \'attachment\',
\'numberposts\' => -1,
\'post_status\' => null,
\'post_parent\' => $post->ID );
$attachments = get_posts($args);
if ($attachments) {
foreach ( $attachments as $attachment ) {
echo the_attachment_link( $attachment->ID , \'full\' );
echo $img_count > 1 ? \'rel="\'.$post->ID.\'"\' : \'\';
// OK all my post attached images are visible.
}
}
//if image is not the first one then add class="fantome"
if ( $attachments != $first_attachment ) {
echo \'<a class="fantome">\';
}
?>
最合适的回答,由SO网友:Pradipta Sarkar 整理而成
我只是在修改你的代码,请检查一下。希望这会有所帮助。
<?php $args = array( \'post_type\' => \'attachment\',
\'numberposts\' => -1,
\'post_status\' => null,
\'post_parent\' => $post->ID,
\'order\' => \'ASC\' );
$i = 0;
$attachments = get_posts($args);
if ($attachments) {
foreach ( $attachments as $attachment ) {
if($i != 0){
$class = \'fantome\';
}else{ $class = \'\';}
$imgarr = wp_get_attachment_image_src($attachment->ID,\'full\');
/*---------------this will output your image and class------- */
echo \'<img class="\'.$class.\'" src="\'.$imgarr[0].\'" />\';
$i++;
}
}
?>