【问题标题】:Pulling featured image as a background on Wordpress在 Wordpress 上将特色图像拉为背景
【发布时间】:2014-11-16 04:02:31
【问题描述】:

我正在尝试弄清楚如何将精选帖子图片作为背景(在 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。

【问题讨论】:

  • 加载此页面时实际呈现的是什么?
  • 它显示了特色帖子(课程),但特色图片代码($thumbimg)是我添加的
  • 我的意思是当你加载页面时PHP实际生成的,显示一些代码

标签: php wordpress image background featured


【解决方案1】:

因为您输入的 ID 错误,所以它不起作用。缩略图的代码应该是这样的:

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

你做对了,除了你在一个对象 $lesson 上调用 $lesson-&gt;ID,它在它之后不存在于循环之外:)

编辑您的代码:

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

【讨论】:

  • 谢谢,太好了。只是我之前遇到的一些问题。在此处查看屏幕截图:postimg.org/image/sa0bre7kj 由于某种原因,PHP 结束标记无法按预期工作
  • 您在结束标记后缺少一个单引号 :) 检查我上面发布的代码,如果有帮助请接受答案
  • 我仍然收到该行的解析错误。看看这里(完整代码)pastebin.com/i8442QKj - 第 543 行
  • 将第 543 行替换为 $html .= '&lt;li class="lesson-items" style="background-image: url("' . echo $thumbimg[0]; . '")&gt;';
  • 这些错误很容易调试 .. 我无法跟踪您的代码可以使用的内容,尤其是当我没有有用的调试信息(如行号)时 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-04-10
  • 1970-01-01
  • 2015-07-26
  • 1970-01-01
  • 2013-01-31
  • 2015-09-28
  • 2015-04-11
相关资源
最近更新 更多