我正在制作一个响应迅速的WordPress作品集网站(www.jasongilmour.co.uk),我对WP主题开发知之甚少,所以真的陷入了一些PHP。我希望通过为不同的设备加载不同的图像来使我的网站真正响应,但无法获得正确的代码...
我现在所拥有的是从maximage2幻灯片的帖子中获取全尺寸图像。
<div id="maximage">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$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 wp_get_attachment_image( $attachment->ID, 'full' );
}
}
endwhile; endif; ?>
</div>
我尝试将其嵌入<picture>
标记并仅回显src,但当我使用wp_get_attachment_image_src
时,它会返回HTML“ArrayArrayArrayArray
”。我也试过放echo wp_get_attachment_image( $attachment->ID, 'full', 'src'
,但这并没有改变返回的HTML。
我真的不太了解WP文档足以使它在...循环或数组中工作我认为这是被称为?所以这对我来说不太好。
这是我网站中非常重要的一部分,因为我使用大量图片来保持我的设计理念,并且在网站缓存之前使网站变得非常慢。
PS
尽管过去几周我学到了很多东西,但我仍然不明白我在用PHP做什么......我只是一名平面设计专业学生。
我也可以将主题设为开源,如果有人有兴趣获取副本,请告诉我。
我还需要在平板设备上对鼠标悬停菜单进行排序问题,然后才对它感到满意,但我也不明白我在做什么。
提前致谢!
答案 0 :(得分:1)
但是当我使用wp_get_attachment_image_src时,它会返回HTML
这是因为该函数根据the source返回一个数组,包含图像的URL,宽度和高度。
所以:
list($url, $width, $height) = wp_get_attachment_image_src($attachment->ID, 'full');
printf('<img src="%s" width="%d" heigt="%d"', $url, $width, $height);
// or vprintf and directly pass the function call...
答案 1 :(得分:0)
嗯,它不像我希望的那么干净,但它确实有效,现在这个网站的速度要快一百万倍。 Javascript只是在某些屏幕尺寸上注释掉了不需要的HTML,因此我可以获得所需的所有图像大小,这意味着PHP没有被更改。
<script>
document.write("<!--");
if (screen.width <= '400') {document.write(" good -->");}
</script>
<div id="maximage">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$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 wp_get_attachment_image( $attachment->ID, 'full' );
}
}
endwhile; endif; ?>
</div>
<script>
if (screen.width <= '400') {document.write("<!-- good ");}
</script>
-->
<script>
document.write("<!--");
if ((screen.width >= '401') && (screen.width <= '800')) {document.write(" good -->");}
</script>
<div id="maximage">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$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 wp_get_attachment_image( $attachment->ID, 'full' );
}
}
endwhile; endif; ?>
</div>
<script>
if ((screen.width >= '401') && (screen.width <= '800')) {document.write("<!-- good ");}
</script>
-->
<script>
document.write("<!--");
if ((screen.width >= '801') && (screen.width <= '1280')) {document.write(" good -->");}
</script>
<div id="maximage">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$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 wp_get_attachment_image( $attachment->ID, 'large' );
}
}
endwhile; endif; ?>
</div>
<script>
if ((screen.width >= '801') && (screen.width <= '1280')) {document.write("<!-- good ");}
</script>
-->
<script>
document.write("<!--");
if (screen.width >= '1281'){document.write(" good -->");}
</script>
<div id="maximage">
<?php if ( have_posts() ) : while ( have_posts() ) : the_post();
$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 wp_get_attachment_image( $attachment->ID, 'full' );
}
}
endwhile; endif; ?>
</div>
<script>
if (screen.width >= '1281') {document.write("<!-- good ");}
</script>
-->