drmticpet66231422 2017-09-03 08:38
浏览 48
已采纳

为什么在同一函数内回显局部变量不起作用? [重复]

This question already has an answer here:

In the flowing Code :(see down)

I defined a local variable named post_author_nickname

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


The Code

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

Not working

"< ? php echo the_author_meta( 'nickname', $post_author_id ); ? > " 

Working

Edit 1 :

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

Edit 2 :

I checked

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

EDIT 3 :

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

When Gaurav sugesited in his anser changing

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

To

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

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

Now I understand

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.

thanks Gaurav and @mario

</div>
  • 写回答

1条回答 默认 最新

  • dsfds656545 2017-09-03 08:44
    关注

    Use this function instead of the_author_meta.

    get_the_author_meta()
    

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 求购一套带接口实现实习自动签到打卡
  • ¥50 MacOS 使用虚拟机安装k8s
  • ¥500 亚马逊 COOKIE我如何才能实现 登录一个亚马逊账户 下发新 COOKIE ..我使用下发新COOKIE 导入ADS 指纹浏览器登录,我把账户密码 修改过后,原来下发新COOKIE 不会失效的方式
  • ¥20 玩游戏gpu和cpu利用率特别低,玩游戏卡顿
  • ¥25 oracle中的正则匹配
  • ¥15 关于#vscode#的问题:把软件卸载不会再出现蓝屏
  • ¥15 vimplus出现的错误
  • ¥15 usb无线网卡转typec口
  • ¥30 怎么使用AVL fire ESE软件自带的优化模式来优化设计Soot和NOx?
  • ¥15 Ubuntu20.04.4.LTS系统如何下载安装VirtualBox虚拟机?