Užitečné pluginy, návody a šablony pro WordPress CMS

Uživatelské taxonomie, přidávání uživatelských polí k taxonomiím

Custom user taxonomies »

*

To add a custom field to your custom taxonomy, add the following code to your theme's functions.php:

// A callback function to add a custom field to our "presenters" taxonomy
function presenters_taxonomy_custom_fields($tag) {
// Check for existing taxonomy meta for the term you're editing
$t_id = $tag->term_id; // Get the ID of the term you're editing
$term_meta = get_option( "taxonomy_term_$t_id" ); // Do the check
?>
<tr class="form-field">
    <th scope="row" valign="top">
        <label for="presenter_id"><?php _e('WordPress User ID'); ?></label>
    </th>
    <td>
        <input type="text" name="term_meta[presenter_id]" id="term_meta[presenter_id]" size="25" style="width:60%;" value="<?php echo $term_meta['presenter_id'] ? $term_meta['presenter_id'] : ''; ?>"><br />
        <span class="description"><?php _e('The Presenter\'s WordPress User ID'); ?></span>
    </td>
</tr>
<?php
}

Next, we'll create a callback function that we'll use to save our custom fields. Add the following code to your theme's functions.php:

// A callback function to save our extra taxonomy field(s)
function save_taxonomy_custom_fields( $term_id ) {
if ( isset( $_POST['term_meta'] ) ) {
    $t_id = $term_id;
    $term_meta = get_option( "taxonomy_term_$t_id" );
    $cat_keys = array_keys( $_POST['term_meta'] );
    foreach ( $cat_keys as $key ){
        if ( isset( $_POST['term_meta'][$key] ) ){
            $term_meta[$key] = $_POST['term_meta'][$key];
        }
    }
//save the option array
update_option( "taxonomy_term_$t_id", $term_meta );
}
}

Let's associate these callback functions to the "edit" screen for our custom taxonomies. Add the following code to your theme's functions.php:

// Add the fields to the "presenters" taxonomy, using our callback function
add_action( 'presenters_edit_form_fields', 'presenters_taxonomy_custom_fields', 10, 2 );
// Save the changes made on the "presenters" taxonomy, using our callback function
add_action( 'edited_presenters', 'save_taxonomy_custom_fields', 10, 2 );

To access a custom field added to your custom taxonomy using the method noted earlier, add the following code inside your custom taxonomy template (in our case, taxonomy-presenters.php), within the PHP block at the top:

// Get the custom fields based on the $presenter term ID
$presenter_custom_fields = get_option( "taxonomy_term_$presenter->term_id" );
// Return the value for the "presenter_id" custom field
$presenter_data = get_userdata( $presenter_custom_fields[presenter_id] ); // Get their data

To see what data is stored inside the custom fields you created, add the print_r function to your template, then preview it:

<?php
    echo '<pre>';
    print_r( $presenter_custom_fields );
    echo '</pre>';
?>

To keep it dynamic, we are passing in the "presenter_id" field of our $presenter_cus­tom_fields variable. Now, to see what data you have to work with, use print_r again:

<?php
    echo '<pre>';
    print_r( $presenter_data );
    echo '</pre>';
?>

Zdroj: How To Add Custom Fields To Custom Taxonomies »


To add a form field to the Edit Category screen we’ll use the edit_category_for­m_fields action hook available in WordPress:

<?php
add_action ( 'edit_category_form_fields', 'tme_cat_featured');

function tme_cat_featured( $tag ) {

//check for existing featured ID
$cat_featured = get_option( 'category_featured' );
$featured_id = '';
if ( is_array( $cat_featured ) && array_key_exists( $tag->term_id, $cat_featured ) ) {
     $featured_id = $cat_featured[$tag->term_id] ;
}
?>
<tr class="form-field">
    <th scope="row" valign="top"><label for="category_featured"><?php _e('Featured Post ID') ?></label></th>
    <td>
        <input type="text" name="category_featured" id="category_featured" size="3" style="width:5%;" value="<?php echo $featured_id; ?>"><br />
        <span class="description">The post ID that will be the featured post when viewing this category.</span>
    </td>
</tr>
<?php
}
?>

The above code will add a single text field (Featured Post ID) to the Edit Category screen.

Now we need to save the input to our option array. To capture the ID when a category is updated we’ll use the edited_category action hook like so:

<?php
add_action ( 'edited_category', 'tme_save_featured');

function tme_save_featured( $term_id ) {
if ( isset( $_POST['category_featured'] ) ) {

    //load existing category featured option
    $current_featured = get_option( 'category_featured' );

    //set featured post ID to proper category ID in options array
    $current_featured[$term_id] = intval( $_POST['category_featured'] );

    //save the option array
     update_option( 'category_featured', $current_featured );
    }
}
?>

As you can see we are saving the Category ID and Featured Post ID as a single associative array option value in WordPress named category_featured. As you add Featured Post ID values to each of your categories they will be added to this array and saved in WordPress.

Save Taxonomy Meta Data as an Options Array in WordPress »


How to Add Custom Meta Fields to Custom Taxonomies in WordPress »

Adding Custom Meta Fields to Taxonomies »

How To Add Custom Fields To Custom Taxonomies»

WordPress: Adding custom fields to taxonomies »

WordPress Taxonomies Extra Fields the easy way »

Hooking WordPress Taxonomy Changes With The Plugins API »

Extending WordPress Taxonomies »

OVX.cz 2003 | Přihlásit se
Bez cookies už dnes těžko provozovat plnohodnotný web. Setrváním zde tudíž vyjadřujete souhlas s jejich používáním. Více informací. | Zavřít.