【问题标题】:PHP style="background-image(..." breaking my javascript in fun new waysPHP style="background-image(..." 以有趣的新方式破坏我的 javascript
【发布时间】:2017-01-26 22:13:19
【问题描述】:

噢噢噢男孩。所以我在这里有这个非常hacky的PHP,但我正在自学wordpress主题开发项目。

尝试集成this rad gradientMaps API,发现了一个奇怪的错误。当我将 js 函数应用于 PHP 构建的元素时,除非我从 html 元素的末尾删除 > 并允许它吞下下一个元素的开始标记(在这种情况下,“

我已经研究了好几个小时,是的,我尝试以新的方式重建我的 PHP,但这是它似乎为我呈现的唯一方式:

            <?php 
        $args = array( 'numberposts' => 6, 'post_status'=>"publish",'post_type'=>"post",'orderby'=>"post_date");

        $postslist = get_posts( $args );
        echo '<div class="recent-posts flexbox">';
         foreach ($postslist as $post) :  setup_postdata($post);
             echo "<div class=\"flex-section-3 rpost-thumb\" style=\"background-image: url(";
             the_post_thumbnail_url();
             // reference point here
             echo ")\">"; 
             ?>
        <h4 class="rpost-category">
         <?php es_entry_category(); ?>
        </h4>

就像我说的,hacky。

当我添加我的类以在元素上运行 gradientMap javascript 时,控制台中会显示以下内容:

    <div class="flex-section-3 random-grad rpost-thumb" style="background-image: url("http://localhost:8000/wp-content/uploads/2016/12/20161222_203726-1200x675.jpg"); filter: url("#filter-1485467730713");" <h4 data-gradientmap-filter="filter-1485467730713">
     Category <!-- this is the text result of es_entry_category() -->        
         <div class="rpost-meta">
           <div class="rpost-meta-wrapper">
           ...

据我所知,问题在于 js 正在添加更多样式属性,而我的 \" 就在我评论的参考点之后关闭 style="" 属性,然后所有渐变图的东西都添加到那里。但是我不能真正失去 style="background..." 业务而不失去动态背景 url 功能(php 函数 the_post_thumbnail_url)。我用谷歌搜索了我的脸,我无法弄清楚。

所以 A) 是否有一些我完全遗漏的明显解决方案? 和 B) 是否有其他 PHP 语法可以用来清理这个荒谬的回声和静音引号网络,并希望以这种方式解决我的问题?

这里是我的jsfiddle 了解更多信息。非常感谢我能在这方面获得任何帮助!

【问题讨论】:

  • 看起来&lt;div class="recent-posts flexbox"&gt; 在循环之后缺少一个结束&lt;/div&gt;。不确定这是否有助于解决问题。
  • 检查和双重检查,绝对不是这样。在 PHP 代码中有两个 div 会使小提琴读起来好像 div 开始和结束标记没有对齐,但它们确实对齐。不过感谢您的支持!

标签: javascript php wordpress-theming gradient


【解决方案1】:
 <?php 
        $args = array( 'numberposts' => 6, 'post_status'=>"publish",'post_type'=>"post",'orderby'=>"post_date");

        $postslist = get_posts( $args );
        echo '<div class="recent-posts flexbox">';
         foreach ($postslist as $post) :  setup_postdata($post);
             echo '<div class="flex-section-3 rpost-thumb" style="background-image: url("';
             echo the_post_thumbnail_url();
             // reference point here
             echo '")>'; 
             ?>
        <h4 class="rpost-category">
         <?php es_entry_category(); ?>
        </h4>

这可能会解决问题,使用'',也不在the_post_thumbnail_url()中调用echo;可能会使值不打印在 html 字符串中

【讨论】:

  • 这感觉就像密歇根青蛙——它破坏了 PHP 现在它不是(诚然,这可能是由于我的语法错误)。尽管如此,问题仍然存在。除非我从 div 中取出最后的 >,否则 js 会以某种方式中断。
  • 对不起,这可能是我在代码中的错误 echo '")'>";而不是 echo '")>';,现在试试看
  • 实际上我自己实现了它 - 您的两个示例都缺少“关闭 style="background-image:url()" 属性。:) 可以肯定的是,我确实尝试了所有三个版本. 这是有效的: foreach ($postslist as $post) : setup_postdata($post); echo '
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-24
  • 1970-01-01
  • 2011-05-04
相关资源
最近更新 更多