【问题标题】:ACF text previous postACF 文字 上一篇
【发布时间】:2016-12-15 07:12:44
【问题描述】:

我有一个 ACF 文本字段。

我想在这些文本上使用之前的帖子,但是我的代码不起作用...

这是行不通的这条线:

var r = parseInt('<?php the_field('red', $prev_post->ID); ?

这是我的代码:

 $(document).ready(function() {

    $(".next_image_cover a").hover(

        function() {

        var r = parseInt('<?php the_field('red', $prev_post->ID); ?>'); 
        var g = parseInt('<?php the_field('green', $prev_post->ID); ?>'); 
        var b = parseInt('<?php the_field('blue', $prev_post->ID); ?>'); 


        $("body").stop().animate({"background-color": "rgb(" + r + "," + g + "," + b + ")"}, "200");
        },
        function() {

        $("body").stop().animate({"background-color": "#e6e6e6"}, "200");

    }); 

【问题讨论】:

  • 你标记为 doesn't work 的行没问题。你确定$prev_post 被WP_Post 对象正确实例化了吗?你试过var_dump()吗?
  • 这些“

标签: javascript jquery html wordpress advanced-custom-fields


【解决方案1】:

你不能在 javascript 中使用 php 变量 -

parseInt('<?php the_field('red'); ?>', **$prev_post->ID**); 

【讨论】:

  • parseInt('ID); ?>');
  • 它根本不起作用,它取消了我以前的代码
  • 是的,你可以。您可以使用 PHP 输出 HTML。您可以将 Javascript 放在 HTML 中,当然,您也可以使用 PHP 输出 Javascript。这样做没有错,例如:&lt;script&gt;var url = '&lt;?php $url = get_bloginfo('url'); echo $url; ?&gt;';&lt;/script&gt;
  • var r = parseInt('ID); echo $url; ?>'; 是正确的?因为它没有工作
  • 不,@user3870112。这只是与您的问题无关的示例代码。顺便说一句,我已经看过最初的 before-edit 问题,现在我得到了你的评论 Rohan。
猜你喜欢
  • 2016-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-31
  • 1970-01-01
  • 1970-01-01
  • 2018-07-22
相关资源
最近更新 更多