如果是自定义字段,则将类添加到 div
我在 wilcity_belongs_to 的 WordPress 中有一个自定义字段,其值为 21956。我试图让我的 php 代码正常工作,这样,如果自定义字段的值为 21956,那么它将运行 Jquery 代码并将类 .hidden 添加到具体部门
运行此代码时我没有收到任何错误,尽管我也没有看到它与 Jquery 相呼应。我缺少什么?感谢
到目前为止更新的脚本:
global $post;
$meta_print_value = get_post_meta($post->ID,'wilcity_belongs_to',true);
if( $meta_print_value == '21956' ) {
echo "
<script type=\"text/javascript\">
$(document).ready(function() {
$('.wil-single-navimage1646724156466').addClass('hidden');
}
</script>
";
} else {
}
这是我试图引用的自定义字段: 自定义字段
I have a custom field in Wordpress of wilcity_belongs_to with a value of 21956. I am trying to get my php code to work, so that if the custom field's value is 21956 then it will run the Jquery code and add the class .hidden to a specific Div.
I do not get any errors running this code, although i do not see it echoing the Jquery either. What am i missing? Thanks
Updated Script so far:
global $post;
$meta_print_value = get_post_meta($post->ID,'wilcity_belongs_to',true);
if( $meta_print_value == '21956' ) {
echo "
<script type=\"text/javascript\">
$(document).ready(function() {
$('.wil-single-navimage1646724156466').addClass('hidden');
}
</script>
";
} else {
}
This is the custom field i am trying to refer to:
Custom Field
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这可能会发生,因为当脚本被附加到 DOM 时,类
.wil-single-navimage1646724156466
的元素甚至不存在,因为您仍然需要回显它,或者因为文档中的元素不存在还没有准备好,你需要等到元素出现在 DOM 中然后执行 jQuery 代码,这通常是通过
$((function(){...})
完成的,基本上在执行函数内容之前等待整个文档准备好this probably happens because the moment the script is appended to the DOM the element with class
.wil-single-navimage1646724156466
is not even present either because you still need to echo it or because the element in the document isn't ready yetyou need to wait until the element is present in the DOM then execute the jQuery code, this is usually done with
$((function(){...})
that basically waits for the whole document to be ready before the content of the function is executed