如何将Wordpress循环输出包含在JS中?

时间:2016-01-19 22:12:42

标签: javascript php jquery wordpress

我有一个脚本在单击按钮时随机显示文本,如何用Wordpress get_post()循环替换手动列表?

jQuery(document).ready(function ($) {
    $("input[id='mantra']").keyup(function() {
        $("#preview").text($("input[id='mantra']").val());
    })

    // I wish to substitute these variables with a list of post titles.
    var randomStrings = [
     "I AM DIVINE PERFECTION",
     "LET GO. LET GOD.",
     "FOLLOW YOUR HEART",
    ];

    var randomDiv = document.getElementById("preview");
    var changer = document.getElementById("mantra");

    document.getElementById("myMantraButton").addEventListener("click", function(){
      randomIndex = Math.ceil((Math.random()*randomStrings.length-1));
      newText = randomStrings[randomIndex];
      randomDiv.innerHTML = newText;
      changer.value = newText;
    });
});

提前致谢。

0 个答案:

没有答案