0

I have the following code for adding a dropdown box with a new taxonomy currently the default when I create a new post is None. Is there a way I can define a default, for example News.

function create_theme_taxonomy() {
    if (!taxonomy_exists('type')) {
        register_taxonomy( 'type', 'post', array( 'hierarchical' => false, 'label' => __('Article Type'), 'query_var' => 'type', 'rewrite' => true ));
    }
}

function add_type_box() {
    add_meta_box('type_box_ID', __('Article Type'), 'article_type', 'post', 'side', 'core');
    remove_meta_box('tagsdiv-type','post','core');
}   
function add_type_menus() {
    if ( ! is_admin() )
        return;
    add_action('admin_menu', 'add_type_box');
}
add_type_menus();

// This function gets called in edit-form-advanced.php
function article_type($post) {

    echo '<input type="hidden" name="taxonomy_noncename" id="taxonomy_noncename" value="' . 
            wp_create_nonce( 'taxonomy_type' ) . '" />';
    // Get all type taxonomy terms
    $types = get_terms('type', 'hide_empty=0'); 
?>
<select name='post_type' id='post_type'>
    <!-- Display types as options -->
    <?php 
        $names = wp_get_object_terms($post->ID, 'type'); 
        ?>
        <option class='type-option' value=''
        <?php if (!count($names)) echo "selected";?>>None</option>
        <?php
    foreach ($types as $type) {
        if (!is_wp_error($names) && !empty($names) && !strcmp($type->slug, $names[0]->slug)) 
            echo "<option class='type-option' value='" . $type->slug . "' selected>" . $type->name . "</option>\n"; 
        else
            echo "<option class='type-option' value='" . $type->slug . "'>" . $type->name . "</option>\n"; 
    }
   ?>
</select>    
<?php
}
0

This is the following new updated article_type function to make it work:

I have removed selected attribute from none option and added selected attribute to that terms having news slug.

// This function gets called in edit-form-advanced.php
function article_type($post) {

echo '<input type="hidden" name="taxonomy_noncename" id="taxonomy_noncename" value="' . 
        wp_create_nonce( 'taxonomy_type' ) . '" />';
// Get all type taxonomy terms
$types = get_terms('type', 'hide_empty=0'); 
?>
<select name='post_type' id='post_type'>
<!-- Display types as options -->
<?php 
    $names = wp_get_object_terms($post->ID, 'type');         
    ?>
    <option class='type-option' value=''>None</option>
    <?php
foreach ($types as $type) {
    if (!is_wp_error($names) && !empty($names) && !strcmp($type->slug, $names[0]->slug)) 
        echo "<option class='type-option' value='" . $type->slug . "' selected>" . $type->name . "</option>\n"; 
    else if(!strcmp($type->slug, 'news'))
         echo "<option class='type-option' value='" . $type->slug . "' selected>" . $type->name . "</option>\n";
    else
        echo "<option class='type-option' value='" . $type->slug . "'>" . $type->name . "</option>\n"; 
}
?>
</select>    
<?php
}
| improve this answer | |

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.