【问题标题】:ajax handler to delete a tag用于删除标签的 ajax 处理程序
【发布时间】: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 是我发布的最后一个函数。

标签: php ajax wordpress


【解决方案1】:

我让它工作了。 $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来获取当前分配的tag的id来区分归档和非归档。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-21
    • 1970-01-01
    相关资源
    最近更新 更多