我写了一篇关于Creating an Alphabetical Glossary of Posts 但这里是最相关的部分。
首先为字母表中的字母创建一个隐藏的分类法
// Add new taxonomy, NOT hierarchical (like tags)
function kia_create_glossary_taxonomy(){
if(!taxonomy_exists(\'glossary\')){
register_taxonomy(\'glossary\',array(\'post\'),array(
\'show_ui\' => false
));
}
}
add_action(\'init\',\'kia_create_glossary_taxonomy\');
然后,在保存任何帖子时,将第一个字母另存为词汇表分类法中的术语
/* When the post is saved, saves our custom data */
function kia_save_first_letter( $post_id ) {
// 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 location (only run for posts)
$limitPostTypes = array(\'post\');
if (!in_array($_POST[\'post_type\'], $limitPostTypes)) return;
// Check permissions
if ( !current_user_can( \'edit_post\', $post_id ) )
return;
// OK, we\'re authenticated: we need to find and save the data
$taxonomy = \'glossary\';
//set term as first letter of post title, lower case
wp_set_post_terms( $post_id, strtolower(substr($_POST[\'post_title\'], 0, 1)), $taxonomy );
//delete the transient that is storing the alphabet letters
delete_transient( \'kia_archive_alphabet\');
}
add_action( \'save_post\', \'kia_save_first_letter\' );
瞧,现在知道你有A、B、C等术语,每个术语都应该包含标题以该字母开头的所有帖子。