简体   繁体   中英

How get the permalink of a WordPress multisite shared post

In WordPress multisite setup each network site has its own wp_posts and wp_postmeta tables. So if I want to share a post from one wp network site to another I can use wpdb class to query posts from other network sites or I can use the switch_to_blog() WPMU function

<?PHP
$new_blog = 2;
switch_to_blog($new_blog);
?>
<a href="<?php echo get_post_permalink($post->ID) ?>"><?php echo $post->post_title ?></a>
<?php
restore_current_blog();
?>

the get_post_permalink() function will return the link to the shared post of the network site.

My question is how can I get a permalink of the shared post pointing to current's blog domain making it look like the post is hosted on the current blog.

All the plugins I found that share content across multiple WPMU sites duplicate and syndicate posts. I'm looking for a solution that would enable me to share some content and making it look like it has it's own url.

I would do the following, as @David Nguyen suggested.

<?php
    $new_blog = 2;
    switch_to_blog($new_blog);  

    $my_permalink = str_replace( home_url() . '/', network_home_url(), get_post_permalink($post->ID) );
?>
<a href="<?php echo $my_permalink ?>"><?php echo $post->post_title ?></a>
<?php
    restore_current_blog();
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM