我在网上搜索过,但是我看到的结果不够清楚。
我可以添加一个清单。通过将以下代码添加到functions.php文件到我的wordpress主题。
//manifest file
add_action( 'wp_head', 'inc_manifest_link' );
// Creates the link tag
function inc_manifest_link() {
echo '<link rel="manifest" href="'.get_template_directory_uri().'/manifest.json">';
}
清单。Json就是它应该的样子。
然而,我希望在manifest文件中使用一些theme_mod函数。我不能没有php。所以无论如何都要在清单中编写php代码。json文件。
提前感谢。
你可以这样做:
<?php
$filename = "manifest.json";
// Grab contents and decode them into an array
$data = json_decode(file_get_contents($filename), true);
//this is where you're adding your content so below you can write to the file
//Creating new var
//(make sure no var has the name "['newnewname']" unless you want to edit it)
$data['newname'] = 'Adding NEW contents';
//Creating new 2 dimensional array
$data['newname'] = array("another1" => "val1", "another2" => "val2");
//adding content to an existing 2 dimensional array
$data['exists'] += array("another1" => "val1", "another2" => "val2");
//adding content to an existing 3 dimensional array
$data['exists']['name'] += array("another1" => "val1", "another2" => "val2");
//adding content to an existing 4 dimensional array
$data['exists']['name']['name'] += array("another1" => "val1", "another2" => "val2");
// encode back into json
$jencode = json_encode($data);
// write over file with new contents
$fopen = fopen($filename, "w");
if(fwrite($fopen, $jencode)){
echo('Success!<hr>');
echo(file_get_contents($filename));
}
fclose($fopen);
下面是一个活生生的例子(删除了fopen/write/read的内容):
先进:http://sandbox.onlinephpfunctions.com/code/eacdd6b8254c2127521769461d5f6d9daeda224d
简单:http://sandbox.onlinephpfunctions.com/code/3589151881aac2e442738b381c05a37240081901