Wordpress:使用 Javascript 输出短代码? [英] Wordpress: Outputting shortcode with Javascript?

查看:27
本文介绍了Wordpress:使用 Javascript 输出短代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<script language="javascript">
if (country_code==’US’)
{
document.write("[adrotate banner="1"]");
}
else
{
document.write("[adrotate banner="2"]");
}
</script>

[adrotate banner="x"] 是用于 Adrotate 插件的 wordpress 短代码

[adrotate banner="x"] are wordpress shortcodes used for the Adrotate plugin

此脚本不起作用,因为无法以某种方式输出 [adrotate banner="x"]?它使用全局保存的变量 country_code.

This script doesen't work because somehow outputting [adrotate banner="x"] is not possible? It uses the globally saved variable country_code.

我尝试过 "'(内部使用其中一个,外部使用另一个),但没有运气.

I've tried with both " and ' (and the inner using one of them and the outer the other), but with no luck.

有什么办法可以解决这个问题并且仍然使用 JS?

Any way to work around this and still use JS?

推荐答案

Shortcodes 仅适用于服务器端.如果你想解决这个问题,你可以使用 AJAX 提交值,在你的 PHP 中调用 do_shortcode() ,将结果作为 JSON 返回,然后写入页面.

Shortcodes only work server side. If you want to work around it, you can submit the value using AJAX, call do_shortcode() on it in your PHP, return the results as JSON, and then write to the page.

http://codex.wordpress.org/Function_Reference/do_shortcode

作为旁注,您的示例有一个语法错误,因为您的字符串中有双引号,因为它不会作为短代码处理.您需要对其进行转义或使用单引号.

As a side note, your example has a syntax error because you have double quotes inside your string, since it won't process as a shortcode. You either need to escape it or use single quotes.

这篇关于Wordpress:使用 Javascript 输出短代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆