Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

one page wordpress site theme to have menu item(static page) which links to different page?

Hi, After i went through your "one-page-wordpress-site" course, i had used the theme and i built the website with four menus.

The content of first 3 menu items will scroll in the same page but my 4th menu content should open in different page. but right now its showing the content in the same page.

Could you please tell me how to open up the 4th menu item in different page instead of content showing in same one page. the below code comes from one-page-site.php

<?php 
                $args = array(
                    'post_type' => 'page',
                    'order' => 'ASC'
                );
                $the_query = new WP_Query( $args );         

                global $post;

            ?>
            <?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?> 

            <?php 
            if ($post->post_name == "info") {
                get_template_part( 'content', 'info' );
            }
            if ($post->post_name == "programm") {
                get_template_part( 'content', 'programm' );
            }
            if ($post->post_name == "referenten") {
                get_template_part( 'content', 'referenten' );
            }
            if ($post->post_name == "location") {
                get_template_part( 'content', 'location' );
            }
<?php endwhile; endif; ?>

Thank you C

2 Answers

Heather Newton
Heather Newton
13,598 Points

It looks like the code you've entered here is the portion used to print the content from individual pages into the home page. If you are saying you don't want Laocation to show on the home page, remove the portion of this code relating to the Location content.

Then, in the WP admin Appearance>Menus page, don't set a "Link" for the location menu item. Instead check the box for the location page in the Pages panel and click the Add to Menu button in the Pages panel (And remove the old Link menu item for Location).

Now, what happens with the menu on the seperate Location page is another story. Having additional pages other than the fixed home page brings up the need to have a second menu. The links you made in the main menu will be dead on any page that doesn't have sections with IDs that match the links.

I myself am currently looking for instructions on how to get the fixed link nav to only show on the home page but bring up a second nav that uses Pages in the nav instead of Links on every other page. I've figured out how to create this second menu by watching the videos about creating menues in the WP Theme Development course, but I can't fugure out how best to set up a conditional in header.php that shows the right menu in the right place. Any advice for this part of the process would be much appreciated.

Heather Newton
Heather Newton
13,598 Points

Actually, I think I figured it out :)

First I had to register the two menues in my child theme's functions.php file:

<?php
function register_theme_menus() {   
    register_nav_menus(
        array(
            'primary-menu'  => __( 'Primary Menu', 'Vertex' ),
            'front-page-menu'   => __( 'Front Page Menu', 'Vertex' ),
        )
    );
}
add_action ( 'init' , 'register_theme_menus' );

Next, I created the two different menues in the WP admin area. The Primary Menu is the one with links to individual pages, and the Front Page Menu is the one with anchor links matching the section IDs on the home page.

Then, I customized the nav portion of the code in my child theme's header.php file as follows. Note that I started with a code base from a theme I purchsed called Vertex from Elegant Themes, so certain aspects of it are very specific to my situation. It's probablly also important to note that, unlike the demonstation in the One Page WP Site workshop, I have my website set to 'Your Latest Posts' for the front page display under Settings>Reading. I'm making edits to the homepage in my child theme's front-page.php file.

<nav>
<?php
    $menuClass = 'nav';
    if ( 'on' == et_get_option( 'vertex_disable_toptier' ) ) $menuClass .= ' et_disable_top_tier';
        $primaryNav = '';
        $primaryNav = wp_nav_menu( 
            array( 
                'theme_location' => 'primary-menu', 
                    'container' => '', 
                    'fallback_cb' => '', 
                    'menu_class' => $menuClass, 
                    'echo' => false 
                )   
            );

        $frontPageNav = '';
        $frontPageNav = wp_nav_menu(
            array( 
                'theme_location' => 'front-page-menu', 
                    'container' => '', 
                    'fallback_cb' => '', 
                    'menu_class' => $menuClass, 
                    'echo' => false 
                ) 
            );

        if ( is_home() ) :
            echo( $frontPageNav );
        else :
            echo( $primaryNav );
        endif;
?>
</nav>

<?php do_action( 'et_header_top' ); ?>

Note that this code doesn't provide any fallback for what will happen if both of these menus haven't been set up in the WP admin panel, but, since I'm planning to be the only person who ever manages this website, I didn't want to spend my time making the theme extra foolproof for someone else's sake.

I hope that helps!

Hi Heather Newton, Thank you very much. I will check this solution and report you.

Thanks and regards, C