将附加图像显示为背景图像css

时间:2018-01-16 作者:Oyun Drop

我有个问题,我希望你们能帮我,因为我快疯了。无论如何,正如您所知,wordpress在标签中输出附加的图像。我需要他们作为背景图像css。这是我的代码;

        <?php
            $content = get_the_content();
            $feat_image_url = wp_get_attachment_url(  $post->ID, \'\' )[0];
            $content = preg_replace("/<img[^>]+\\>/i", "<div class=\'single-
            parallax-image\' style=\'background-image:url(".$feat_image_url.")\'></div>", $content);          
            $content = apply_filters(\'the_content\', $content);
            $content = str_replace(\']]>\', \']]>\', $content);
            echo $content; 
        ?>
如您所见,我需要将img标记替换为

<div class=\'single-parallax-image\' style=\'background-image:url(".$feat_image_url.")\'></div>
谢谢。。

1 个回复
SO网友:Toir

using this code

<?php if (has_post_thumbnail( $post->ID ) ): ?>
      <?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), \'full\' ); ?>
    <div class=\'single-parallax-image\' style=\'background-image:url("<?=$image[0]; ?>")\'><?=get_the_content()?></div>

      </div>
    <?php endif; ?>
结束

相关推荐

Get images by category

我目前获取滑块图像的方法是使用以下工作正常的代码:$args = array( \'post_type\' => \'attachment\', \'sort_order\' => \'ASC\', \'sort_column\' => \'menu_order\', ); $attachments = get_posts($args); if ($attachments) { $