在我的Wordpress网站上,我使用了一个自定义帖子(称为跟踪,可以在附加的代码中看到)。在此自定义帖子中包含分类法(称为track_category,也可以在随附的代码中看到)。尽管每个帖子都可以有多个轨道类别,但实际上,每个轨道都只有一个分类法(“远足”,“步行”,“山地自行车”或“公路自行车”)。
我创建了一些Track类型的帖子。每个帖子都有不同的分类法(“远足”,“步行”,“山地自行车”和“公路自行车”)
用户可以编辑曲目。编辑轨道时,我希望当前分配的分类法可以正确显示在下拉列表框中(然后用户可以更改分类法)。
但是,我的问题是在编辑自定义帖子时,未显示指定的分类法。始终显示第一个分类类型(即远足)。
为什么?!
已附加代码。任何想法都将非常受欢迎。
<?php
// This line is from the post template
echo do_shortcode('[sut_form alreadysubmitted="' . 1 . '" postid="' . $track_ID . '"]');
// Following code is in the plugin
add_shortcode('sut_form', 'sut_form_shortcode');
function sut_form_shortcode($atts, $content = null) {
$params = shortcode_atts(array(
'alreadysubmitted' => false,
'postid' => '',
), $atts );
$sut_postID = $params['postid']; //$sut_postID is now the ID of the custom post being looked at
// Lot of other code
// Lot of other code
$sut_track_name = get_the_title($sut_postID);
$sut_track_sentence = get_field('sentence', $sut_postID);
$terms = get_the_terms($sut_postID, 'track_category');
$sut_track_category = $terms[0]->name; // Custom post can only have one taxonomy so can select the first
echo sut_get_create_track_form($sut_track_name, $sut_track_sentence, $sut_track_category); // Create form
// PROBLEM --> resulting form does NOT show the taxonomy of the post. It always show the first taxonomy ( = Hike)
}
function sut_get_create_track_form($sut_track_name = '', $sut_track_sentence = '', $sut_track_category = ''){
$out .= '<form id="create_track_form" method="post" action="" enctype="multipart/form-data">';
// Other parts of the form
$out .= '<label for="sut_track_category">Track type</label><br/>';
$out .= sut_get_track_categories_dropdown('track_category', $sut_track_category) . '<br/>';
// Other parts of the form
$out .= '<input type="submit" id="sut_submit" name="sut_submit" value="Submit Track For Publication">';
$out .= '</form>';
return $out;
}
function sut_get_track_categories_dropdown($taxonomy, $selected){
return wp_dropdown_categories(array('taxonomy' => $taxonomy, 'name' => 'sut_track_category', 'selected' => $selected, 'hide_empty' => 0, 'echo' => 0));
}
add_action('init', 'sut_plugin_init');
function sut_plugin_init(){
$track_type_labels = array(
'name' => _x('Tracks', 'post type general name'),
'singular_name' => _x('Track', 'post type singular name'),
'add_new' => _x('Add New Track', 'track'),
'add_new_item' => __('Add New Track'),
'edit_item' => __('Edit Track'),
'new_item' => __('Add New Track'),
'all_items' => __('View Tracks'),
'view_item' => __('View Track'),
'search_items' => __('Search Tracks'),
'not_found' => __('No Tracks found'),
'not_found_in_trash' => __('No Tracks found in Trash'),
'parent_item_colon' => '',
'menu_name' => 'Tracks'
);
$track_type_args = array(
'labels' => $track_type_labels,
'public' => true,
'query_var' => true,
'rewrite' => true,
'capability_type' => 'post',
'has_archive' => true,
'hierarchical' => false,
'menu_position' => null,
'supports' => array('title', 'editor', 'author')
);
register_post_type('tracks', $track_type_args);
$track_category_labels = array(
'name' => _x( 'Track Categories', 'taxonomy general name' ),
'singular_name' => _x( 'Track', 'taxonomy singular name' ),
'search_items' => __( 'Search Track Categories' ),
'all_items' => __( 'All Track Categories' ),
'parent_item' => __( 'Parent Track Category' ),
'parent_item_colon' => __( 'Parent Track Category:' ),
'edit_item' => __( 'Edit Track Category' ),
'update_item' => __( 'Update Track Category' ),
'add_new_item' => __( 'Add New Track Category' ),
'new_item_name' => __( 'New Track Name' ),
'menu_name' => __( 'Track Categories' ),
);
$track_category_args = array(
'hierarchical' => true,
'labels' => $track_category_labels,
'show_ui' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'track_category' ),
);
register_taxonomy('track_category', array('tracks'), $track_category_args);
$default_track_cats = array('Hike', 'Walk', 'Mountain bike', 'Road bike');
foreach($default_track_cats as $cat){
if(!term_exists($cat, 'track_category')) wp_insert_term($cat, 'track_category');
}
}