再次感谢米洛和乔治的帮助。我最终决定使用自定义分类法。这是我的程序。
// 1. Create the two taxonomies I need.
function create_playground_taxonomy() {
register_taxonomy( \'cartoon-playground\', \'post\', array (
\'rewrite\' => array( \'slug\' => \'Cartoon-Playground\'),
\'hierarchical\' => false,
\'labels\' => array(
\'name\' => _x( \'Cartoon Playground Tags\', \'taxonomy general name\' ),
\'all_items\' => __( \'All Playground Cartoons\' ),
\'singular_name\' => _x( \'Cartoon Playground Tag\', \'taxonomy singular name\' )
)
)
);
}
add_action( \'init\', \'create_playground_taxonomy\' );
function create_treasury_taxonomy() {
register_taxonomy( \'cartoon-treasury\', \'post\', array (
\'rewrite\' => array( \'slug\' => \'Cartoon-Treasury\' ),
\'hierarchical\' => false,
\'labels\' => array(
\'name\' => _x( \'Cartoon Treasury Tags\', \'taxonomy general name\' ),
\'all_items\' => __( \'All Treasury Cartoons\' ),
\'singular_name\' => _x( \'Cartoon Treasury Tag\', \'taxonomy singular name\' )
)
)
);
}
add_action( \'init\', \'create_treasury_taxonomy\' );
// 2. Copies terms from post_tag to the two new taxonomies.
// After updating functions.php, refresh any non-admin page
// on your site——that\'s what activates the function.
function replicateTaxonomyTerms() {
$terms = get_terms( \'post_tag\' );
$count = count($terms);
if ( $count > 0 ){
foreach ( $terms as $term ) {
wp_insert_term( $term->name, \'cartoon-treasury\', array( \'slug\' => $term->slug) );
wp_insert_term( $term->name, \'cartoon-playground\', array( \'slug\' => $term->slug) );
}
}
}
add_action(\'init\', \'replicateTaxonomyTerms\');
// 3. Identifies post_tag terms that are associated with one post
// and associates that post with terms in the other taxonomies.
//
// I needed this code because I invented these taxonomies AFTER
// the posts were imported. If you define the taxonomies before
// importing, you should work with your import functions instead.
// This code was used only to initially update all my posts. Below
// is a different function that I use update the posts every day.
//
// So for example post1 is originally associated with the post_tag
// term \'green-bean.\' After running this code, post1 will also be
// associated with the treasury taxonomy term \'green-bean.\'
//
// Refresh a non-admin page once this code is in functions.php.
// Increment the offset by 500 (or whatever your server can handle)
// until you modify all your posts.
//
// When you\'ve updated all your posts, comment-out or delete the code.
function updatePostsTerms() {
global $post;
$args = array(
\'posts_per_page\' => 500,
\'offset\' => 0,
\'post_type\' => \'post\',
\'cat\' => 953
);
$the_query = get_posts( $args ); // Should grab ALL posts
if ($the_query) {
foreach ($the_query as $post) {
$post_id = $post->ID;
$args = array( \'orderby\' => \'name\', \'order\' => \'ASC\', \'fields\' => \'names\' );
$tags = wp_get_post_tags( $post_id, $args ); // Gets existing tags. The array is a list af arguments used to grab only the term IDs and order them.
$new_terms = implode(\', \', $tags);
$append = false;
wp_set_post_terms( $post_id, $new_terms, \'cartoon-treasury\', $append );
wp_set_post_terms( $post_id, $new_terms, \'cartoon-playground\', $append );
}
}
wp_reset_query();
}
add_action(\'wp_head\', \'updatePostsTerms\'); */
// Duplicates post_tag terms when post is saved. So for example user
// inputs \'chocolate\' as a new tag term. When saved, \'chocolate\' is
// saved to the other taxonomies as well.
//
// To maintain accurate tag clouds, I need some posts to NOT be
// associated with all taxonomies, and so I check the posts\' category
// to see if it\'s one that should have the post_tag term duplicated.
//
// Keep this code in functions.php.
function duplicateTerms( ){
$post_id = $_POST[\'post_ID\'];
$new_terms = $_POST[\'adv-tags-input\'];
$categories = $_POST[\'post_category\'];
$new_terms = explode(\',\', $new_terms);
foreach ($new_terms as $term){
wp_insert_term( $term, \'cartoon-treasury\', array( \'slug\' => $term) );
wp_insert_term( $term, \'cartoon-playground\', array( \'slug\' => $term) );
}
$append = false;
foreach ( $categories as $category ) {
if ( $category == 953 ) {
wp_set_post_terms( $post_id, $new_terms, \'cartoon-treasury\', $append );
}
}
foreach ( $categories as $category ) {
if ( $category == 4074 ) {
wp_set_post_terms( $post_id, $new_terms, \'cartoon-playground\', $append );
}
}
}
add_action( \'save_post\', \'duplicateTerms\' );
// With these taxonomies, I have control over tag clouds and template pages.
// When looking at cartoons via the slider, a user can click a tag from
// the tag cloud and be directed to another slider page. These slider pages
// have URLs that look like this: site.com/taxonomy-name/term , and I can
// modify these pages by modifying the taxonomy\'s template.
//
// When looking at cartoons on the blog, a user can click a tag from the tag
// cloud and be directed to posts in blog-format. URLs look like this: site.com
// /blog/term.
//
// I\'m still running into some problems with this setup, by most things
// are working. :)
谢谢大家的帮助。