我最终选择了CPT路线,因为我想不出其他方法。只是添加了一系列逻辑,以便在需要时复制/修改/删除正常帖子。
<?php // function for the CPT
function SU_kuuma_kysymys_type() {
// creating (registering) the custom type
register_post_type( \'kuuma_kysymys\', /* (http://codex.wordpress.org/Function_Reference/register_post_type) */
// let\'s now add all the options for this post type
array( \'labels\' => array(
\'name\' => ( \'Kuumat kysymykset\' ), /* This is the Title of the Group */
\'singular_name\' => ( \'Kuuma kysymys\' ), /* This is the individual type */
\'all_items\' => ( \'Kaikki kysymykset\' ), /* the all items menu item */
\'add_new\' => ( \'Lisää uusi\' ), /* The add new menu item */
\'add_new_item\' => ( \'Add New Custom Type\' ), /* Add New Display Title */
\'edit\' => ( \'Edit\' ), /* Edit Dialog */
\'edit_item\' => ( \'Muokkaa kysymystä\' ), /* Edit Display Title */
\'new_item\' => ( \'Lisää kysymys\' ), /* New Display Title */
\'view_item\' => ( \'Näytä kysymys\' ), /* View Display Title */
\'search_items\' => ( \'Etsi kysymyksiä\' ), /* Search Custom Type Title */
\'not_found\' => ( \'Kysymyksiä ei löytynyt.\' ), /* This displays if there are no entries yet */
\'not_found_in_trash\' => ( \'Roskakori on tyhjä.\' ), /* This displays if there is nothing in the trash */
), /* end of arrays */
\'public\' => true,
\'publicly_queryable\' => true,
\'exclude_from_search\' => true,
\'show_ui\' => false,
\'show_in_admin_bar\' => false,
\'query_var\' => true,
\'menu_position\' => 8, /* this is what order you want it to appear in on the left hand side menu */
\'menu_icon\' => get_stylesheet_directory_uri() . \'/library/images/custom-post-icon.png\', /* the icon for the custom post type menu */
\'rewrite\' => array( \'slug\' => \'kuuma-kysymys\', \'with_front\' => false ), /* you can specify its url slug */
\'has_archive\' => \'kuuma-kysymys\', /* you can rename the slug here */
/* the next one is important, it tells what\'s enabled in the post editor */
\'supports\' => array( \'title\', \'editor\', \'author\', \'comments\')
) /* end of options */
); /* end of register post type */
/* this adds your post categories to your custom post type */
register_taxonomy_for_object_type( \'category\', \'custom_type\' );
}
// adding the function to the Wordpress init
add_action(\'init\', \'SU_kuuma_kysymys_type\');
//first all the methods handling the question CPT posts
function SU_add_kysymys_post($post) {
$newpost = array(
\'post_name\' => $post->post_name,
\'post_title\' => $post->post_title,
\'post_content\' => $post->post_content,
\'post_status\' => $post->post_status,
\'post_type\' => \'kuuma_kysymys\',
\'post_author\' => $post->post_status,
\'post_category\' => $post->post_category
);
$kysymys_post_id = wp_insert_post($newpost, true);
// add the post ID\'s to their respective meta fields on both posts
update_post_meta($post->ID, \'question_post_id\', $kysymys_post_id);
update_post_meta($kysymys_post_id, \'original_post_id\', $post->ID);
}
function SU_update_kysymys_post($post, $q_post_id) {
$updatedpost = array(
\'ID\' => $q_post_id,
\'post_name\' => $post->post_name,
\'post_title\' => $post->post_title,
\'post_content\' => $post->post_content,
\'post_status\' => $post->post_status,
\'post_type\' => \'kuuma_kysymys\',
\'post_author\' => $post->post_status,
\'post_category\' => $post->post_category
);
wp_update_post($updatedpost, true);
}
function SU_delete_kysymys_post($post, $q_post_id = false) {
// If we dont give $q_post_id check if we have the meta field
if (!$q_post_id) { $q_post_id = get_post_meta($post_id, \'question_post_id\', true); }
// If we now have CPT post id -> delete it
if($q_post_id != "") {
update_post_meta($post->ID, \'question_post_id\', \'\');
// Remove the deletion hook in case we\'re just removing the category
remove_action(\'delete_post\', \'SU_check_kysymys_deletion\', 10);
wp_delete_post($q_post_id, true);
add_action(\'delete_post\', \'SU_check_kysymys_deletion\', 10);
}
}
// and the logic on what to do and when
function SU_check_kysymys($post_id, $post, $update) {
//if new post or doing autosave/revision do nothing
if(wp_is_post_revision($post_id) || wp_is_post_autosave($post_id) || get_post_status($post_id) == \'auto-draft\') { return; }
else {
// We\'re saving/updating, go for it
$q_post_id = get_post_meta($post_id, \'question_post_id\', true);
if (has_category(\'kuuma-kysymys\', $post_id)) { // has the category "kuuma kysymys"
if($q_post_id == "") {
SU_add_kysymys_post($post);
// wp_die(\'<pre>No q_post_id meta so new post?.<br><br>$post = \'.var_export($update, true).\'</pre>\');
} else {
SU_update_kysymys_post($post, (int)$q_post_id);
// wp_die(\'<pre>We have q_post_id so just update existing CP I think.<br><br>$post = \'.var_export($post, true).\'</pre>\');
}
} else { // doesn\'t have the category (at least not anymore)
if($q_post_id == "") { return; } // doesn\'t have the category or earlier remains of the meta field
else { // doesn\'t have the category but has meta field -> had the category before -> delete the added CP
SU_delete_kysymys_post($post, (int)$q_post_id);
//wp_die(\'<pre>We have post meta but lack the category so delete CP<br><br>$post = \'.var_export($post, true).\'</pre>\');
}
}
}
}
add_action(\'save_post\', \'SU_check_kysymys\', 10, 3);
// logic for deleting questions if the concurrent regular post is deleted for any reason
function SU_check_kysymys_deletion($post) {
if (has_category(\'kuuma-kysymys\', $post->ID)) {
SU_delete_kysymys_post($post);
}
}
add_action(\'delete_post\', \'SU_check_kysymys_deletion\', 10); ?>
但通过这种方式,我可以从两个不同的位置访问“相同”的帖子,并为每个帖子创建一个自定义模板。