Php wordpress回显变量,用于保存用户输入的字符串

Php wordpress回显变量,用于保存用户输入的字符串,php,string,wordpress,variables,echo,Php,String,Wordpress,Variables,Echo,所以我正在制作一个插件,它使用自定义字段 现在我保存了字段,除了最后一部分,其他一切都正常工作,我无法让echo在头部以html的形式显示字符串。除$keywords为空外,所有内容均正确显示 我尝试过使用全局变量和静态变量,但结果相同 function PrintKeywords( $post ){ $keywords = get_post_meta( $post->ID, '_my_meta_value_key2', true ); echo '<meta

所以我正在制作一个插件,它使用自定义字段

现在我保存了字段,除了最后一部分,其他一切都正常工作,我无法让echo在头部以html的形式显示字符串。除$keywords为空外,所有内容均正确显示

我尝试过使用全局变量和静态变量,但结果相同

function PrintKeywords( $post ){

     $keywords = get_post_meta( $post->ID, '_my_meta_value_key2', true );
     echo '<meta name="keywords" content="' . $keywords . '" />';
}

add_action( 'wp_head',  'PrintKeywords' );
将全局$post放在您的函数中:

function PrintKeywords( $post ){
     global $post;
     $keywords = get_post_meta( $post->ID, '_my_meta_value_key2', true );
     echo '<meta name="keywords" content="' . $keywords . '" />';
}

您是否在header.php主题文件中调用了wp_head是,echo正在输出使用全局$post和print_r$post;谢谢,但我不确定你的确切意思,我尝试用全局$post替换$post,但这会导致代码中断…好的,我刚刚找到一个解决方案,但是如果你有其他解决方案,请发布!存储在变量中的字符串存储为数组,以回送数组。