[英]How to add shortcode in wordpress using Javascript?
我想使用 JavaScript 在 WordPress 网站中放置一个简码。
网站结构是
<div class="shop-container">
<div class="wrap">[shortcode here]</div>
<div class="wc-pr"> </div>
</div>
首先,我使用以下代码将 div 元素放在 wc-pr 之上。
<script>
const parent5 = document.querySelector('.shop-container');
const billingField1 = document.querySelector('.wc-pr');
const newDiv = document.createElement('div');
newDiv.setAttribute('id', 'wrap');
parent5.insertBefore(newDiv, billingField1);
</script>
然后我尝试了下面的代码。
newDiv.innerHTML = `<?php do_shortcode("[shortcode here]");?>`;
newDiv.innerText = `<?php do_shortcode("[shortcode here]");?>`;
但没有工作。 innertext 显示为原始格式,Hhtml 未显示。
请让我知道我该如何解决这个问题
谢谢你。
您正在创建/生成一个 HTML,因此您必须将其回显到脚本中。 所以只需将 echo 添加到 PHP
newDiv.innerHTML = '<?php echo do_shortcode("[shortcode here]");?>';
代替
newDiv.innerHTML = '<?php do_shortcode("[shortcode here]");?>';
应该这样做。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.