【问题标题】:How create a JSON file and read it on Wordpress如何创建 JSON 文件并在 Wordpress 上阅读
【发布时间】:2019-08-25 09:33:48
【问题描述】:

我需要创建一个 JSON 文件以在其中保存一个列表。此代码必须进入 Wordpress functions.php 文件中。我该怎么做?

function ProgramarBorrado(){
        ?> 
        <script type="text/javascript">
            var day = (new Date).getDate(); //Get today
            var month = (new Date).getMonth()+1; //Get this month
            var year = (new Date).getFullYear(); //Get this year
            var lista = [];
            if ((day == 1) && (month == 4) && (year==2019)) {  //Push ID on a list
            lista.push('#borrar22marc');
            }
            if ((day == 6) && (month == 4) && (year==2019)) {
            lista.push('#borrar6abril');
            }
            if ((day == 5) && (month == 5) && (year==2019)) {
            lista.push('#borrar5maig');
            }
            if ((day == 19) && (month == 5) && (year==2019)) {
            lista.push('#borrar19maig');
            }
            if ((day == 8) && (month == 6) && (year==2019)) {
            lista.push('#borrar8juny');
            }
            if ((day == 10) && (month == 8) && (year==2019)) {
            lista.push('#borrar10agost');
            }

            lista.forEach(function(element) {
                  jQuery(element).addClass('borrar-programado');// For each element on the list add a class on the ID
                });

        </script>
    <?php
}
add_action('wp_footer', 'ProgramarBorrado');

代码必须与此类似。将变量“lista”保存在 JSON 文件中

【问题讨论】:

  • 没有更多关于数据来源等的信息,很难提出具体的建议 - 您可以查看stackoverflow.com/questions/1435072/…stackoverflow.com/questions/2467945/…
  • 我修改了帖子,谢谢!
  • JSON 数据是如何生成的? JSON 数据是否包含在静态 JSON 文件中?您的代码仅显示将一些 ID 推入数组的 Javascript 函数?您的目标是将这个数组导出到 JSON 文件然后重用吗?如是?您希望将其直接导入到 Wordpress 中吗?
  • 我的目标是创建一个 JSON 文件并在满足条件时添加 ID。然后读取该文件并使用 Javascript 应用 AddClass

标签: javascript php json wordpress


【解决方案1】:

如果数组是用javascript生成的,你可以使用JSON.stringify

var json = JSON.stringify(lista);

【讨论】:

    猜你喜欢
    • 2021-06-06
    • 2016-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-29
    • 1970-01-01
    • 2014-03-28
    相关资源
    最近更新 更多