WordPress中的多个uri到同一个帖子

时间:2015-10-22 04:03:30

标签: php wordpress .htaccess uri

我想让两个uri指向Wordpress中的相同帖子,但没有重定向, mysite.co.uk/awesome-post mysite.co.uk/?p=12

我希望这两个uri指向相同的帖子但是当你到达页面时,uri不应该改变。

1 个答案:

答案 0 :(得分:2)

我知道的唯一解决方案是复制你的帖子并从中获取第二个链接并隐藏重复的循环,如果你希望后端。

为此你必须对已经发布的重复帖子使用save_post动作和插件/主题激活挂钩(这可以是init钩子但是要小心你需要这样做只有一次)。

首先,您必须遍历所有帖子并制作副本

add_action('switch_theme', 'your_prefix_setup_options');

function your_prefix_setup_options () {
  // WP_Query arguments
    $args = array (
        'post_type'              => array( 'post' )
    );

    // The Query
    $query = new WP_Query( $args );

    // The Loop
    if ( $query->have_posts() ) {

        while ( $query->have_posts() ) {
            $query->the_post();

            // duplicate post like this
            your_prefix_post_duplicate( get_the_ID(), get_the_title(), get_the_content(), get_post_thumbnail_id() );

        }

    } else {
        // no posts found
    }

    // Restore original Post Data
    wp_reset_postdata();
}

function your_prefix_post_duplicate($post_id, $title, $content, $attachment){


    $post_id = $post_id;
    $post_name = $title;
    $content = $content;
    $attachment = $attachment;


    $slug = str_replace( " ", "-", $post_name );
    $slug = strtolower($slug);

    $post_data = array(
        'post_content' => $content,
        'comment_status'    =>  'closed',
        'ping_status'       =>  'closed',
        'post_author'       =>  0,
        'post_name'     =>  $slug,
        'post_title'        =>  $post_name,
        'post_status'       =>  'published',
        'post_type'     =>  'post'
    );

    $prefix = 'your_prefix__';
    //create your duplicate post
    $duplicate_post_id = wp_insert_post( $post_data );
    set_post_thumbnail( $duplicate_post_id, $attachment );
    add_post_meta( $duplicate_post_id, $prefix . 'duplicate', TRUE );
    //get duplicate link
    $perma_link = get_permalink ( $duplicate_post_id );


    //set this link to your post as meta
    add_post_meta( $post_id, $prefix . 'duplicate_url', $perma_link );

}

// now you can get second uri for fb like/share
$second_link = get_post_meta(get_post_ID(),$prefix . 'duplicate_url', true);

//and you can use the meta and ignore duplicate post from showing on loop 
// or in advance you can use pre get post to hidethese duplicates from front end & back end permanently
get_post_meta(get_post_ID(),$prefix . 'duplicate', true);