php - Wordpress - 在 PHP 的短代码中使用特色图像作为 div 背景

标签 php css wordpress shortcode

我试图在 wordpress 中获取帖子的特色图片并将其输出为 div 的背景图片并将其与 div 的右下角对齐。

到目前为止,我的代码如下所示,但是 background-image: url(' . wp_get_attachment_url() . '); 返回为 background-image: url(); 代码执行时。

提前感谢任何可以提供帮助的人!

<?php
    query_posts(array( 'category_name' => 'what-we-do'));
    if(have_posts()) : while(have_posts()) : the_post();
?>
<div>
    <?php 
        echo do_shortcode( '
            [expand title="' . get_the_title() . '" swaptitle="Close" trigpos="below" id="' . get_the_ID() . '" trigclass="arrowright" rel="whatwedo-highlander"]
                <h4 class="collapse-inner-title">' . get_the_title() . '</h4>
                <div class="whatwedo-collapse-background-img" style="background-image: url(' . wp_get_attachment_url() . '); background-position: right bottom;">
                    <div class="whatwedo-collapse-content">' . get_the_content() . '</div>
                </div>
            [/expand]
        ' ); 
    ?>
</div>

<?php
    endwhile;
    endif;
    wp_reset_query();
?>

最佳答案

试试这个:

$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'single-post-thumbnail' );

然后我们在$image[0]的时候设置background-image

关于php - Wordpress - 在 PHP 的短代码中使用特色图像作为 div 背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26036921/

相关文章:

css - WordPress 主题中的并排 div

php - Wordpress 组合查询

php - 使用哪个,concat,concat_ws?在 mysql 中

php - mysql中的1a 1b 1c级别描述符

html - 当屏幕分辨率大于 1600px 时,试图让网站缩放 120%

javascript - 覆盖 Instaemail 弹出窗口的 CSS

css - 为什么警告 "Selector specificity 0, 2, 0"?

php - Web本地应用程序Apache:运行Shell脚本

php - 类似wordpress updater的远程升级脚本

sql - 选择带别名的 SQL 查询并加入