# Show content only if related pages exist

**URL:** <https://forum.getkirby.com/t/show-content-only-if-related-pages-exist/9968>\
**Category:** Questions\
**Tags:** v2\
**Created:** [March 13, 2018, 12:11pm UTC](https://forum.getkirby.com/t/show-content-only-if-related-pages-exist/9968 "2018-03-13T12:11:52Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jimbobrjames](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jimbobrjames/32/5802_2.png) [@jimbobrjames](https://forum.getkirby.com/u/jimbobrjames)\
**Post date:** [March 13, 2018, 12:11pm UTC](https://forum.getkirby.com/t/show-content-only-if-related-pages-exist/9968/1 "2018-03-13T12:11:53Z")

</div>

I using @texnixe related pages plugin but i want to hide some code unless related pages actually exist, otherwise I will end up with a useless heading and a dead list.

Heres my snippet:

```auto
<?php
$relatedPages = $page->related(array(
  'searchCollection' => $site->index()->find('blog')->children()->visible(),
  'searchField' => 'tags',
  'matches' => 2,
  'delimiter' => ','
  ));
?>

<!-- only if related pages exist -->
<div class="blog-similar flank">
  <header>
    <h2>Similar Articles</h2>
  </header>
  <ul>
  <?php foreach($relatedPages as $related): ?>
      <li><p><a title="<?= $related->title() ?>" href="<?= $related->url() ?>"><?= $related->title() ?></a></p></li>
  <?php endforeach ?>
  </ul>
</div>
<!-- end only if related pages exist --> 

```

I don’t know how to check the `$relatedPages` for a positive match on related articles. I don’t think `isset` will work because from `var_dump` it always gets a value, but i’m no genius 🙂

Whats the best way please?

---

<div class="post-metadata">

**Author:** ![jimbobrjames](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jimbobrjames/32/5802_2.png) [@jimbobrjames](https://forum.getkirby.com/u/jimbobrjames)\
**Post date:** [March 13, 2018, 1:31pm UTC](https://forum.getkirby.com/t/show-content-only-if-related-pages-exist/9968/2 "2018-03-13T13:31:26Z")

</div>

Sorted:

```auto
<?php if(!empty($relatedPages)): ?>
<div class="blog-similar flank">
  <header>
    <h2>Similar Articles</h2>
  </header>
  <ul>
  <?php foreach($relatedPages as $related): ?>
      <li><p><a title="<?= $related->title() ?>" href="<?= $related->url() ?>"><?= $related->title() ?></a></p></li>
  <?php endforeach ?>
  </ul>
</div>
<?php endif ?>

```

---

<div class="post-metadata">

**Author:** ![jimbobrjames](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jimbobrjames/32/5802_2.png) [@jimbobrjames](https://forum.getkirby.com/u/jimbobrjames)\
**Post date:** [March 13, 2018, 1:38pm UTC](https://forum.getkirby.com/t/show-content-only-if-related-pages-exist/9968/3 "2018-03-13T13:38:58Z")

</div>

Hmm Spoke to soon, why doesn’t the above work?

---

<div class="post-metadata">

**Author:** ![jimbobrjames](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jimbobrjames/32/5802_2.png) [@jimbobrjames](https://forum.getkirby.com/u/jimbobrjames)\
**Post date:** [March 13, 2018, 2:24pm UTC](https://forum.getkirby.com/t/show-content-only-if-related-pages-exist/9968/4 "2018-03-13T14:24:53Z")

</div>

Ive done it like this but it seems like a silly way…

```auto
<?php if(strpos($relatedPages, 'blog') !== false): ?>
<div class="blog-similar flank">
  <header>
    <h2>Similar Articles</h2>
  </header>
  <ul>
  <?php foreach($relatedPages as $related): ?>
      <li><p><a title="<?= $related->title() ?>" href="<?= $related->url() ?>"><?= $related->title() ?></a></p></li>
  <?php endforeach ?>
  </ul>
</div>
<?php endif ?>

```

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [March 13, 2018, 2:25pm UTC](https://forum.getkirby.com/t/show-content-only-if-related-pages-exist/9968/5 "2018-03-13T14:25:04Z")

</div>

Because `$relatedPages` is a collection. It is never empty:

```auto
<?php if($relatedPages->count()): ?>

```

> [@jimbobrjames](#):
>
> Ive done it like this but it seems like a silly way…

🙄

---

<div class="post-metadata">

**Author:** ![jimbobrjames](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/jimbobrjames/32/5802_2.png) [@jimbobrjames](https://forum.getkirby.com/u/jimbobrjames)\
**Post date:** [March 13, 2018, 2:26pm UTC](https://forum.getkirby.com/t/show-content-only-if-related-pages-exist/9968/6 "2018-03-13T14:26:51Z")

</div>

Thanks… i still have some hair left after two hours of pulling it.

---

<div class="post-metadata">

**Author:** ![texnixe](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/texnixe/32/5754_2.png) [@texnixe](https://forum.getkirby.com/u/texnixe)\
**Post date:** [March 13, 2018, 2:30pm UTC](https://forum.getkirby.com/t/show-content-only-if-related-pages-exist/9968/7 "2018-03-13T14:30:21Z")

</div>

> [@jimbobrjames](#):
>
> Thanks… i still have some hair left after two hours of pulling it.

Wonderful, then you can pull out some more next time 🙂

---

<div class="post-metadata">

**Author:** ![distantnative](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/distantnative/32/11319_2.png) [@distantnative](https://forum.getkirby.com/u/distantnative)\
**Post date:** [February 6, 2025, 5:28pm UTC](https://forum.getkirby.com/t/show-content-only-if-related-pages-exist/9968/8 "2025-02-06T17:28:40Z")

</div>


