0

おすすめの投稿画像を背景として (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>';

                        }

style="background.." を取得して、必要に応じて php を読み取ることもできないようです。

4

1 に答える 1

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 ()

于 2014-09-22T09:05:50.720 に答える