在wordpress中链接php和javascript错误
我有一个名为custom.js的javascript(jquery)文件,我正在使用它:
(function ($) { $(document).ready(function () { $('.days').countdown({ until: directorDate, layout: '{dn} {dl}', timezone: +7 }); $('#weather').openWeather({ city: 'directorCity //The city is in a string.It used to be 'New York,US' placeTarget: '.weather-place', iconTarget: '.weather-icon', customIcons: 'dark/files/img/icons/weather/' }); }); })(jQuery);
在我的标题中,我正在使用:
var directorDate = new Date( ); var directorCity =
基本上发生的事情是标题从文件themeoptions.php获取数据(wordpress管理员中的一个表单供用户输入数据。我没有显示它,因为director_city和director_date的结构在那里是相同的)和然后custom.js从标题中获取数据。
现在倒计时运行但是城市没有显示。 为什么会这样?
你需要从php中回显值,而不是将它放在变量中。
您可能应该回显该值而不是将其分配给php变量,如下所示:
var directorCity =
编辑:我不完全确定你要完成的是什么,你也许可以尝试以下方法来解决问题:
如果您的网站不再有效,那么您的php可能存在错误。 请在此处发布您的代码。
这种事情最好用wp_localize_script来完成 ,但是如果你想直接输出它,试试这样的东西,认为它比其他答案更具可读性:
//use ternary operator to check, otherwise we want an empty string $director_date = get_option('director_date') ? get_option('director_date') : ''; $director_city = get_option('director_city') ? get_option('director_city') : ''; ?>
另外我不知道这里只是代码中的输入错误,或者实际上是在你的javascript中,但在你的openWeather
选项中你传递的是
city: 'directorCity'
而不是
city: directorCity