0

I apologies before I start this question is vagus and I don't have any code to show as I'm working locally.

I have a Wordpress Multisite, with 3 sites - 'site-1', 'site-2', 'site-3'.

They all import the same nav 'nav-section.php' from a templates folder.

The nav is hard coded but dynmically pulls content from 'site-2' for the the site-2 button

At the start of 'nav-section.php' I get the current blog id then switch to 'site-2'.

    $the_blog_id = get_current_blog_id();
    switch_to_blog(2);

In 'nav-section.php' I create the nav and pull content from 'site-2'.

At the bottom of 'nav-section.php' I switch back to the current blog

    switch_to_blog( $the_blog_id );

On each site I check the switch to blog has worked with

    echo get_current_blog_id();

This gives me the current blog id for each site and on every site it is correct.

Each site has pages that pull content from custom fields on that site.

On 'site-1' and 'site-2' the content from the custom fields is not being pulled through but on 'site-2' it is.

If I remove the switch_to_blog(2) in 'nav-section.php' the dynamic content is not added to the nav but the custom field content appears on all pages correctly.

I can only think the switch back to the current blog is not working.

I know this is vague but does anyone have any experience with WP and blog switching.

1 Answer 1

1

If you use switch_to_blog, that use always also restore_current_blog. The restore_current_blog function undos the last call to switch_to_blog, but only by one step, the calls are not nestable, so always call restore_current_blog before calling switch_to_blog again.

1
  • I nest my switch_to_blog calls and call this at the end. switch_to_blog(1); unset($GLOBALS['_wp_switched_stack']); $GLOBALS['switched'] = false; I found the code somewhere on SE and I haven't been having any problems with this. Commented Jul 22, 2015 at 3:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.