【问题标题】:wordpress echo variable that holds string from user input保存来自用户输入的字符串的 wordpress 回显变量
【发布时间】:2014-02-11 18:04:46
【问题描述】:

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

现在我保存了字段,除最后一部分外一切正常,我无法通过 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' );

【问题讨论】:

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

标签: php string wordpress variables echo


【解决方案1】:

在你的函数中放置global $post

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-14
    • 2018-12-01
    相关资源
    最近更新 更多