??yy 2015-01-19 21:13 采纳率: 0%
浏览 24

在Jquery变量中使用PHP

I'm trying to format what new comments look like on a Wordpress comment list, using Ajax. This is the snippet I'm working on.

The full piece can be found here: http://pastebin.com/UHnPgf4J

success: function(data, textStatus){
if(data=="success"){
var avatar = <?php echo get_avatar( $comment, 32 ); ?>;
var author =  <?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>'+'<div class="comment-author vcard">'+avatar+
      '<div class="comment-meta">'+author+'</div>'+
      '<div class="comment-time-stamp">'+timestamp+'</div>'+
      '<div class="comment-text">'+commenttext+'</div>'+'</li>'+).insertBefore(respond);
statusdiv.html('<p class="ajax-success" >Thanks for your comment. We appreciate your response.</p>');

}

The only one that works is "commenttext" because it doesn't have php. The others ("avatar" "author" and "timestamp") all come back with errors in Firebug.

I tried some suggestions I found, but couldn't get any to work. Any help would be appreciated.

  • 写回答

2条回答 默认 最新

  • weixin_33739646 2015-01-19 21:17
    关注

    You should brace the string values with double quotes in Javascript.
    Like this:

    var author =  "<?php the_author_meta( 'user_url'); ?>";
    
    评论

报告相同问题?