在wordpress中链接php和javascript错误 [英] Linking php and javascript error in wordpress

查看:47
本文介绍了在wordpress中链接php和javascript错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为custom.js的javascript(jquery)文件,其中我正在使用:

I have got a javascript(jquery) file called custom.js in which i am using:

(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);

在我的标题中,我正在使用:

In my header i am using:

<script>var directorDate = new Date(<?php $date = get_option('director_date');?>
<?php if( $date) : ?>
<?php echo $date; ?>
<?php endif; ?>);
var directorCity = <?php $city = get_option('director_city');?>
<?php if( $city) : ?>
<?php echo $city; ?>
<?php endif; ?></script>

基本上发生的事情是标题从文件themeoptions.php获取数据(一个表格中的wordpress管理员为用户输入数据。我没有显示它,因为director_city和director_date的结构都相同)然后custom.js从标题中获取数据。

Basically what is happening is that the header gets the data from a file themeoptions.php (A form in the wordpress admin for a user to input data.I have not shown it because the structure of both director_city and director_date is the same there) and then custom.js gets the data from the header.

现在倒计时运行,但城市没有显示。为什么会这样?

Now the countdown runs however the city is not displayed. Why is that so?

推荐答案

您应该回显该值,而不是将其分配给php变量,如下所示:

You should probably echo the value instead of assigning it to a php variable like so:

var directorCity = <?php echo $get_option('director_city');?>

编辑:我不完全确定你想要完成什么,你可以试试以下for tbe head:

It's not entirely sure to me what you are trying to accomplish, you could perhaps try the following for tbe head:

<script>
var directorDate = new Date(<?php echo get_option('director_date');?>
var directorCity = <?php echo get_option('director_city');?>
</script>

这篇关于在wordpress中链接php和javascript错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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