PHP – How do I Add Exceptions or Exclude Pages, Sub Pages, or Objects from a “For Each” Loop?

I am trying to exclude certain sub-Pages (just listed as “Pages”) using WordPress 4.2.2

I’ve been having troubles trying to exclude a few Pages, because with the code below, the script grabs ALL the Pages or sub-Pages under the Parent Page and executes commands on them.

Read More

There is a “foreach” loop, and I was wondering what I could add to exclude a few Pages.

The Code, which is part of theme and is the PHP for a particular Page/Section of the site is:

<?php get_header(); ?>

<div class="b-page">
<h1 class="b-page__title"><span><?php the_title(); ?></span></h1>
<div class="container">
    <div class="main">
        <div class="b-services">
            <?php if (have_posts()) : ?>
                <?php while (have_posts()) : the_post(); ?>
                    <?php the_content(); ?>
                <?php endwhile; ?>
            <?php endif; ?>

            <ul>
            <?php $subPages = get_pages(array('child_of' => $post->ID, 'sort_column' => 'menu_order'));
                foreach($subPages as $item): ?>
                <li class="item">
                    <h3 class="item-title"><?php echo get_the_title($item); ?></h3>
                    <div class="item-image"><a href="<?php echo get_permalink($item); ?>"><?php echo get_the_post_thumbnail($item->ID, 'page-thumbnail');?></a></div>
                    <div class="item-content">
<?php //echo apply_filters('the_excerpt', wp_trim_words($item->post_excerpt, 35)); ?>
                        <?php echo apply_filters('the_content', substr(strip_tags($item->post_content), 0, 140) . '&hellip;'); ?>
                        <a class="b-read-more" href="<?php echo get_permalink($item); ?>">Read More</a>
                    </div>
                </li>
                <?php endforeach; ?>
            </ul>

Related posts

1 comment

  1. You can make an array of titles you want to skip. And then match them with the title to see if you need to display it or continue to the next item.

    $skip = array("Title1", "Title2", "Very long page title");
    foreach($subPages as $item): ?>
      <?php
        // Skip these page title
    
        if (in_array(get_the_title($item), $skip)) {
          continue;
        }
      ?>
      <li class="item">
        <h3 class="item-title"><?php echo get_the_title($item); ?></h3>
        <div class="item-image"><a href="<?php echo get_permalink($item); ?>"><?php echo get_the_post_thumbnail($item->ID, 'page-thumbnail');?></a></div>
        <div class="item-content">
          <?php //echo apply_filters('the_excerpt', wp_trim_words($item->post_excerpt, 35)); ?>
          <?php echo apply_filters('the_content', substr(strip_tags($item->post_content), 0, 140) . '&hellip;'); ?>
          <a class="b-read-more" href="<?php echo get_permalink($item); ?>">Read More</a>
        </div>
      </li>
    <?php endforeach; ?>
    

    Or you can exclude them in the get_pages function with their id.

    $subPages = get_pages(array('child_of' => $post->ID, 'sort_column' => 'menu_order', 'exclude' => array(1,5,8,13), ));
    

Comments are closed.