I have a custom taxonomy named author
containing multiple terms. The default output order in the front end is alphabetically. But I like the authors to be ordered individually per post, e.g.
First post: Sam Meyer, Isabel Plim, Joseph Peters
Second post: Isabel Plim, Joseph Peters, Sam Meyer
I found this question with a relatively detailed answer but I didn’t manage to get it to work.
Questions:
— Where do I have to put the »HTML for the term order metabox«?
<?php
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>';
?>
— Does the »JavaScript to make the above list sortable and save the order using ajax« has to be enqueued like so?
<?php function myscript() { ?>
<script>
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;
});
});
</script>
<?php } ?>
<?php add_action( 'wp_footer', 'myscript' ); ?>
Aucun commentaire:
Enregistrer un commentaire