【问题标题】:How to get image source instead of image tag in wordpress如何在wordpress中获取图像源而不是图像标签
【发布时间】:2017-04-26 14:51:58
【问题描述】:

我是 wordpress 的新手,我正在尝试更改代码,以便我可以获取图像源,以便我可以在 css 中使用它作为背景,但它会生成图像

这是已经写在 funtions.php 中的代码

$string .= '<p class="recent-image"><a href="' . get_the_permalink() .'">' . get_the_post_thumbnail($post_id, array( 700, 500) ) .'</a></p></div>';

在查看了很多关于此的帖子后,我发现我只能通过以下代码获取图片网址

$string .= '<p class="recent-image"><a href="' . get_the_permalink() .'">' . wp_get_attachment_image_src() .'</a></p></div>';

在我的代码wp_get_attachment_image_src() 中更新此代码,但结果中没有生成任何内容。

有人可以建议我应该尝试什么吗?

【问题讨论】:

    标签: php html wordpress wordpress-theming


    【解决方案1】:
    <?php get_image_tag( $id, $alt, $title, $align, $size ); ?>
    

    【讨论】:

    • 一个图像附件的 标签,如果需要,可以缩小它。过滤器“get_image_tag_class”允许更改图像的类名,而不必在 HTML 内容上使用正则表达式。参数是:WordPress 将用于该类的内容、附件 ID、图像对齐值以及图像的大小。第二个过滤器“get_image_tag”包含 HTML 内容,然后可以通过插件进一步操作以更改所有属性值甚至 HTML 内容。
    • 虽然这段代码 sn-p 可以解决问题,但including an explanation 确实有助于提高帖子的质量。请记住,您是在为将来的读者回答问题,而这些人可能不知道您提出代码建议的原因。
    【解决方案2】:

    你可以使用get_the_post_thumbnail_url():

    $string .= '<p class="recent-image"><a href="' . get_the_permalink() .'">' . get_the_post_thumbnail_url($post_id, array( 700, 500) ) .'</a></p></div>';
    

    如果要使用wp_get_attachment_image_src(),还需要传递post id:

    wp_get_attachment_image_src($post_id)

    $string .= '<p class="recent-image"><a href="' . get_the_permalink() .'">' . wp_get_attachment_image_src($post_id) .'</a></p></div>';
    

    您可以阅读有关wp_get_attachment_image_src here 的文档,您可以在其中看到传递给此函数的第一个参数是id,它是必需的,而不是可选的,这就是为什么它不会像您使用它那样返回任何内容。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-06-14
      • 2017-01-22
      • 2012-03-02
      • 1970-01-01
      • 1970-01-01
      • 2016-10-14
      • 2012-10-20
      • 2022-08-17
      相关资源
      最近更新 更多