对于这种情况,您可以考虑添加一个“action”;当您将鼠标悬停在其他项目上时会显示。
下面的代码不完整,还没有经过测试,但应该会让您走上正确的轨道
Add the action
首先,要添加操作链接,请使用
tag_row_actions
钩(请记住,与此挂钩的函数将在每个分类法上启动,因此需要检查分类法)。类似于:
add_filter(\'tag_row_actions\',\'my_term_action\',10,2);
function my_term_action($actions,$tag){
if($tag->taxonomy == \'issue\'):
$actions[\'my-action-class\'] = \'<a href="#"> My action </a>\';
endif;
return $actions;
}
The
$actions
数组是关联的,其中键将成为
span
包装动作的元素。值是
span
将环绕。
您可以使用$tag
(这是一个对象,术语id由$tag->term_id
) 确定要显示的操作,因此如果要显示“发布”操作
$actions[\'my-action-class\'] = \'<a class="Publish" termid="\'.$tag->term_id.\'" href="#"> Publish </a>\';
如果要显示“取消发布”操作,请添加:
$actions[\'my-action-class\'] = \'<a class="Unpublish" termid="\'.$tag->term_id.\'" href="#"> Unpublish </a>\';
我给他们不同的类和一个额外的属性。jQuery/AJAX将使用它执行相关操作。。。
The jQuery /AJAX
jQuery(document).ready(function(){
jQuery(".my-action-class a").click(function(){
change_publish_states(jQuery(this));
});
});
function change_publish_states(el){
if(el.hasClass(\'Publish\')){
new_state = \'Unpublish\';
}else{
new_state = \'Publish\';
}
jQuery.getJSON(ajaxurl,
{ term_id: el.attr("termid"),
action: "my_change_publish_state",
new_state: new_state
},
function(data) {
if (data.error){
alert(data.error);
}else{
//Update label and class
el.text(data.new_state);
el.attr(\'class\', data.new_state)
}
});
}
您需要在“自定义分类法”页面上添加此脚本,您可以在上运行函数
admin_enqueue_scripts
检查正在查看的页面并有条件地将其排队;
add_action(\'admin_enqueue_scripts\',\'load_my_ajax_script\');
function load_my_ajax_script(){
//Check globals like $pagenow==\'edit-tags\' and $taxonomy==\'issues\' or $current_screen
//to make sure you are on the right page and enqueue script - or add a filter
//to print the script at the footer.
global $current_screen;
if ( $current_screen->id != \'edit-issue\' )
return;
wp_enqueue_script( \'my_ajax_save\', get_stylesheet_directory_URI() . \'/my_ajax_save.js\' );
}
然后还要在AJAX操作挂钩上添加一个函数:
AJAX action
add_action(\'wp_ajax_my_change_publish_state\', \'change_the_publish_state\');
function change_the_publish_state(){
$term_id = $_GET[\'term_id\'];
$new_state = $_GET[\'new_state\'];
//Perform checks / logic & update publish status.
$term_meta = get_option( "taxonomy_term_$term_id" );
if ( $new_state == "Unpublish" )
$term_meta[\'issue_published\'] = \'yes\';
if ( $new_state == "Publish" )
$term_meta[\'issue_published\'] = \'\';
update_option( "taxonomy_term_$term_id", $term_meta );
//return the new state of the term
$result[\'new_state\'] = $new_state;
echo json_encode($result);
exit();
}