admin管理员组文章数量:1122832
How do I use a custom filed value in an external javascript? I'd like to use a custom field already stored for a post that is an url in an external javascript
<script type='text/javascript' src='.js'></script>
in order to open a new window
onclick="window.open('MY_CUSTOM_FIELD_VALUE');"
I tried to add
global $post; $cf = get_post_meta( $post->ID, 'my_custom_field', true );
but it doesn't work. Thanks for your help.
How do I use a custom filed value in an external javascript? I'd like to use a custom field already stored for a post that is an url in an external javascript
<script type='text/javascript' src='http://www.my-website.com/wp-content/themes/mytheme/js/scripts.js'></script>
in order to open a new window
onclick="window.open('MY_CUSTOM_FIELD_VALUE');"
I tried to add
global $post; $cf = get_post_meta( $post->ID, 'my_custom_field', true );
but it doesn't work. Thanks for your help.
Share Improve this question asked Mar 24, 2014 at 9:43 advalueadvalue 32 silver badges5 bronze badges 2- You might be able to do with Ajax. You have the control of the 'external JavaScript' ? – sri Commented Mar 24, 2014 at 10:17
- yes I have control of the javascript... I've created it – advalue Commented Mar 24, 2014 at 10:36
1 Answer
Reset to default 0I had a similar problem using custom field values in a widget that used JavaScript. I wrote a tutorial explaining it. Try this code:
<?php
global $post;
$cf = get_post_meta($post->ID, 'my_custom_field', true);
?>
<button onclick="window.open(<?php echo json_encode($cf); ?>);">Click me</button>
本文标签: Custom field in external Javascript
版权声明:本文标题:Custom field in external Javascript 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736294580a1929388.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论