为什么在同一个函数内回显局部变量不起作用? [英] Why echoing a local variable inside the same function is not working?

查看:26
本文介绍了为什么在同一个函数内回显局部变量不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在流动的代码中:(见下)

In the flowing Code :(see down)

我定义了一个名为 post_author_nickname 的局部变量

I defined a local variable named post_author_nickname

为什么我不能在与 HTML echo 相同的函数中使用它?

Why I can't use it inside the same function as HTML echo?

我直接使用函数而不是将其分配给变量

I'm using the function directly instead of assigning it to variable

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 ); ? > " 

工作

感谢 @mario 建议阅读问题get_the_有什么区别* 和 the_* 模板标签在 wordpress 中?- 堆栈溢出但我不明白答案是什么?或者它是如何相关的

Thanks @mario for suggesting reading the question What is the difference between get_the_* and the_* template tags in wordpress? - Stack Overflow but I didn't understand what the answer? or how it is related

注意:我不熟悉 php (10%) 并且只将它用于 wordpress 我的背景是在 c# 中

NOTE: I'm not familiar with php (10%) and only use it for wordpress my background is in c#

阅读 @mario 后建议 分析器好几次我都明白了.

After reading @mario suggested anser multible times I got it.

Gaurav sugesited 在他的 anser 改变

When Gaurav sugesited in his anser changing

$post_author_nickname =  the_author_meta( 'nickname', $post_author_id );

$post_author_nickname =  get_the_author_meta( 'nickname', $post_author_id );

我没有注意到函数名称的get前缀之间的区别'

I didn't note the deferent between the and get prefix to the function name'

现在明白了

通常,如果问题被标记为重复,我会删除它.

Usually, I deleted the question if it is marked as duplicate.

如果像我这样的人没有得到它,我会留下这张纸条.

I will leave this note if some one like me didn't get it.

感谢 Gaurav@马里奥

推荐答案

使用这个函数代替 the_author_meta.

Use this function instead of the_author_meta.

get_the_author_meta()

https://developer.wordpress.org/reference/functions/get_the_author_meta/

这篇关于为什么在同一个函数内回显局部变量不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆