0

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' ); ?>
  • If this is one question specifically regarding the third-party code provided in the other answer, I feel it should be posed in that answer's comments. If your questions are individual and unrelated, "How do I create a metabox?" and "How should I enqueue scripts?" have already been answered, and a little research should yield results. The Codex entries on add_meta_box() and wp_enqueue_script() may also prove useful. – bosco Jan 31 '15 at 7:31
0

For your first question:

— Where do I have to put the »HTML for the term order metabox«?

Take a look at the add_meta_box codex page, the example there shows you how to put a metabox on your posts admin pages.

Regarding your second question:

— Does the »JavaScript to make the above list sortable and save the order using ajax« has to be enqueued like so?

You should be using admin_enqueue_scripts, from within a plugin. Something like this:

function my_admin_scripts(){
  // js
  wp_enqueue_script( 'my_admin_js', plugins_url('myPlugin/js/my_admin.js') );
}
add_action( 'admin_enqueue_scripts', 'my_admin_scripts' );

Both of these bits of code should be called from a plugin. Wordpress has a guide to creating plugins at Writing a Plugin

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.