在两个不同位置显示帖子内容,不重定向

时间:2015-04-28 作者:jimihenrik

如何在中获取相同的帖子内容(默认)domain.com/year/month/post-slugdomain.com/custom/post-slug (无重定向)。

我可以通过某种重写来实现这一点吗?或者我需要一个具有自己永久链接结构的自定义帖子类型吗?

所以要点是,我想在不同的URL中以稍微不同的方式显示相同的内容。这可能吗?还有什么其他想法可以实现这样的东西吗?

2 个回复
最合适的回答,由SO网友:jimihenrik 整理而成

我最终选择了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); ?>
但通过这种方式,我可以从两个不同的位置访问“相同”的帖子,并为每个帖子创建一个自定义模板。

SO网友:user2914563

我认为你需要改变你过去和将来所有帖子的永久链接结构?

只需转到设置->永久链接,然后在那里键入自定义永久链接,现在我建议您使用永久链接查找器插件(未更新,但没有任何兼容性问题),它会自动将您的帖子重定向到更改的永久链接。。。我一直在使用它,它总是100%正确重定向。

结束

相关推荐

Dynamic Custom Permalinks

我有一个场景,其目的是创建可以动态更改的自定义永久链接,例如:如果显示国家信息,则URL应为http://example.com/country-information如果显示特定国家的城市信息,则URL应如下所示http://example.com/country/city-information. 我怎样才能做到这一点?