WordPress sub-site

I was recently asked to make what was effectively a sub-site within WordPress, so mybrilliantsite.com/apparentlydifferentsite, given that WordPress is generally setup to do a single site (with corresponding site title) per install (WordPress multiuser is intended for sub-domains) this required a little bit of fiddling (and a child theme, or self coded theme). NB, I am predominantly using Pages rather than Posts on this site, and am generally not creating functions to allow user defined descriptions etc because there isn’t any need for it in this particular case.

Display a different site-title (and site description) for the sub-site pages:

in header.php add the following elseif:

<?php elseif ( get_page_by_path("page-slug") ->ID == $post->post_parent || is_page( "Page Title" ) ):?>
    <h1 class="site-title"><a href='<?php echo esc_url( get_permalink( get_page_by_title( 'Page Title ') ) ); ?>' rel="home">Alternative Title</a></h1>

Then put the bit where header.php calls the site description into an if else loop:

if ( get_page_by_path("page-slug")->ID == $post->post_parent || is_page( "Page Title" ) ) : ?>
    <p class="site-description">Description of sub-site</p>
<?php else:

As a corollary to the above changes I also needed to make the page I was using as a parent display an alternative title since having it display the title of the “sub-site” twice would be silly, to do that I added the following to my content-page.php file:

<?php if ( is_page("Page Title") ) : ?>
    <h1 class="entry-title">Welcome</h1>
<?php else : the_title( '<h1 class="entry-title">', '</h1>' ); endif; ?>

Make the sub-site pages display a different menu

First I needed to add the menu to the backend of WordPress so it could be populated, by adding the following to my themes functions.php:

add_action ( 'init', 'register_additional_menu' );

function register_additional_menu() {
    register_nav_menu( 'my_additional_menu', __( 'My Additional Menu', 'themeslug' ) );
}

Once I’d done that I needed to make the menu appear. As I wanted it to replace the standard site menu I edited sidebar.php so that instead of just calling the primary navigation it checks to see which page it’s on and calls the appropriate menu:

<?php if ( get_page_by_path("page-slug")->ID == $post->post_parent || is_page( "Page Title" ) ) : ?>
    <?php if ( has_nav_menu( 'my_additional_menu' ) ) : ?>
        <nav id='site-navigation' class='main-navigation' role='navigation'>
    <?php // Custom menu.
    wp_nav_menu( array( 'menu_class' => 'nav-menu', 'theme_location' => 'my_additional_menu', ) ); ?>
        </nav>
<?php endif; ?>

<?php else : all the code that already existed

Voila, one sub-site constructed.

Disclosure

As an Amazon Associate I earn from qualifying purchases.