2

I have a WP Multisite that contains 3 sites - sites 1, 2, 3

Site 1 and 2 are just one page but site 3 has multi pages inside.

I would like to show a navigation on the top of each site to the other sites.

I would also like to show the child pages in site 3 on every page.

I'm using this code here to create the nav for all the sites.

<div class="navbar-collapse collapse">
<?php

  $the_blog_id = get_current_blog_id();
  $bcount = get_blog_count();

  global $wpdb;
  $blogs = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM $wpdb->blogs WHERE spam = '0' AND deleted = '0' and archived = '0' and public='1'",13, 'stack overflow' ) );

  echo ( $blogs->blogname );

  if ( !empty( $blogs ) ) {

?>
  <ul class="nav navbar-nav">
    <li class="portalHome">
      <a href="dashboard.html">
        <i class="fa fa-home"></i>
        <i class="fa fa-chevron-right pull-right chervonMobNav"></i>
      </a>
    </li>
<?php

  foreach ( $blogs as $blog ) {
    $details = get_blog_details( $blog->blog_id );

      if ( $details != false ) {
        $addr = $details->siteurl;
        $name = $details->blogname;
        $id = $details->blog_id;

        // echo $name ." is blog ". $id." / ";

        if ( !( ( $blog->blog_id == 2 ) && ( $show_main != 1 ) ) ) {
          $class = ( $blog->blog_id == get_current_blog_id() ) ? ' current-site' : '';

?>
    <li class="menu-item<?php echo $class; ?>" >
      <a href="<?php echo $addr; ?>"><?php echo $name;?> <i class="fa fa-chevron-right pull-right chervonMobNav"></i></a>
<?php

         if ( $blog->blog_id == 4 ) {

            echo 'here';
            //show the nav for blog 4

            $pages_args = array(
              'title_li' => '',
              'depth' => 0
            );

            wp_list_pages( $pages_args );

         }

?>
    </li>
<?php
        }
      }
    }
?>
  </ul>
<?php
  }
?>
</div><!--/.nav-collapse -->

The problem here is it only shows the pages in site 3 when I click that link and go it that site.

I want the nav to show the pages in site 3 when I'm on site 1 and site 2

So the nav in each site should look somneting like.

Site One | Site Two | Site Three
                        Site Three - page 1
                        Site Three - page 2
                        Site Three - page 3
1

You did a good job writing your own query to get all the sites/blogs, but there's also a special WordPress function you can use: wp_get_sites()

If you want to get content from another site/blog you have to switch first with switch_to_blog() to make sure your query will run within the right context. (And don't forget to switch back to normal with restore_current_blog() afterwards!)

Having that said, I updated your snippet accordingly, so you can give it a try:

<div class="navbar-collapse collapse">
  <ul class="nav navbar-nav">
    <li class="portalHome">
      <a href="dashboard.html">
        <i class="fa fa-home"></i>
        <i class="fa fa-chevron-right pull-right chervonMobNav"></i>
      </a>
    </li>
<?php

  $args = array(
    'public' => 1 // limit to public sites
  );
  $sites = wp_get_sites( $args );
  $current = get_current_blog_id(); // current blog id

  foreach ( $sites as $site ) :

    $site_meta = get_blog_details( $site['blog_id'] );
    $current_class = $site['blog_id'] == $current ? ' current' : ''; // check for current site

    echo '<li class="menu-item' . $current_class .'" >';
    echo '<a href="' . $site_meta->siteurl . '">' . $site_meta->blogname . ' <i class="fa fa-chevron-right pull-right chervonMobNav"></i></a>';

      if ( $site['blog_id'] == 4 ) : // check for special site
        switch_to_blog( 4 ); // now switch to this site

          $pages_args = array(
            'title_li' => '',
            'depth' => 0
          );
          echo '<ul>';
          wp_list_pages( $pages_args );
          echo '</ul>';

        restore_current_blog(); // switch back to normal
      endif;

    echo '</li>';

  endforeach;

?>
  </ul>
</div><!--/.nav-collapse -->

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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