doutanggun9816 2014-09-22 08:40
浏览 33
已采纳

在Wordpress上拉特色图像作为背景

I'm trying to figure out how to pull the featured post image as a background (on Wordpress) but cannot get it to work. See below the code I'm using. $thumbimg should be pulling the featured image but obviously i am doing something wrong there.

            $lessons = get_posts( $args );

                        if( count( $lessons ) > 0 ) {

                            $html .= '<section class="module-lessons">';

                                $html .= '<header><h3>' . __( 'Lessons', 'sensei_modules' ) . '</h3></header>';
                                 $thumbimg = wp_get_attachment_image_src( get_post_thumbnail_id($lesson->ID), array( 200,200 ), false, '' );

                                $html .= '<ul>';

                                    foreach( $lessons as $lesson ) {
                                        $html .= '<li class="lesson-items" style="background: url(<?php echo $thumbimg[0]; ?>)>';
                                        $html .= '<a href="' . esc_url( get_permalink( intval( $lesson->ID ) ) ) . '" title="' . esc_attr( get_the_title( intval( $lesson->ID ) ) ) . '">' . get_the_title( intval( $lesson->ID ) ) . '</a>';

                                        $html .= '</li>';                                           

                                        // Build array of displayed lesson for exclusion later
                                        $displayed_lessons[] = $lesson->ID;
                                    }

                                $html .= '</ul>';

                            $html .= '</section>';

                        }

I cannot also seem to get the style="background.." to read the php as I would like to.

  • 写回答

1条回答 默认 最新

  • dqch34769 2014-09-22 09:05
    关注

    It will not work because you are getting the ID wrong. The code for the thumbnail should be something like:

    wp_get_attachment_image_src( get_post_thumbnail_id($post_ID), 'large' ) ;
    

    you are doing it right except for the fact that you are calling the $lesson->ID on an object $lesson that doesn't exist outside of the loop after it :)

    Editing your code:

            $lessons = get_posts( $args );
    
                        if( count( $lessons ) > 0 ) {
    
                            $html .= '<section class="module-lessons">';
    
                                $html .= '<header><h3>' . __( 'Lessons', 'sensei_modules' ) . '</h3></header>';
    
                                $html .= '<ul>';
    
                                    foreach( $lessons as $lesson ) {
                                        // This should be inside the loop
                                        $thumbimg = wp_get_attachment_image_src( get_post_thumbnail_id($lesson->ID), array( 200,200 ), false, '' );
                                        $html .= '<li class="lesson-items" style="background-image: url("'. $thumbimg[0] . '")>';
                                        $html .= '<a href="' . esc_url( get_permalink( intval( $lesson->ID ) ) ) . '" title="' . esc_attr( get_the_title( intval( $lesson->ID ) ) ) . '">' . get_the_title( intval( $lesson->ID ) ) . '</a>';
    
                                        $html .= '</li>';                                           
    
                                        // Build array of displayed lesson for exclusion later
                                        $displayed_lessons[] = $lesson->ID;
                                    }
    
                                $html .= '</ul>';
    
                            $html .= '</section>';
    
                        }
    

    As for the background in CSS, use background-image: url ()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀