【问题标题】:Why echoing a local variable inside the same function is not working? [duplicate]为什么在同一个函数中回显局部变量不起作用? [复制]
【发布时间】: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:

看完@mario建议anser 好多次了。

Gaurav 在他的anser 中更改时

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

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

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

现在我明白了

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

如果像我这样的人没有收到,我会留下这张便条。

感谢Gaurav@mario

【问题讨论】:

    标签: php wordpress wordpress-theming


    【解决方案1】:

    使用此函数代替 the_author_meta。

    get_the_author_meta()
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-20
      • 2021-10-03
      • 1970-01-01
      • 1970-01-01
      • 2013-01-09
      • 1970-01-01
      • 2021-06-10
      • 1970-01-01
      相关资源
      最近更新 更多