Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm looking for some suggestions or tutorial links about how to create a meta box that allows users to choose from a list of link categories. So far I've been looking at this post about taxonomies in a drop down and this post about an audio file drop down. I've also looked at get_terms.

The end goal is to display the links within the user selected link category on a single post page.

Update #2. Have this working properly now (updated code). Last thing: I just need too find a way to keep the selected option selected. It saves the right value, but visually just defaults to the first option. Using this post as guidance.

function tf_book_purchase (){
    global $post;
            $custom  = get_post_custom($post->ID);
            $link    = $custom["link"][0];
            $selected = isset( $custom['link'] ) ? esc_attr( $custom['link'][0] ) :'';


            echo '<div class="link_header">';


            $myterms = get_terms("link_category");

            echo '<select name="link" id="link">';
            echo '<option class="buy_books">Select A Link Category</option>';
            foreach($myterms as $term){
                        $term_slug=$term->slug;
                        $term_name =$term->name;
                        $term_id =$term->term_id;


           ?>   <option value="<?php echo $term_id;?>" <?php selected( $selected, ".$term_id." ); ?>><?php echo $term_name;?></option>

            <?php   }


             echo '</select><br /></div>';
             echo '<p>Please select a set of purchase links for this book.</p>';

     }



    add_action ('save_post', 'save_tf_book_purchase');

    function save_tf_book_purchase() {

    global $post;

    // make sure we're on a supported post type
        if ( $_POST['post_type'] != 'books' ) return;  

        // verify this came from our screen and with proper authorization.
        if ( !wp_verify_nonce( $_POST['book_nonce_name'], 'book-nonce' )) return;

        // verify if this is an auto save routine. If it is our form has not been submitted, so we dont want to do anything
        if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return;


        // Check permissions
        if ( 'page' == $_POST['post_type'] ) {
            if ( !current_user_can( 'edit_page', $post_id ) ) return;
        } else {
            if ( !current_user_can( 'edit_post', $post_id ) ) return;
        }

    //if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE){ return $post->ID; }

    update_post_meta( $post->ID, 'link', esc_attr( $_POST['link'] ));

    }
share|improve this question
add comment

2 Answers 2

Ok, this took me a minute to realize what you were wanting to do, and to make sure that I understand it, I'm going to restate it here:

You have categories of links (using the default links manager in WordPress, correct?). You want to be able to associate a selected category with any given post and to display the links from that category on the single page view of the given post.

It seems to me that what you would then want to do is create a meta value, using a meta box, which you have rightly suggested, to store the link category value associated with that post.

I would recommend using this meta_box "plugin" in your theme's function.php file. I say "plugin" loosely, because this isn't a true plugin in the WordPress sense of the word, but rather a block of code that you'll associate with your theme.

See also these links: http://www.deluxeblogtips.com/meta-box/ and http://www.deluxeblogtips.com/meta-box/define-fields/

Note: In this last link (the define-fields one), you'll notice that one of the form field types is a select box. You'll have to mingle your code from above with the setup for the select box to get it to display in the meta box the way you want it to. Something like this (HUGE DISCLAIMER - I didn't test any of this. Just suggesting something that might work.) :

$myterms = get_terms("link_category");
$linkCatsArray = array();

foreach($myterms as $term){
    $linkCatsArray[$term->slug] = $term->name;
}

$meta_boxes[] = array(
    'id' => 'my_link_categories',       // meta box id, unique per meta box
    'title' => 'Link Categories',       // meta box title
    'pages' => array('post'),           // post types, accept custom post types as well, default is array('post'); optional
    'context' => 'normal',              // where the meta box appear: normal (default), advanced, side; optional
    'priority' => 'high',               // order of meta box: high (default), low; optional

    'fields' => array(  
        array(
            'name' => 'Available Categories',
            'id' => $prefix . 'link_cats',
            'type' => 'select',                     // select box
            'options' => $linkCatsArray             // your links category array
        )
    )
);

Then once you are able to store the selected category in the database as a metadata value, you would use the get_post_meta($ID, '_my_meta_key'); value to retrieve that value from within the loop. Then from there, you'll want to use the wp_list_bookmarks( $args ); function (click on the function to go to the codex page) to get the appropriate links categories.

I'd be curious to know how this works for you.

share|improve this answer
    
Yep. This looks pretty much like what I need. I'm going to give this a try when I'm home in a bit and let you know how it goes. Thanks! –  Nadine Sep 14 '12 at 13:25
    
Hmm. I will have to do this without a functions file 'plugin' tho, I already have a lot of metaboxes set up that connect to custom post types, and I don't want them interfering with each other. I'll see what i can hash together. –  Nadine Sep 14 '12 at 17:23
    
Also just edited original post. –  Nadine Sep 14 '12 at 17:32
    
Metaboxes shouldn't conflict with each other. Should be able to add as many as you need, whether for custom post types or for the base post type. Might help to organize it by breaking up chunks of code into individual includes files. –  Brent Sep 14 '12 at 21:38
    
Sorry, I guess what i meant is I didn't want to start using something plugin-ish, as I'm mostly done this project and don't want to risk something eating itself. :) But I will look at it for next time. +1 on includes for organizing. –  Nadine Sep 14 '12 at 23:07
add comment

Found the last bit. It was just a syntax error:

<?php selected( $selected, ".$term_id". ); ?>

Should be...

<?php selected( $selected, $term_id ); ?>

Entire Function:

function tf_book_deets_create(){
add_meta_box('tf_book_purchase', 'Book Purchase Links', 'tf_book_purchase', 'books','side','default');
}
function tf_book_purchase (){
    global $post;
            $custom  = get_post_custom($post->ID);
            $link    = $custom["link"][0];
            $selected = isset( $custom['link'] ) ? esc_attr( $custom['link'][0] ) :'';


            echo '<div class="link_header">';

            $myterms = get_terms("link_category");

             echo '<p>Please select a set of purchase links for this book.</p>';

            echo '<select name="link" id="link">';
            echo '<option class="buy_books">Select A Link Category</option>';
            foreach($myterms as $term){
                        $term_slug=$term->slug;
                        $term_name =$term->name;
                        $term_id =$term->term_id;


           ?>   <option value="<?php echo $term_id;?>" <?php selected( $selected, $term_id ); ?>><?php echo $term_name;?></option>


           <?php   } echo '</select><br /></div>';


}



    add_action ('save_post', 'save_tf_book_purchase');

    function save_tf_book_purchase() {

    global $post;

    // make sure we're on a supported post type
        if ( $_POST['post_type'] != 'books' ) return;  

        // verify this came from our screen and with proper authorization.
        if ( !wp_verify_nonce( $_POST['book_nonce_name'], 'book-nonce' )) return;

        // verify if this is an auto save routine. If it is our form has not been submitted, so we dont want to do anything
        if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return;


        // Check permissions
        if ( 'page' == $_POST['post_type'] ) {
            if ( !current_user_can( 'edit_page', $post_id ) ) return;
        } else {
            if ( !current_user_can( 'edit_post', $post_id ) ) return;
        }

    //if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE){ return $post->ID; }

    update_post_meta( $post->ID, 'link', esc_attr( $_POST['link'] ));

    }
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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