URL引用图片的动态变化

发布于 2025-01-14 09:54:03 字数 393 浏览 1 评论 0原文

我正在使用 Elementor 在 Wordpress 上设计一个网页,目前该页面的正文中有一个图像。

该图像使用“从 URL 插入”选项,而不是从媒体库中提取。我希望页面上有这样的功能,用户可以单击按钮,通过将 php 字符串传递到 URL 来切换显示的图像,这在理想情况下会更改图片的 URL。

目标是减少 Web 服务器本身的压力。图片 URL 结构是一致的,如下所示:

www.example.com/graphics/someregion_somevariable.png

是否可以利用 CSS 代码(在 Elementor 图片小部件内部)直接更改传递到网站 URL 的 php 字符串中引用图片的 URL?需要利用哪些 CSS 和 PHP 代码来执行此操作?谢谢你!

I am utilizing Elementor to design a webpage on Wordpress and currently have an image in the body of this page.

This image utilizes the "Insert from URL" option as opposed to pulling from the media library. I would like functionality on the page where a user can click buttons to switch which image is displayed by passing a php string into the URL, which would ideally change the URL of the picture.

The goal is to reduce strain on the web server itself. The picture URL structure is consistent, and is as follows:

www.example.com/graphics/someregion_somevariable.png

It is possible to leverage CSS code (inside the Elementor picture widget) to directly change the URL referenced picture off a php string passed into the website URL? What CSS and PHP code would need to be leveraged to execute this? Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情释 2025-01-21 09:54:03

JavaScript 解决方案

使用 URL 参数指示要显示的图片:

  • https://example.com/page?picture=picture_1
  • https://example.com/page?picture=picture_2 code>
  • https://example.com/page?picture=picture_3

您可以根据此 URL 参数有条件地加载特定图像:

const urlParams = new URLSearchParams(window.location.search);
const pictureParam = urlParams.get('picture')

const pictureUrl = switch (pictureParam) {
  case 'picture_1':
    return 'www.example.com/graphics/picture_1.png'
    break

  case 'picture_2':
    return 'www.example.com/graphics/another-picture.png'
    break

  case 'picture_3':
    return 'www.example.com/graphics/picture_x.png'
    break

  default:
    return 'www.example.com/graphics/fallback.png'
    break
}

现在您可以在页面上选择图像元素并将 src 更改为所需图片。

document.querySelector('#the-image-element').src = pictureUrl

JavaScript Solution

Using URL params to indicate which picture to display:

  • https://example.com/page?picture=picture_1
  • https://example.com/page?picture=picture_2
  • https://example.com/page?picture=picture_3

You can conditionally load a specific image based on this URL Parameter:

const urlParams = new URLSearchParams(window.location.search);
const pictureParam = urlParams.get('picture')

const pictureUrl = switch (pictureParam) {
  case 'picture_1':
    return 'www.example.com/graphics/picture_1.png'
    break

  case 'picture_2':
    return 'www.example.com/graphics/another-picture.png'
    break

  case 'picture_3':
    return 'www.example.com/graphics/picture_x.png'
    break

  default:
    return 'www.example.com/graphics/fallback.png'
    break
}

Now you can select your image element on page and change the src to the required picture.

document.querySelector('#the-image-element').src = pictureUrl
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文