根据登录显示用户名


Show username depending on login

这是为构建wordpress评论系统的人准备的。此代码确定单击"回复"后的帖子。

目标是获取在"作者"表单中输入的名称或用户身份(通过 php),具体取决于该人是否已登录。

success: function(data, textStatus){
  if(data=="success"){
    var avatar = "<?php echo get_avatar($id_or_email, 48,'/Avatar.png' ); ?>";
    var author =  "<?php if ( is_user_logged_in() ) : ?><?php echo $user_identity; ?><?php else :?>"jQuery('#author').val()  ;
    var authorurl = "<?php the_author_meta( 'user_url'); ?>";
    var timestamp = "<?php printf(__('%1$s at %2$s'), get_comment_date(),  get_comment_time()) ?>";
    var commenttext =  jQuery('#comment').val();
    jQuery('<li class="new-post">'+'<div class="comment-author vcard">'+avatar+
    '<div class="comment-meta">'+'<b>'+'<a href="authorurl">'+author+'</b>'+'</a>'+
    '<div class="comment-time-stamp">'+timestamp+'</div>'+'</div>'+'</div>'+
    '<div class="comment-text">'+'<p>'+'<pre class="new-post-pre">'+commenttext+'</pre>'+'</p>'+'</div>'+'</li>').insertBefore(respond);
        statusdiv.html('<p class="ajax-success" ></p>');
}

这就是问题所在:

 var author =  "<?php if ( is_user_logged_in() ) : ?><?php echo $user_identity; ?><?php else :?>"jQuery('#author').val()  ;

您的 PHP 代码中缺少endif,并且在错误的地方使用了双引号(如果我很好地理解这个问题):

var author =  <? if ( is_user_logged_in() ) : echo '"'.$user_identity.'"'; else: ?> jQuery('#author').val()  <? endif; ?>;