I have created a custom post type and added various meta boxes/fields to this custom post type. All is working excellent except for one element...
Instead of utilizing the default interface for selecting a taxonomy I would like to just have a drop down menu for the user to select from.
The idea here is to enable the admins to add taxonomy elements which can be managed centrally however for a specific post to only be associated with one taxonomy.
Further more, I would prefer to just add this drop down into one of my existing meta boxes.
Does anyone happen to have any sample code which would enable me to complete this task?
I have created a custom post type and added various meta boxes/fields to this custom post type. All is working excellent except for one element...
Instead of utilizing the default interface for selecting a taxonomy I would like to just have a drop down menu for the user to select from.
The idea here is to enable the admins to add taxonomy elements which can be managed centrally however for a specific post to only be associated with one taxonomy.
Further more, I would prefer to just add this drop down into one of my existing meta boxes.
Does anyone happen to have any sample code which would enable me to complete this task?
Share Improve this question asked Sep 2, 2010 at 12:19 NetConstructorNetConstructor 4,41618 gold badges70 silver badges82 bronze badges 3- Is the existing meta box you want to modify one of your own, or one of the core boxes? – Jan Fabry Commented Sep 3, 2010 at 9:28
- one of my own... I used the WPAlchemy metabox code to create them but I guess that does not matter. – NetConstructor Commented Sep 3, 2010 at 22:18
- does anyone has a response to this by chance? bump? – NetConstructor Commented Sep 6, 2010 at 0:03
4 Answers
Reset to default 4This is how I did this.
<?php $tax = get_object_taxonomies('TAXONOMY_NAME');
$taxterms = get_terms( $tax, 'orderby=count&offset=1&hide_empty=0&fields=all' );
?>
<select name='tax' id='tax'>
<option value='' <?php if (!count( $names )) echo "selected";?>>Select Term</option>
<?php
foreach ( $taxterms as $term ) {
echo '<option value="' . $term->slug . '" selected>' . $term->name . '</option>',"\n";
} ?>
</select>
I answered this question on a different post: Saving Taxonomy Terms
I don't have code to do this, but it should be simple: create a dropdown named tax_input[your_taxonomy_name]
, where the values are id's if your taxonomy is hierarchical (like categories), values if not (like tags). If you use this name, I think it is saved automatically, without extra code from you. You can create the dropdown with the wp_dropdown_categories
function, pass the selected
option with the taxonomy term that should be selected. The callback function that creates the meta box gets the $post
parameter, so you can get the current taxonomy term from there.
To disable the meta box that would normally be added, you could set show_ui
to false when creating the taxonomy, or remove the meta box before it is drawn (I think the add_meta_boxes
hook is a good place). It will have the id tagsdiv-your_taxonomy_name
if it is not hierarchical, or your_taxonomy_namediv
if it is.
I have an option that creates the option menu of terms, and also links to the taxonomy. Also, this is a shortcode as well.
// Shortcode for Taxonomy Dropdown [taxonomy_dropdown taxonomy="category"]
function taxonomy_dropdown_shortcode($atts){
$atts = shortcode_atts(
array(
'taxonomy' => 'category', // Default taxonomy
),
$atts,
'taxonomy_dropdown'
);
// Ensure the taxonomy exists
if (!taxonomy_exists($atts['taxonomy'])) {
return '<p>Invalid taxonomy specified.</p>';
}
$taxterms = get_terms(array(
'taxonomy' => $atts['taxonomy'], // Specify the taxonomy
'orderby' => 'count',
'offset' => 1,
'hide_empty' => false, // Show all terms, even if empty
));
ob_start(); ?>
<form method="get" action="<?php echo esc_url(home_url('/')); ?>">
<select name="<?php echo esc_attr($atts['taxonomy']); ?>" id="tax-dropdown" onchange="this.form.submit()">
<option value='' <?php if (empty($taxterms)) echo "selected"; ?>>Select Term</option>
<?php foreach ($taxterms as $term) { ?>
<option value="<?php echo esc_attr($term->slug); ?>">
<?php echo esc_html($term->name); ?></option>
<?php } ?>
</select>
</form>
<?php return ob_get_clean();
}
add_shortcode('taxonomy_dropdown', 'taxonomy_dropdown_shortcode');
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744340234a4569358.html
评论列表(0条)