duanniu3385 2015-12-07 15:15
浏览 35
已采纳

插件内的Wordpress Shortcode

Here is a snippet from the shortcodes.php file in one of the plugins I'm writing.

if($home_loop->have_posts()) {
        switch($display) {      

        case "static":

                while ($home_loop->have_posts()): $home_loop->the_post();

                        if(has_post_thumbnail() ) { 
                            $content .= '<div class="parallax">';
                            $content .= get_the_post_thumbnail($post->ID, 'wrapper-wide', array('class' => "img-responsive")); 
                            $content .= '</div>';
                            $content .= '<div class="container">';
                            $content .= get_the_content();
                            $content .= '</div>';

                            $content .= '<style>';
                            $content .= '.parallax{//css style here}';

                            $content .= '</style>;



                        } else {
                            //Something
                        }

                endwhile;

                break;

This works perfect. If I put a featured image thumbnail in my home post it will be displayed in my parallax div. The question I have is instead of placing the thumbnail inside of the div, I want to have it background-image: url(). I found this code below

<?php
$background = get_field( 'background_image' );

if($background):
?>
<style>
.main-wrapper {
 background-image: url(<?php echo $background ?>);
}
</style>
<?php endif; ?>

How would I incorporate and run something like this inside of the $content? The reason I'm trying to get the image as a background-image:url() is so I can add specific css styling like background-position, size, repeat, attachment.

  • 写回答

1条回答 默认 最新

  • doucuyu2259 2015-12-07 15:32
    关注

    Why not just add the image as a background-image right inside your code? [wp_get_attachment_image_src][1] will return an array of the url, width and height of any attachment. Pass it the ID of the featured image, and gain the flexibility to use ANY size you want, not just the actual featured image size. Then place it as the background using inline CSS. It can be overwritten in your CSS sheet if needed.

    ($home_loop->have_posts()) {
            switch($display) {      
    
            case "static":
    
                    while ($home_loop->have_posts()): $home_loop->the_post();
    
                            if(has_post_thumbnail() ) {
                                $post_thumnail_info = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'wrapper-wide' );
                                $content .= '<div class="parallax" style="background-image: url( \'' . $post_thumbnail_info[0] . '\' );">';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用