WordPress get_post_meta在为动态php编写的.php文件中不起作用? [英] Wordpress get_post_meta not working inside a .php file that is written for dynamic php?
问题描述
echo
function dynamicAdjust(){
jQuery('#main-home')css('margin-top',jQuery(window) height());
}
;
?>
也许有一个更好的方法来创建动态PHP(让我知道是否有)与后期元选项,但jQuery这里工作正常,我排在我的functions.php作为一个javascript文件,并且jQuery功能工作正常。
问题和整点将jQuery放在PHP文件中是为了让用户打开/关闭选项,例如当我将代码包装在这样的if语句中时。
<?php
if(get_post_meta($ post-> ID,'_moon_full_static_area',true)=='on'); {
echo
function dynamicAdjust(){
jQuery('#main-home')。css('margin-top',jQuery(window).height());
}
;
}
?>
这给了我这个错误致命错误:调用未定义的函数get_post_meta )在我的目录中。
确定所以根据消息显然,它看不到get_post_meta作为一个功能,我知道任何已经启用post_meta的连接到数据库,我只是不知道我需要做什么,而不是排入脚本?有任何想法吗?
PS .. 在WordPress函数文件中包含JS(带PHP)的最佳实践我遇到这个,这是我在找什么?
将PHP值传递给您的Javascript文件的标准方法是通过 wp_localize_script
。
另请参阅 在WordPress答案。
add_action('wp_head','localize_script_so_17497763');
函数localize_script_so_17497763()
{
//也许检查`is_single()`或其他条件标签
//检查帖子元
global $ post;
$ get_meta = get_post_meta($ post-> ID,'_moon_full_static_area',true);
$ meta_value =($ get_meta =='on')? '开关';
//构建要传递的本地化数组
$ localize_array = array(
'moon'=> $ meta_value,
'post_id'=> $ post - > ID,
'title'=> $ post-> post_title,
);
wp_enqueue_script('my-file',plugin_dir_path(__FILE__)。'my-file.js',array('jquery'));
wp_localize_script('my-file','wplocal',$ localize_array);
}
然后,在 my-file.js
:
console.log(wplocal.moon);
console.log(wplocal.post_id);
console.log(wplocal.title);
请参阅: PHP中的var_dump或print_r的JavaScript等价物是什么?
I want to tie in jquery functions with post meta options, so I have created a PHP file with jQuery code inside the PHP tags.
<?php
echo "
function dynamicAdjust() {
jQuery('#main-home').css('margin-top', jQuery(window).height());
}
";
?>
Maybe there is a better way to create dynamic PHP (let me know if there is) with post meta options, but the jQuery here works fine, I enqueued in in my functions.php as a javascript file, and the jQuery funciton works fine.
The issue and the whole point of having the jQuery inside a PHP file is for users to turn options on/off so for example when I wrap the code up there in an if statement like so.
<?php
if(get_post_meta($post->ID, '_moon_full_static_area', true) == 'on'); {
echo "
function dynamicAdjust() {
jQuery('#main-home').css('margin-top', jQuery(window).height());
}
";
}
?>
This gives me this error Fatal error: Call to undefined function get_post_meta() in my dir..
Ok so according to the message its obvious that it does not see get_post_meta as a function, I know anything that has post_meta enabled is connected to the database, Im just not sure what else I need to do other than enqueue the script?? Any ideas?
PS.. Best Practice for Including JS (with PHP) in WordPress Functions file I came across this, is this what I am looking for?
The standard way to pass PHP values to your Javascript files is through wp_localize_script
.
See also in WordPress Answers.
add_action( 'wp_head', 'localize_script_so_17497763' );
function localize_script_so_17497763()
{
// Maybe check for `is_single()` or other Conditional Tag
// Check post meta
global $post;
$get_meta = get_post_meta( $post->ID, '_moon_full_static_area', true );
$meta_value = ( $get_meta == 'on' ) ? 'on' : 'off';
// Build localization array to be passed
$localize_array = array(
'moon' => $meta_value,
'post_id' => $post->ID,
'title' => $post->post_title,
);
wp_enqueue_script( 'my-file', plugin_dir_path( __FILE__ ).'my-file.js', array('jquery') );
wp_localize_script( 'my-file', 'wplocal', $localize_array );
}
And then, in my-file.js
:
console.log( wplocal.moon );
console.log( wplocal.post_id );
console.log( wplocal.title );
See: What is the JavaScript equivalent of var_dump or print_r in PHP?
这篇关于WordPress get_post_meta在为动态php编写的.php文件中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!