【问题标题】:PHP Template - Overwrite background-image location with PHP ShortcodePHP 模板 - 使用 PHP 简码覆盖背景图像位置
【发布时间】:2016-12-15 17:19:13
【问题描述】:

我的目的是构建一个 wordpress 模板文件,其中使用出现在帖子中的短代码用 php 覆盖特定图像。

场景: 1)我使用sliderrevolution插件并将特定滑块的背景图像定义为placeholder.jpg 2)我将简码放入我的页面中,当找到特定文件时,在这种情况下:placeholder.jpg 将被替换为:post-one.jpg。

我的意图是在不同的帖子中使用相同的滑块,并用简码替换所有内容。

这对文本很有效,但我没有找到动态更改图片的方法。当然图片的大小和我要替换的一样。

希望有人可以帮助我,在此先感谢。

我尝试了下面的代码,但我没有让它工作。这是我要“更改”的 HTML 代码。

编辑:

感谢您的快速回答;)我尝试使用您的代码,但我没有让它工作:(

这是我要替换的图像的 HTML 代码:

<div class="tp-bgimg defaultimg" style="width: 100%; height: 100%; opacity: 1; visibility: inherit; z-index: 20; background-image: url(&quot;http://localhost/wp-content/uploads/revslider/fashion/fashion_bg1.jpg&quot;); background-color: rgba(0, 0, 0, 0); background-size: cover; background-position: center center; background-repeat: no-repeat;" src="http://localhost/wp-content/uploads/revslider/fashion/fashion_bg1.jpg"></div></code>

我尝试了多个代码,因为它没有改变任何东西:

<script>
$("tp-bgimg defaultimg").attr("src", "http://localhost/wp-content/uploads/2016/08/logo.png");
$("tp-bgimg").attr("src", "http://localhost/wp-content/uploads/2016/08/logo.png");
$("defaultimg").attr("src", "http://localhost/wp-content/uploads/2016/08/logo.png");

$("#tp-bgimg defaultimg").attr("src", "http://localhost/wp-content/uploads/2016/08/logo.png");
$("#tp-bgimg").attr("src", "http://localhost/wp-content/uploads/2016/08/logo.png");
$("#defaultimg").attr("src", "http://localhost/wp-content/uploads/2016/08/logo.png");

$(".tp-bgimg defaultimg").attr("src", "http://localhost/wp-content/uploads/2016/08/logo.png");
$(".tp-bgimg").attr("src", "http://localhost/wp-content/uploads/2016/08/logo.png");
$(".defaultimg").attr("src", "http://localhost/wp-content/uploads/2016/08/logo.png");

</script>

你知道我做错了什么吗?该脚本位于标题中并且是可调用的(使用警报进行测试)。

【问题讨论】:

    标签: php wordpress image dynamic replace


    【解决方案1】:

    您可能想使用 jQuery 动态隐藏现有元素,然后显示其他内容来代替它。

    一个很好的例子是:

    $("#img1").attr("src", "path/to/new_img.jpg");
    

    【讨论】:

      猜你喜欢
      • 2014-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-14
      • 1970-01-01
      • 2020-07-12
      • 1970-01-01
      相关资源
      最近更新 更多