我得到它的工作。 $ post_ids和$ post_id有问题。它被分配了两倍。
这是我对任何人谁喜欢有它最终代码:
它能做什么。它在点击链接时指定一个标签并取消指定旧链接。我用它来让用户将附件的状态更改为已存档或未更改。
的HTML:
制作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来区分归档和不归档。