2012-02-20 54 views
2

在WordPress的一个帖子我想ajaxify我的WordPress的主题,我使用ajax-in-WordPress method,现在我通过的functions.php尝试后get_the_content。使用jQuery,当我提醒(数据)时,我得到'标题'回声,但没有我想要的现有帖子的内容(返回0)。无法get_the_content();通过AJAX

我在做什么错?

jQuery的部分

$('.ajaxed,.ajaxed a,.menu-item-home a,.menu-item-object-page a').live('click', function(event) { 
     event.preventDefault(); 
     var link = $(this).attr('href'); 
     var toRemove = MySettings.url; 
     var rewritepath = link.replace(toRemove,''); 
     var handler = function(data) { 
      $('title').html($('title', data).html()); 
      $('#primary').html($('#primary', data).html()); 
      $('#primary').hide().fadeIn('slow'); 
      $.address.title(/>([^<]*)<\/title/.exec(data)[1]); 
     }; 
     $.post(ajax_object.ajaxurl, { 
      action: 'ajax_action', 
      post_id: $(this).find('input.post_id').attr('value') 
     },function(data) { 
      alert(data.post_title); 
      alert(data.post_content); 
     }); 
     /*$.ajax({ 
      url: link, 
      error: function(XMLHttpRequest, textStatus, errorThrown) { 
       handler(XMLHttpRequest.responseText); 
      }, 
      success: function(data, textStatus, XMLHttpRequest) { 
       handler(data, function(){ 
       }); 
      } 
     });*/ 
     $.address.state(MySettings.path).crawlable(true).value(rewritepath); 
     return false; 
    }); 

的functions.php的部分

<?php 
function javascripts() { 
    if(!is_admin()){ 
     $blogurl = get_bloginfo('url'); 
     $thumbnail_width = get_option('thumbnail_size_w'); 
     $thumbnail_height = get_option('thumbnail_size_h'); 
     $path = parse_url(get_bloginfo('siteurl'), PHP_URL_PATH); 
     $url = 'http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js'; 
     wp_deregister_script('jquery'); 
     if (get_transient('google_jquery') == true) {  
      wp_register_script('jquery', $url, array(), null, true); 
     } 
     else { 
      $resp = wp_remote_head($url); 
      if (!is_wp_error($resp) && 200 == $resp['response']['code']) { 
       set_transient('google_jquery', true, 60 * 5); 
       wp_register_script('jquery', $url, array(), null, true); 
      } 
      else { 
       set_transient('google_jquery', false, 60 * 5); 
       $url = get_bloginfo('wpurl') . '/wp-includes/js/jquery/jquery.js'; 
       wp_register_script('jquery', $url, array(), '1.7', true); 
      } 
     } 
     wp_enqueue_script('plugins.js', get_bloginfo('template_directory') . "/js/plugins.js" , array('jquery')); 
     wp_enqueue_script('ajax-script', get_bloginfo('template_directory') . "/js/scripts.js", array('jquery')); 
     wp_localize_script('ajax-script', 'ajax_object', array('ajaxurl' => admin_url('admin-ajax.php'))); 
     wp_localize_script('jquery', 'MySettings', array('width' => $thumbnail_width,'height' => $thumbnail_height,'url' => $blogurl,'path' => $path)); 
    } 
} 
add_action('wp_enqueue_scripts', 'javascripts'); 
add_action('wp_ajax_ajax_action', 'ajax_action_stuff'); // ajax for logged in users 
add_action('wp_ajax_nopriv_ajax_action', 'ajax_action_stuff'); // ajax for not logged in users 
function ajax_action_stuff() { 
    $post_id = $_POST['post_id']; 
    update_post_meta($post_id, 'post_key', 'meta_value'); //not sure why you need this 
    $post_data = get_post($post_id); 
    echo json_encode($post_data); 
} 
?> 

我在做什么错?由于

回答

2

你没有告诉get_the_content()其张贴到检索内容。在内部,该功能检查全局对象并过滤该对象的内容。

因此改变你的AJAX功能,这样的事情:

function ajax_action_stuff() { 
    global $post; 

    $post_id = $_POST[ 'post_id' ]; 
    update_post_meta($post_id, 'post_key', 'meta_value'); 

    $post = get_post($post_id); 

    $title = 'title'; 
    $content = get_the_content(); 

    echo $title; 
    echo $content; 
} 

这将使用你传递给查询特定职位数据库中的ID和产生全局$post对象。现在,get_the_content()乃至get_the_title()应该正常工作。

+0

不为我工作。我得到: 注意:试图获取非对象的属性在/var/www/vhosts/xxx.com/httpdocs/wp-includes/post-template.php在线279 – geoidesic 2016-07-18 10:42:35

2

没有看到你的代码的整个范围,看来,你可能会调用get_the_content()之外的The Loop上下文。如果是这样,该功能并不了解后你想检索的内容。尝试组织的功能是这样的:

function ajax_action_stuff() { 
    $post_id = $_POST['post_id']; 
    update_post_meta($post_id, 'post_key', 'meta_value'); //not sure why you need this 
    $post_data = get_post($post_id); 
    $title = $post_data->post_title; 
    $content = $post_data->post_content; 
    echo $title; 
    echo $content; 
} 

这里我们使用get_post()与所有的POST数据返回的对象。

您所创建的jQuery函数...

function(data) { 
    alert(data); 
}); 

...应主要包含包含您的标题和内容的data对象的字符串。

这里有一个建议,虽然,你如何能在一个更有条理的方式回报你的数据,如果你喜欢。

“数据”对象(这是你一直回荡在PHP函数ajax_action_stuff()什么)只是一个字符串值。但问题在于,数据的构造方式并不是jQuery完全理解和充分利用其潜力的方式。如果你改变你的php函数来返回一个JSON对象,那么你可以在jQuery中单独使用你所有的属性。我会告诉你如何...

function ajax_action_stuff() { 
    $post_id = $_POST['post_id']; 
    update_post_meta($post_id, 'post_key', 'meta_value'); //not sure why you need this 
    $post_data = get_post($post_id); 
    echo json_encode($post_data); 
} 

然后在jQuery的功能,您可以访问每个属性是这样的:

$.post(ajax_object.ajaxurl, { 
    action: 'ajax_action', 
    post_id: $(this).find('input.post_id').attr('value') 
},function(data) { 
    alert(data.post_title); 
    alert(data.post_content); 
}); 

看一看的get_post()函数来查看所有属性的你已经可以得到你。

+0

我已更新问题,添加了更多的代码和信息......我得到undefinied结果(2警告框)。 – Gab 2012-02-20 19:19:30

+0

让我们看看我们是否能够找出故障发生的位置......如果您使用Firefox和Firebug,请打开“控制台”选项卡并确保控制台已启用。然后点击你的锚点,触发一个AJAX调用。您应该在请求中看到控制台中的新帖子和响应。帖子数据是否包含您期望的所有数据?怎么回应?您应该在您的帖子中看到post_id变量和值,并且还会显示包含所有发布数据的JSON编码响应。你得到那么远吗? – 2012-02-20 21:25:49

+0

不,我没有看到所有的...... http://themes.visualise.ca/visualise/blog – Gab 2012-02-20 21:47:25