2012-02-20 91 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