0

I am creating a single page site, that contains multiple sections. These sections are pages within WordPress.

I use the following loop to get all pages in index.php file.

<?php 
        $args = array('post_type' => 'page', 'orderby' => 'menu_order', 'posts_per_page' => -1);
        $loop = new WP_Query($args); 
    ?>
    <?php if ($loop) : while ($loop->have_posts()) : $loop->the_post(); ?>
    <?php 
        global $post;
        $slug = $post->post_name;
    ?>
    <section id="<?php echo $slug; ?>" class="slideshow">
        <div class="container">
           <h3><?php the_title(); ?></h3>
           <?php the_content(); ?>
        </div>
    </section>
    <?php endwhile; ?>
    <?php endif; ?>

To order the pages (sections) inside the index.php file, I use the menu_order loop parameter to position the sections (pages) depending on the menu order, as you can see in above code.

This is working correctly, but the problem starts here. Because the site contains multiple sections that represent a kind of advertising section, it should not display in the menu as a menu item.

Look for the visual I added to this question. Left is a sidebar that contains the menu items. If you click on a item, it scrolls to the right section (page) on the page.

I created the menu as follow:

<?php wp_page_menu( array(
       'sort_column' => 'menu_order, post_title',
       'menu_class'  => 'primary-menu',
       'exclude'     => '',
    )); 
?>

I can use exclude to exclude specific pages from the menu, but this should be hardcode. All sections (pages) that use the Template parallax should be exclude from the list of menu items. As you can see in the visual, page 2 should be exclude from the menu, ONLY from the menu, the section should still visible on the page.

It would be great, if there is a way to exclude these pages from the menu, but still use the order of the menu to position these sections on the right place, the same as the other ones that are not containing ads. Only the items that are exclude, should not display in the list of menu items.

Good to know is that each "advertising" section has the class parallax. And also the template has the header name in it (see below).

So if there is a way to code a function that will exclude these sections with the class parallax, would be great. There is a way to exclude pages hardcoded by putting in the pageID, but I am searching for a way this will be automatically handled within WordPress, by looking for the class parallax or something else...

Header of the parallax template:

<?php

/*
    Template Name: Parallax
*/

?>

If there are questions, I would like to hear from you. Hope someone can help me out! Thanks in advance. Have a good evening!

enter image description here

2

Exclusion has to be based on something connected to the pages that is stored in the database. The only thing template-related stored in the database is the filename of your Parallax template, which is stored in post meta under the key _wp_page_template. You could query for pages that either don't have the key _wp_page_template, or if it does have the key, that it doesn't equal whatever the filename of the template file is.

$args = array(
    'post_type'       => 'page',
    'posts_per_page'  => -1,
    'fields'          => 'ids',
    'meta_query'      => array(
        'relation'    => 'OR',
        array(
            'key'     => '_wp_page_template',
            'value'   => 'templates/parallax.php',
            'compare' => '!=',
        ),
        array(
            'key'     => '_wp_page_template',
            'compare' => 'NOT EXISTS',
        ),
    ),
);

You can then use the results of this query to build your own menu rather than using wp_page_menu. This will be more efficient in terms of number of queries, compared to the other option below...

EDIT - Another option is to query for all pages with the Parallax template. You can then use the results of this query to exclude those pages from wp_page_menu:

$args = array(
    'post_type'       => 'page',
    'posts_per_page'  => -1,
    'fields'          => 'ids',
    'meta_query'      => array(
        array(
            'key'     => '_wp_page_template',
            'value'   => 'templates/parallax.php',
            'compare' => '=',
        )
    ),
);

$pages_to_exclude = new WP_Query( $args );

wp_page_menu( array(
   'sort_column' => 'menu_order, post_title',
   'menu_class'  => 'primary-menu',
   'exclude'     => implode( ',', $pages_to_exclude->posts ),
));
  • Thank you for your answer. This looks like to work, but the only thing is that this will work for which page is showed, am I right? What I am searching for is, the way to exclude only the menu item in the menu if the item has the page template parallax, but it should still display the page. So all pages should be visible, only the ones with a custom template parallax should not appears in the menu item list. – Casper Nov 19 '15 at 14:57
  • It excludes all pages with the parallax template, the page currently being viewed is irrelevant. See the Custom Fields section for WP_Query for more details about meta queries. – Milo Nov 19 '15 at 16:32
  • Hmm this is not working as expect, because this array of arguments is used for the loop to walk through al pages. That is working correct, only what I want is to doesn't show the sections inside the menu. – Casper Nov 19 '15 at 18:05
  • Did you change parallax.php to the filename of your template file? – Milo Nov 19 '15 at 18:19
  • Yes, but your arg array should be added to the page loop, am I right? And parallax.php is saved in a folder templates. – Casper Nov 19 '15 at 18:21

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.