Tell us what you are trying to do?
I want to display some View Shortcodes inside one site to another in the multisite installation.
I see that there is this code
switch_to_blog( $blog_id );
// Do something
restore_current_blog();
If I will create a shortcode in the main site like this
function view(){
switch_to_blog(5);
return do_shortcode{'[view-shortcode]'}
restore_current_blog();
}
add_shortcode('view', 'view');
You think it can work?
Because now I clone the posts from subsite to main site, I change the canonical URL to the partent subsite post and I show it, but this process it is not automated and the posts clone are a redundance in the database...
Is there any documentation that you are following? I don't know.
Is there a similar example that we can see? I don't know.
What is the link to your site? inonda.tv
Hi Francesco,
Thank you for contacting us and I'd be happy to assist.
I've tested the approach that you're planning and it works on my test website.
You can create a custom shortcode and use the "switch_to_blog" function within, to call output from the different site's view(s) from it.
Note: A function's executions stop at the "return" statement, which is why it is better to move the "restore_current_blog" function's call before the return.
Example"
switch_to_blog( 5 );
$output = do_shortcode('[wpv-view name="slug-of-the-view"]');
restore_current_blog();
return $output;
I hope this helps and please let me know if you need any further assistance around this.
regards,
Waqar
Thanks Waqar.
And also, I have another question.
If I will creating single shortcodes one for post title, one for featured image, one for post meta, to take them from the subsites using the
switch_to_blog( 2,5,7,8,ecc );
And I will use these shortcodes inside a View to display the last network posts like a slideshow, will Toolset View let me do it? Or it will crashes the network?
Hi Francesco,
Thanks for writing back.
The "switch_to_blog" is a WordPress own function, which supports only a single site ID in a single call, as explained in its documentation:
https://codex.wordpress.org/Function_Reference/switch_to_blog
To get data from the multiple sites, you can use it in a loop, as shown in the example snippet under the heading "Multiple switches", in the above link.
Toolset Views components, like a content template or view, shouldn't have any issue with your custom shortcode, as long as it returns that data properly.
For more ideas, suggestions, and recommendations based on best practices around general WordPress features and functions, you can also consult dedicated WordPress community forums:
https://wordpress.org/support/forums/
https://wordpress.stackexchange.com/
regards,
Waqar