【发布时间】:2014-09-10 20:24:50
【问题描述】:
我使用此代码将 php 变量从 wordpress 帖子发送到函数,只要单击按钮,就会从帖子中删除标签“test”。 只要我在functions.php中使用它并且没有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; ?>">
他也没有删除标签。
这就是功能:
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);
}
}
干杯
【问题讨论】:
-
每次页面加载时都会触发 ajax 调用,因为您已将超时设置为 10 微秒。为了真正弄清楚 ajax 调用发生了什么,您还应该将 projekt-getter.php 的内容发布到您发送 ajax 请求的位置。
-
你需要开始在大括号上缩进你的代码,因为这是不可读的。
-
projekt-getter.php 是我发布的最后一个函数。