【问题标题】:WordPress: CSS property getting cancelled out?WordPress:CSS 属性被取消了?
【发布时间】:2017-09-27 17:02:26
【问题描述】:

在我的 page.php 中,我试图在 .page-wrapper div 中输出特色图像,并在页面顶部使用 style="background-image: url('<?php echo $thumb['0'];?>') !important;" 显示(内联 css)

问题是它不断被浏览器划掉。可能是因为有一个 3rd 方插件并且可能会覆盖它 - 所以我尝试将 !important 标签添加到我的属性中。不工作。而且我禁用了插件,没有运气。想法??

页面链接:http://radian3.com/events/

这是page.php

<div class="about-container">
<!--  The POST LOOP -->
    <?php  if(have_posts()) :
              while (have_posts()) : the_post(); ?>
                  <?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), 'full' );?>

                  <div class="page-wrapper" style="background-image: url('<?php echo $thumb['0'];?>') !important;">

                 <b class="about-title"><?php the_title(); ?></b>
                 <!-- <div class="about-icon-wrapper" style="background-image: url('<?php echo $thumb['0'];?>')"></div> -->
                 </div>
                 <div class="contact-body-wrapper">
                 <?php the_content(); ?>    
                 </div> 
                    <?php endwhile; ?>

                        <?php else : 
                            echo '<p>No content found..</p>';

                        endif;         
                    ?>

CSS

/* single.php  page */
.page-wrapper {
    text-align: center;
    display: flex;
    justify-content: center;
    flex-direction: column;
    align-items: center;
/*  background-image: url('../img/about-our-team.jpg') !important;
*/  height: 30rem;
    background-repeat: no-repeat;
    background-size: cover;
}

【问题讨论】:

    标签: php jquery html css wordpress


    【解决方案1】:

    实际上,它在 page.php 中的代码正在执行覆盖。

    您看到的图像被划掉了包含在您的 css 文件中的 id,带有 emply url 的图像是由您的代码生成的。

    您的代码没有返回任何图像,因此您需要解决这个问题。除非您明确定义了 $post,否则它不会在此处具有值,因此 $post->ID 将为空。 您应该在循环内use get_the_ID(),即

    <?php 
    $thumb = wp_get_attachment_image_src( get_post_thumbnail_id(get_the_ID()), 'full' );
    if ($thumb){
    ?>
          <div class="page-wrapper" style="background-image: url('<?php echo $thumb['0'];?>')">
    <?php 
    // rest of your code....
    }
    

    仅供参考,您还应该检查以确保 $thumb 具有值 - 如果没有,您可以改用默认占位符

    【讨论】:

    • 再次感谢您的回答!顺便说一句,你建议的themesharper 教程很棒,通过它!
    • 出于好奇,您是 WordPress 开发人员吗?您是否也在其上开发电子商务主题,如果是,您是否也使用 WooCommerce 框架?有点随机:)
    • 嗯,我的背景是软件工程,但这些天我在做一些 Web 开发和 Wordpress 开发。虽然没有 Woocommerce。但是我有一个客户可能很快就会需要它,所以谁知道呢 - 我可能会在这些日子里向你征求意见 :) 而且我发现该教程也很有帮助 - 它是让我在 WP 中启动并运行的那个来自标准的 HTML 和 PHP 网站(就像你自己一样!)
    【解决方案2】:

    您需要删除不设置图像的内联样式属性,但它会覆盖 page-wrapper 的背景图像 url 的 css

    您需要从page-wrapper div 中删除以下属性。

    style="background-image: url('') !important;"
    

    【讨论】:

      【解决方案3】:

      请这样给予

      background-image: url("&lt;?php bloginfo('template_directory'); ?&gt;/images/parallax_image.jpg ");

      【讨论】:

      • 那行不通。 thumb('template_directory') 应该是什么?
      • bloginfo('template_directory') 基本上给出了活动主题的目录。
      • 是的,但是什么是“拇指”?
      • 它也是对图像进行硬编码 - OP 想要使用 $thumb 中返回的图像,所以如果你建议他们使用template_directory,你如何建议他们从帖子中获取图像?
      猜你喜欢
      • 1970-01-01
      • 2019-04-17
      • 2014-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多