【问题标题】:How can I get only the image src in a wordpress loop如何在 wordpress 循环中仅获取图像 src
【发布时间】:2013-01-24 22:43:03
【问题描述】:

我正在制作一个响应式 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>

我尝试将它嵌入到 &lt;picture&gt; 标记中并仅回显 src,但是当我使用 wp_get_attachment_image_src 时,它返回 HTML“ArrayArrayArrayArray”。我也尝试输入echo wp_get_attachment_image( $attachment-&gt;ID, 'full', 'src',但这并没有改变返回的 HTML。

我不太了解 WP 文档,无法使其在...循环或数组中工作我认为这被称为?所以这对我来说不是很好。

这是我网站的一个非常重要的部分,因为我使用大量图片来保持我的设计理念,这会使网站在缓存之前变得非常缓慢。

PS

虽然在过去的几周里我学到了很多东西,但我仍然不明白我在用 PHP 做什么......我只是一个平面设计专业的学生。

我也可能将主题开源,所以如果有人有兴趣获得副本,请告诉我。

我还需要对平板设备上的鼠标悬停菜单问题进行排序,然后才能满意,但我也不明白我在做什么。

提前致谢!

【问题讨论】:

标签: php image wordpress loops responsive-design


【解决方案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...

【讨论】:

  • 非常感谢您的回复!我将其粘贴在而不是'echo wp_get_attachment_image($attachment->ID,'full');'行,但它不会循环遍历帖子中的每个图像,我无法让它获得其他图像尺寸。那是我得到的最接近的,我尝试了很多其他的东西,但它通常只是破坏了我的 php.ini 文件。真的很抱歉,我真的不太了解我在做什么。
  • 您正在查询$post-&gt;ID 的附件您确定这篇文章有预期的附件数量吗? count($attachments) 有什么建议?
  • 是的,肯定的,count($attachments) 也给了我我所期望的。我只设法获得了一张图片和所有帖子的 src。
  • 这里是主题 zip 文件的链接,它在 single.php 文件中。 link
  • 由于某种原因仍然没有循环:/
【解决方案2】:

嗯,它不像我希望的那样干净,但它确实有效,该网站现在快了一百万倍。 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>
-->

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-21
    • 2015-06-11
    • 1970-01-01
    • 2019-05-14
    • 1970-01-01
    • 2018-06-06
    • 1970-01-01
    相关资源
    最近更新 更多