我不确定我是否完全理解你想要实现的目标,但我已经为你概述了一种方法,可以对与当前帖子相关的术语进行排序。
术语order metabox的Html:
echo \'<ul id="the-terms">\'
$terms = get_the_terms( $post->ID, $taxonomy );
foreach ( $terms as $term ) {
echo \'<li class="item" id="term-\'.$term->term_id.\'">\'. $term->name .\'</li>\';
}
echo \'</ul>\';
echo \'<a href="javascript: void(0); return false;" id="save_term_order" class="button-primary">Update Order</a>\';
Javascript使上述列表可排序并使用ajax保存顺序*注意:需要jQuery UI可排序。
jQuery(document).ready(function() {
// Make the term list sortable
jQuery("#the-terms").sortable({
items: \'.item\',
placeholder: \'sortable-placeholder\',
tolerance: \'pointer\',
distance: 1,
forcePlaceholderSize: true,
helper: \'clone\',
cursor: \'move\'
});
// Save the order using ajax
jQuery("#save_term_order").live("click", function() {
var postID = $("#post_ID").val();
jQuery.post(ajaxurl, {
action:\'save_term_order\',
cache: false,
post_id: postID,
order: jQuery("#the-terms").sortable(\'toArray\').toString(),
success: ajax_response()
});
return false;
});
});
将订单另存为自定义字段的WordPress ajax函数:
add_action ( \'wp_ajax_save_term_order\', \'term_order_save\' );
function term_order_save () {
global $wpdb;
$wpdb->flush ();
$item_id = $_POST[\'post_id\'];
$meta_key = \'_term_order\';
$order = $_POST[ \'order\' ];
$str = str_replace ( "term-", "", $order );
$int = str_replace ( "\'", "", $str );
update_post_meta ( $item_id, $meta_key, array ( \'term_order\' => $int ) );
$response = \'<p>Term order updated</p>\';
echo $response;
die(1);
}
这将以序列化数组的顺序保存术语id的列表:
_term\\u order=>数组(term\\u order=>“123312110,34,44,27”)
要在前端显示已订购条款的列表,请执行以下操作:
$orderd_terms = get_post_meta ( $post->ID, \'_term_order\', true );
$terms = $ordered_terms[ \'term_order\' ];
$term_ids = explode ( ",", $terms );
for ( $i = 0; $i < count ( $term_ids ); $i ++ ) {
$term = get_term( $term_ids[$i], $taxonomy, OBJECT)
echo $term->name;
}