2012-01-19 8 views
0

TwitterでTwitterを投稿したり、Google +で好きなFacebookを好きなときに呼び出すカスタムプラグインを作成しています。現在のポストの代わりにページにロードされた最後の変数を使用したAJAXコール

index.phpファイル内で、これをループ内に入れて、ホームページの新しい投稿ごとに呼び出されるようにします。

ここ
<?php cp_social_sharing_4pts(); ?> 

私のPHPの機能の一部:AJAXはそれをここに発射した後

function cp_social_sharing_js(){ 
if (is_user_logged_in()) { 
echo ' 
<script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script> 
<script type="text/javascript"> 
twttr.events.bind(\'tweet\', function(event) { cp_share_twitter_do(); }); 
</script> 
'; 
} 
} 
add_action('wp_head','cp_social_sharing_js'); 

function cp_share_twitter_do(){ // Tweet Entry 
      $cp_postID = $_POST['post']; // THIS IS THE ISSUE. It's not returning the correct ID from the post I shared on. 
    // Do more functions here 
    echo json_encode($r); 
    die(); 
} 
add_action('wp_ajax_cp_share_twitter_do', 'cp_share_twitter_do'); 

function cp_social_sharing_4pts(){ 
if (is_user_logged_in()) { 
echo '<script type="text/javascript"> 
var postid = '.get_the_ID().'; 
function cp_share_twitter_do(){ 
jQuery.ajax({dataType: "json", data: {action: "cp_share_twitter_do", post: postid}, 
    success: function(data){ 
    if(data.success==true){ 
    Boxy.alert(data.message); 
    thebox.hide(); 
    thebox.unload(); 
} else { Boxy.alert(data.message); } 
} 
}); 
} 
</script> 
'; 
} 
// Show Buttons 
echo 'Post ID: '.get_the_ID().'<br />'; // debug 
echo '<a href="https://twitter.com/share" class="twitter-share-button" data- url="'.get_permalink().'" data-text="'.get_the_title().'" data-count="horizontal" data-  via="ps3blogdotnet">Tweet</a>'; 
} 

その後、これは頭の中にロードされます

これは完全に動作しますt。問題はホームページにあります。

変数投稿IDは、各投稿の投稿IDを正しく表示します。しかし、ajax呼び出しが完了すると、現在の投稿の代わりにホームページに表示されている最後の投稿の投稿IDが常に渡されます。

+0

を役に立てば幸いそれ。 –

答えて

0

私は、ループが終了した後で再度IDを取得していないため、その理由がわかります。たとえばcp_share_fb_do()を呼び出すと、var postidはwhileループの最後のIDとして設定されます。あなたはちょうどあなたが新しいVAR名にpost: postidを変更することを確認することを忘れないで

function cp_share_fb_do(){ 
    var id = '.get_the_ID().'; 
    jquery.ajax({ ///etc 

各機能などで get_the_ID()を思い出すまたは代わりにVARにIDを保存する必要があります。

は、それはあなたが本当にのようなテキストの巨大な塊を出力するためのPHPモードの[ヒアドキュメント](http://php.net/heredoc)■または少なくとも「抜け出す」を使用してを検討する必要がある

+0

返事ありがとうございます。私はこれを試しました: 'function cp_share_gplus_do(){var post'.get_the_ID()。 ' = '.get_the_ID()。';私はまたこれを試しました: 'function( 'data type:" json "、data:{action:" cp_share_gplus_do "、post:post_.get_the_ID()。'}最後の投稿IDを取得しています。cp_share_gplus_do(){var postid = '.get_the_ID()。'; jQuery.ajax({dataType: "json"、data:{action: "cp_share_gplus_do"、post:postid}私が共有している最初の投稿の代わりにホームページにあります – Tosh

+0

ボタンを設定したときの問題はソーシャルネットapi側での経験はあまりありませんが、 '.get_the_title ().'と '' .get_permalink()。 ''。正しいIDを取得していることを確認し、IDがwhileループ内で渡されていることを確認してください。 – Zac

関連する問題