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

时间:2014-09-22 08:40:24

标签: php wordpress image background featured

我试图找出如何将特色帖子图片作为背景(在Wordpress上),但无法让它工作。 请参阅下面的代码我正在使用。 $ thumbimg应该拉动特色图像,但显然我在那里做错了。

            $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>';

                        }

我似乎也无法得到风格=&#34;背景......&#34;像我想的那样阅读php。

1 个答案:

答案 0 :(得分:0)

它不起作用,因为您的ID错误。缩略图的代码应该是:

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

你正在做正确的事情,除非你在一个对象$lesson->ID上调用$lesson并且在它之后不存在于循环之外:)

编辑代码:

        $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>';

                    }

至于CSS中的背景,请使用background-image: url ()

相关问题