# The img link is going to the blog page, not the article page

**URL:** <https://forum.getkirby.com/t/the-img-link-is-going-to-the-blog-page-not-the-article-page/6845>\
**Category:** Questions\
**Tags:** v2\
**Created:** [March 5, 2017, 4:54am UTC](https://forum.getkirby.com/t/the-img-link-is-going-to-the-blog-page-not-the-article-page/6845 "2017-03-05T04:54:34Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![AdamRasheed](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/adamrasheed/32/2948_2.png) [@AdamRasheed](https://forum.getkirby.com/u/AdamRasheed)\
**Post date:** [March 5, 2017, 4:54am UTC](https://forum.getkirby.com/t/the-img-link-is-going-to-the-blog-page-not-the-article-page/6845/1 "2017-03-05T04:54:34Z")

</div>

I’m currently setting up my blog page as follows:

```auto
<section>

        <?php if($articles->count()): ?>

          <?php foreach($articles as $article): ?>
    
            <article class="article article--blog">

              <header class="article-header">
                <img src="<?php $article->coverimage()->url(); ?>" alt="">

                <a href="<?php $article->url() ?>" class="article__img-link">
                  <?php snippet('coverimage', $article) ?>
                </a>

                <h2 class="article __title article__ title--blog">
                  <a href="<?= $article->url() ?>" class="article__title-link"><?= $article->title()->html() ?></a>
                </h2>
    
                <p class="article__meta"><?= $article->date('F jS, Y') ?></p>

              </header>
    
            </article>
    
    
          <?php endforeach ?>

        <?php else: ?>
          <p>This blog does not contain any articles yet.</p>

        <?php endif ?>

      </section>

```

and the coverimage snippet is:

```auto
<?php if($item->coverimage()->toFile()): ?>
  <figure>
    <img src="<?= $item->coverimage()->toFile()->url() ?>" alt="" />
  </figure>
<?php endif ?>

```

So, the article title (the h2) links to the article fine, how do I make the image link to the article as well instead of the blog page?

Thanks in advance!

---

<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 5, 2017, 9:18am UTC](https://forum.getkirby.com/t/the-img-link-is-going-to-the-blog-page-not-the-article-page/6845/2 "2017-03-05T09:18:30Z")

</div>

There’s an `echo()` missing:

```auto
<a href="<?= $article->url() ?>" class="article__img-link">
   <?php snippet('coverimage', $article) ?>
</a>

```

Note that this line doesn’t make sense either:

```auto
 <img src="<?php $article->coverimage()->url(); ?>" alt="">

```

First of all, there’s an `echo()` command missing. But secondly it will still not return what you expect, because you are trying to call `url()` on a field object, not an image object. The code should be the same as in your coverimage snippet.

```auto
<?php if($image = $article->coverimage()->toFile()): ?>
    <img src="<?= $image->url() ?>" alt="" />
<?php endif ?>

```

But then you would be repeating the same stuff twice.

Edit: Note that you should always check if the image exists first, like you do in your coverimage snippet. But I would change the snippet as well, so as not to repeat yourself:

```auto
<?php if($image = $item->coverimage()->toFile()): ?>
  <figure>
    <img src="<?= $image->url() ?>" alt="" />
  </figure>
<?php endif ?>

```

---

<div class="post-metadata">

**Author:** ![pedroborges](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/pedroborges/32/7038_2.png) [@pedroborges](https://forum.getkirby.com/u/pedroborges)\
**Post date:** [March 5, 2017, 9:23am UTC](https://forum.getkirby.com/t/the-img-link-is-going-to-the-blog-page-not-the-article-page/6845/3 "2017-03-05T09:23:15Z")

</div>

The reason the link doesn’t work is because it’s not being echoed:

```diff
- <a href="<?php $article->url() ?>" class="article__img-link">
+ <a href="<?= $article->url() ?>" class="article__img-link">

```

At first it’s not clear why you are repeating the cover image in your example… but the URL needs to be echoed there too.

```diff
- <img src="<?php $article->coverimage()->url(); ?>" alt="">
+ <img src="<?= $article->coverimage()->toFile()->url(); ?>" alt="">

```

---

<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 5, 2017, 9:27am UTC](https://forum.getkirby.com/t/the-img-link-is-going-to-the-blog-page-not-the-article-page/6845/4 "2017-03-05T09:27:42Z")

</div>

@pedroborges: In the second image tag, just echoing coverimage does not really make sense, see my post above.

---

<div class="post-metadata">

**Author:** ![pedroborges](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/pedroborges/32/7038_2.png) [@pedroborges](https://forum.getkirby.com/u/pedroborges)\
**Post date:** [March 5, 2017, 9:28am UTC](https://forum.getkirby.com/t/the-img-link-is-going-to-the-blog-page-not-the-article-page/6845/5 "2017-03-05T09:28:48Z")

</div>

Yeah, you are right @texnixe!

EDIT: I have updated my example, but it’s best practice to check if the image exists before displaying it and @texnixe has in her example.

---

<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 5, 2017, 9:44am UTC](https://forum.getkirby.com/t/the-img-link-is-going-to-the-blog-page-not-the-article-page/6845/6 "2017-03-05T09:44:43Z")

</div>

Oh, and more more thing, @AdamRasheed: if you make a habit of checking your source code in developer tools, you can find such issues more easily 🙂

---

<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, 4:52pm UTC](https://forum.getkirby.com/t/the-img-link-is-going-to-the-blog-page-not-the-article-page/6845/7 "2025-02-06T16:52:30Z")

</div>


