【问题标题】:PHP - How do I Add Exceptions or Exclude Pages, Sub Pages, or Objects from a "For Each" Loop?PHP - 如何从“For Each”循环中添加例外或排除页面、子页面或对象?
【发布时间】:2015-08-08 02:46:38
【问题描述】:

我正在尝试使用 WordPress 4.2.2 排除某些子页面(仅列为“页面”)

我在尝试排除一些页面时遇到了麻烦,因为使用下面的代码,脚本会抓取父页面下的所有页面或子页面并对其执行命令。

有一个“foreach”循环,我想知道我可以添加什么来排除一些页面。

代码是主题的一部分,是网站特定页面/部分的 PHP:

<?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>

【问题讨论】:

  • 您想如何排除页面?基于 ID、标题、...?
  • 我想根据页面标题排除页面

标签: php wordpress loops for-loop foreach


【解决方案1】:

您可以制作一系列要跳过的标题。然后将它们与标题匹配,看是否需要显示或继续下一项。

$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; ?>

或者您可以在 get_pages 函数中使用它们的 id 排除它们。

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

【讨论】:

  • 好的,我完全找到了——我只使用了你的底部公式——非常感谢!
  • @nmorris86 如果您喜欢它,请将其标记为正确答案,以便其他有相同问题的人可以找到正确的解决方案。
猜你喜欢
  • 1970-01-01
  • 2021-10-21
  • 2012-09-20
  • 2014-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-07
相关资源
最近更新 更多