2014-09-10 56 views
-1

我使用此代码发送一个php变量从wordpress后发送到一个函数,只要单击一个按钮,将删除帖子中的标记“测试”。 只要我在functions.php中使用它,并且没有使用ajax,标签删除功能就可以正常工作。ajax处理程序删除标记

jQuery(document).ready(function() { 
    setTimeout(function() { 
     jQuery(".taguntagclick").trigger('click'); 
    }, 10); 
}); 

jQuery(document).ready(function() { 
    jQuery('.json_click_handler').click(function() { 
     var c = jQuery(this).attr('rel'); 
     var d = jQuery(this).attr('alt'); 
     jQuery.ajax({ 
      url: 'wp-content/themes/kleinraumv4_ap/projekt-getter.php', 
      data: { 
       'selection': c, 
       'pid': d, 
       'tag': 'test' 
      }, 
      dataType: 'html', 
      success: function() { 
       alert('success'); 
      }, 
      error: function(errorThrown) { 
       alert('error'); 
       console.log(errorThrown); 
      } 
     }); 
    }); 
});  

只要我刷新页面,我得到的“成功” - 消息,尽管我还没有连点击:

<a href="#" rel="beard" class="json_click_handler taguntagclick" alt="<?php echo $attachment->ID; ?>"> 

而且他还doesn't删除标签。

那功能:

include("../../../wp-load.php");  
$selection = $_REQUEST['selection']; 
$post_ids = $_REQUEST['pid']; 
$tags= $_REQUEST['tag']; 


function untag_post($post_ids,$tags) { 
    global $wpdb; 
    if(! is_array($post_ids)) { 
     $post_ids = array($post_ids); 
    } 
    if(! is_array($tags)) { 
     $tags = array($tags); 
    } 

    foreach($post_ids as $post_id) { 
     $terms = wp_get_object_terms($post_id, 'post_tag'); 
     $newterms = array(); 
     foreach($terms as $term) { 
      if (!in_array($term->name,$tags)) { //$term will be a wordpress Term object. 
       $newterms[] = $term; 
      } 
     } 
     wp_set_object_terms($post_id, $newterms, 'post_tag', FALSE); 

    } 
} 

欢呼

+0

ajax调用是在每个页面加载时触发的,因为您已经设置了10微秒的超时时间来执行此操作。为了真正弄清楚ajax调用发生了什么,你还应该在发送你的ajax请求的地方发布projekt-getter.php的内容。 – MSTannu 2014-09-10 20:45:53

+0

您需要在大括号中开始缩进代码,因为这只是不可读的。 – developerwjk 2014-09-10 20:50:07

+0

projekt-getter.php是我发布的最后一个函数。 – mordondro 2014-09-11 07:15:56

回答

0

我得到它的工作。 $ post_ids和$ post_id有问题。它被分配了两倍。

这是我对任何人谁喜欢有它最终代码:

它能做什么。它在点击链接时指定一个标签并取消指定旧链接。我用它来让用户将附件的状态更改为已存档或未更改。

的HTML:

<a href="#" rel="ap_aktuell" class="json_click_handler2" alt="<?php echo $attachment->ID; ?>"><img width="30px" src="<?php echo get_template_directory_uri(); ?>/images/icons/beard.png"></a> 

制作taguntagfunc.php在您的主题,文件夹

include("../../../wp-load.php");  

$selection = $_REQUEST['selection']; 
$post_id = $_REQUEST['pid']; 
$tag= $_REQUEST['tag']; 


if($tag==ap_aktuell){ 
$untag= 'ap_aktuell'; 
$inserttag= 'ap_archiv'; 
untag_post($post_id,$untag); 
wp_set_post_tags($post_id, $inserttag, false); 

} 

elseif($tag==ap_archiv){ 
$untag= 'ap_archiv'; 
$inserttag= 'ap_aktuell'; 
untag_post($post_id,$untag); 
wp_set_post_tags($post_id, $inserttag, false); 

} 

使用这个js脚本来听的链接,已经.json_click_handler2分配。它会在rel和ALT-属性传递给函数:

jQuery(document).ready(function(){ 

jQuery('.json_click_handler2').click(function(){ 

var c = jQuery(this).attr('rel'); 
var d = jQuery(this).attr('alt'); 
jQuery.ajax({ 
      url: 'wp-content/themes/kleinraumv4_ap/taguntagfunc.php', 
      data:{ 
       'pid' : d, 
       'tag' : c 
       }, 
      dataType: 'html', 
      success:function(){ 
         window.location.reload(true); 
    }, 
      error: function(errorThrown){ 
       alert('error'); 
       console.log(errorThrown); 
      } 


    }); 

    }); 
}); 
到functions.php使用

最后下面的函数之一,实际上你想要做什么。过渡时期援助团。

function untag_post($post_ids,$tags) 
{ 
    global $wpdb; 
    if(! is_array($post_ids)) 
    { 
     $post_ids = array($post_ids); 
    } 

    if(! is_array($tags)) 
    { 
     $tags = array($tags); 
    } 

    foreach($post_ids as $post_id) 
    { 
     $terms = wp_get_object_terms($post_id, 'post_tag'); 
     $newterms = array(); 
     foreach($terms as $term) 
     { 
      if (!in_array($term->name,$tags)) 
      { //$term will be a wordpress Term object. 
       $newterms[] = $term; 
      } 
     } 

     wp_set_object_terms($post_id, $newterms, 'post_tag', FALSE); 
    } 
} 


function get_tag_ID($tag_name) { 
$tag = get_term_by('name', $tag_name, 'post_tag'); 
if ($tag) { 
return $tag->term_id; 
} else { 
return 0; 
} 
} 

我用get_tag_ID获取当前分配的标记的ID来区分归档和不归档。