简体   繁体   English

PHP Wordpres 页面中的动态内容

[英]Dynamic content in PHP Wordpres page

I have a ecommerce store website running with Wordpress.我有一个使用 Wordpress 运行的电子商务商店网站。 I'd like to include a section with a -random custormer's product review, so that every time someone access the page, there will be a different comment there.我想包含一个带有 -random 客户产品评论的部分,这样每次有人访问该页面时,都会有不同的评论。 I'm not used to PHP, but I managed to create a shortcode which takes a random comment and returns the proper HTML.我不习惯 PHP,但我设法创建了一个短代码,它接受随机注释并返回正确的 HTML。 It is working fine (in eddition mode, every time I insert the shortcode a different comment appears).它工作正常(在编辑模式下,每次我插入简码都会出现不同的评论)。 My issue is that when I leave the page and return, the previous one is still there.我的问题是,当我离开页面并返回时,前一个页面仍然存在。 I believe that it is being caused by cache, but I wouldn't like to disable the cache for the whole page.我相信它是由缓存引起的,但我不想禁用整个页面的缓存。 How you I force the shortcode run again (I don't know if it is the right way to explain) and make sure that at every access, a different comment appears?我如何强制短代码再次运行(我不知道这是否是正确的解释方式)并确保在每次访问时都会出现不同的评论? One solution I thought is to have JS code which would do preaty much the same thing my PHP code does, using Woocommerce API to get the data.我认为的一种解决方案是让 JS 代码与我的 PHP 代码做的事情大致相同,使用 Woocommerce API 来获取数据。 But I'm wondering if there is a simpler solution to do that, like forcing the specific section not being cached or re-run the shortcode.但我想知道是否有更简单的解决方案可以做到这一点,比如强制不缓存特定部分或重新运行短代码。

Thanks!谢谢!

JS can't do what PHP does here: at most it can create an AJAX-call to the backend that then runs a query for a random comment and returns it. JS 不能做 PHP 在这里所做的事情:最多它可以创建一个对后端的 AJAX 调用,然后运行一个随机评论的查询并返回它。 You need to render it thereafter.之后您需要渲染它。 It's fairly standard, but overkill for your case.这是相当标准的,但对你的情况来说太过分了。

Instead, you're going to want to check whether your caching mechanism supports ESI or something else that excluded parts of your code from being cached.相反,您将要检查您的缓存机制是否支持 ESI 或其他排除部分代码被缓存的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM