【问题标题】:Multiple uri to same post in Wordpress多个uri到Wordpress中的同一个帖子
【发布时间】:2015-10-22 04:03:29
【问题描述】:

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

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

【问题讨论】:

  • 只是好奇...你为什么要做这样的事情?
  • 我的客户想要统计每个帖子的 Facebook 点赞数,但这是不可能的,因为 Facebook 点赞数不仅仅是点赞数,它的点赞数也算作点赞数。所以我要创建两个完全不同的 uri 但相同的帖子,然后放置单独的按钮。所以点赞永远不会影响分享。
  • 所以基本上你说的是你想要复制帖子内容,因为你只需要相同的内容、不同的 URL 和不同的点赞按钮。考虑一下就合理了,一切都应该不同,但文本..我建议安装一个 SEO 插件,这样其中一个帖子就会对搜索引擎隐藏,然后你就可以开始了。
  • 这个插件叫什么名字

标签: php wordpress .htaccess uri


【解决方案1】:

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

为此,您必须对已发布的重复帖子使用save_post 操作和插件/主题激活钩子(这可以是初始化钩子,但请注意,您只需要这样做一次 )。

首先你必须遍历所有帖子并进行重复

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);

【讨论】:

  • 请阅读那里的答案,您可以在每次运行 save_post 操作时使用保存帖子挂钩为新帖子创建副本。
  • 感谢您的回答
猜你喜欢
  • 2013-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-25
  • 2015-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多