【发布时间】:2017-09-03 08:38:50
【问题描述】:
在流动的代码中:(往下看)
我定义了一个名为 post_author_nickname 的局部变量
为什么我不能在与 HTML echo 相同的函数中使用它?
我是直接使用函数而不是赋值给变量
代码
function head_scripts() {
$options = get_option( 'ps_plugindev' );
if ( isset( $options['twitter'] ) && !is_admin() ) {
$post_id = get_queried_object_id();
$post_author_id = get_post_field( 'post_author', $post_id );
$post_author_nickname = the_author_meta( 'nickname', $post_author_id );
?>
<script type="text/javascript">
function setT() {
var b = document.createElement('a');
b.classList += "twitter-share-button";
b.setAttribute("data-text", "<?php echo $post_author_nickname ?>" );
};
</script>
<?php
}
"< ? php echo $post_author_nickname; ? >"
不工作
"< ? php echo the_author_meta( 'nickname', $post_author_id ); ? > "
工作
编辑1:
感谢@mario 建议阅读问题 What is the difference between get_the_* and the_* template tags in wordpress? - Stack Overflow 但我不明白答案是什么?或者它是如何相关的
编辑 2:
我检查过
注意:我不熟悉 php (10%),只将它用于 wordpress 我的背景是 c#
编辑 3:
$post_author_nickname = the_author_meta( 'nickname', $post_author_id );
到
$post_author_nickname = get_the_author_meta( 'nickname', $post_author_id );
我没有注意到函数名的 the 和 get 前缀之间的区别
现在我明白了
通常,如果问题被标记为重复,我会删除它。
如果像我这样的人没有收到,我会留下这张便条。
【问题讨论】:
标签: php wordpress wordpress-theming