# How can I echo the url of the image from a page or posts in the header for twittercard or opengraph?

**URL:** <https://forum.getkirby.com/t/how-can-i-echo-the-url-of-the-image-from-a-page-or-posts-in-the-header-for-twittercard-or-opengraph/4433>\
**Category:** Questions\
**Tags:** v2\
**Created:** [June 16, 2016, 8:32am UTC](https://forum.getkirby.com/t/how-can-i-echo-the-url-of-the-image-from-a-page-or-posts-in-the-header-for-twittercard-or-opengraph/4433 "2016-06-16T08:32:18Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Okaoo](https://avatars.discourse-cdn.com/v4/letter/o/7c8e57/32.png) [@Okaoo](https://forum.getkirby.com/u/Okaoo)\
**Post date:** [June 16, 2016, 8:32am UTC](https://forum.getkirby.com/t/how-can-i-echo-the-url-of-the-image-from-a-page-or-posts-in-the-header-for-twittercard-or-opengraph/4433/1 "2016-06-16T08:32:18Z")

</div>

Heja,

i need your support!

How can I echo the url of the image from a page or posts in the header for the meta information for the twittercard or opengraph.

```
<meta property="og:image" content=" " />  
<meta name="twitter:image" content=" ">

```

How can i echo the different images for a page and posts. I use for both the field coverimage().

I use the blogprint plugin for the blog.

Thanks for help!

---

<div class="post-metadata">

**Author:** ![Thiousi](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/thiousi/32/13656_2.png) [@Thiousi](https://forum.getkirby.com/u/Thiousi)\
**Post date:** [June 16, 2016, 8:35am UTC](https://forum.getkirby.com/t/how-can-i-echo-the-url-of-the-image-from-a-page-or-posts-in-the-header-for-twittercard-or-opengraph/4433/2 "2016-06-16T08:35:09Z")

</div>

Try this:

```auto
<meta property="og:image" content="<?php echo $page->image($page->coverimage())->url ?>" />  
<meta name="twitter:image" content="<?php echo $page->image($page->coverimage())->url ?>">

```

---

<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:** [June 16, 2016, 8:43am UTC](https://forum.getkirby.com/t/how-can-i-echo-the-url-of-the-image-from-a-page-or-posts-in-the-header-for-twittercard-or-opengraph/4433/3 "2016-06-16T08:43:38Z")

</div>

Don’t forget to check if the image exists first:

````auto
<meta property="og:image" content="<?php if($image = $page->coverimage()->toFile()) echo $image->url() ?>" />  
<meta name="twitter:image" content="<?php if($image = $page->coverimage()->toFile()) echo $image->url() ?>" />
```
````

---

<div class="post-metadata">

**Author:** ![Okaoo](https://avatars.discourse-cdn.com/v4/letter/o/7c8e57/32.png) [@Okaoo](https://forum.getkirby.com/u/Okaoo)\
**Post date:** [June 16, 2016, 8:56am UTC](https://forum.getkirby.com/t/how-can-i-echo-the-url-of-the-image-from-a-page-or-posts-in-the-header-for-twittercard-or-opengraph/4433/4 "2016-06-16T08:56:17Z")

</div>

Can i use this for pages and posts?

---

<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:** [June 16, 2016, 8:57am UTC](https://forum.getkirby.com/t/how-can-i-echo-the-url-of-the-image-from-a-page-or-posts-in-the-header-for-twittercard-or-opengraph/4433/5 "2016-06-16T08:57:08Z")

</div>

There’s no difference between pages and post in Kirby, `$page` usually refers to the current page (if not otherwise defined).

---

<div class="post-metadata">

**Author:** ![Okaoo](https://avatars.discourse-cdn.com/v4/letter/o/7c8e57/32.png) [@Okaoo](https://forum.getkirby.com/u/Okaoo)\
**Post date:** [June 17, 2016, 4:28pm UTC](https://forum.getkirby.com/t/how-can-i-echo-the-url-of-the-image-from-a-page-or-posts-in-the-header-for-twittercard-or-opengraph/4433/6 "2016-06-17T16:28:01Z")

</div>

Yes! It work’s! Thank you texnixe 🙂

---

<div class="post-metadata">

**Author:** ![rrrrfischer](https://avatars.discourse-cdn.com/v4/letter/r/6f9a4e/32.png) [@rrrrfischer](https://forum.getkirby.com/u/rrrrfischer)\
**Post date:** [January 26, 2017, 10:23pm UTC](https://forum.getkirby.com/t/how-can-i-echo-the-url-of-the-image-from-a-page-or-posts-in-the-header-for-twittercard-or-opengraph/4433/7 "2017-01-26T22:23:56Z")

</div>

I’ll directly jump into this topic. The solution above doesn’t fix Twitter cards for me, as I’m not using cover images. Is there also a way to use just the first image embedded in a article?

And, whats the way to put a excerpt into the [“description” of the Twitter card meta tag](https://dev.twitter.com/cards/types/summary-large-image)? I tested the following (somehow very obvious) but it directly crashed the site:

```
<?php echo excerpt($article->text(), 300) ?>

```

Thanks for helping me out 🙂

---

<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:** [January 27, 2017, 12:06am UTC](https://forum.getkirby.com/t/how-can-i-echo-the-url-of-the-image-from-a-page-or-posts-in-the-header-for-twittercard-or-opengraph/4433/8 "2017-01-27T00:06:33Z")

</div>

To get the first image in a folder (not the one already included in an article)

```auto
<?php
$image = $page->image();
// which is the same as
$image = $page->images()->first();

```

Excerpt:

```auto
<?= $page->text()->excerpt(300) ?>

```

---

<div class="post-metadata">

**Author:** ![rrrrfischer](https://avatars.discourse-cdn.com/v4/letter/r/6f9a4e/32.png) [@rrrrfischer](https://forum.getkirby.com/u/rrrrfischer)\
**Post date:** [January 27, 2017, 8:02am UTC](https://forum.getkirby.com/t/how-can-i-echo-the-url-of-the-image-from-a-page-or-posts-in-the-header-for-twittercard-or-opengraph/4433/9 "2017-01-27T08:02:37Z")

</div>

Thanks Texnixe! The excerpt is working like a charme. But the img-tag keeps empty. Tried both of them:

```
<?php $image = $page->image() ?>
<?php $image = $page->images()->first() ?>

```

Any idea?

---

<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:** [January 27, 2017, 8:28am UTC](https://forum.getkirby.com/t/how-can-i-echo-the-url-of-the-image-from-a-page-or-posts-in-the-header-for-twittercard-or-opengraph/4433/10 "2017-01-27T08:28:37Z")

</div>

Could you post your code?

You would have to check if the image exists and if so, echo the url of the image

```auto
<?php if($image = $page->image()) { echo $image->url(); } ?>
```

---

<div class="post-metadata">

**Author:** ![rrrrfischer](https://avatars.discourse-cdn.com/v4/letter/r/6f9a4e/32.png) [@rrrrfischer](https://forum.getkirby.com/u/rrrrfischer)\
**Post date:** [January 27, 2017, 8:39am UTC](https://forum.getkirby.com/t/how-can-i-echo-the-url-of-the-image-from-a-page-or-posts-in-the-header-for-twittercard-or-opengraph/4433/11 "2017-01-27T08:39:53Z")

</div>

Ahhhhh… with that snippet its working. You are wonderful 😄

Maybe you can also help with the excerpt - I just need a little tweak: Is there a way to check IF there is text inside the post, and if not take the post title as description (instead of the text excerpt)? I often publish just a picture with a only a subject line.

---

<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:** [January 27, 2017, 9:58am UTC](https://forum.getkirby.com/t/how-can-i-echo-the-url-of-the-image-from-a-page-or-posts-in-the-header-for-twittercard-or-opengraph/4433/12 "2017-01-27T09:58:35Z")

</div>

Sure:

```auto
<?php
if($page->text()->isNotEmpty()) {
  echo $page->text()->excerpt(300); 
} else {
  echo $page->title()->html();
}
?>

```

---

<div class="post-metadata">

**Author:** ![rrrrfischer](https://avatars.discourse-cdn.com/v4/letter/r/6f9a4e/32.png) [@rrrrfischer](https://forum.getkirby.com/u/rrrrfischer)\
**Post date:** [January 27, 2017, 12:46pm UTC](https://forum.getkirby.com/t/how-can-i-echo-the-url-of-the-image-from-a-page-or-posts-in-the-header-for-twittercard-or-opengraph/4433/13 "2017-01-27T12:46:09Z")

</div>

Thanks for the snippet, works for standard articles (with text). Unfortunately it doesn’t work for posts with not text and with a embedded image. IMHO thats because the image is part of the text, so that the text isn’t empty. Could that be the cause?

![](https://europe1.discourse-cdn.com/flex017/uploads/getkirby/original/2X/0/019e2c14f51e6a0813391cff4a166095e2548220.png)

---

<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:** [January 27, 2017, 12:59pm UTC](https://forum.getkirby.com/t/how-can-i-echo-the-url-of-the-image-from-a-page-or-posts-in-the-header-for-twittercard-or-opengraph/4433/14 "2017-01-27T12:59:15Z")

</div>

Yes, exactly, if there is anything in the field, it is not empty.

Alternative:

```auto
<?php
if(! $page->text()->excerpt(300) == '') {
   echo $page->text()->excerpt(300); 
} else {
   echo $page->title()->html();
}
?>

```

The excerpt method will return an empty string if there’s only an image kirbytag in it.

---

<div class="post-metadata">

**Author:** ![rrrrfischer](https://avatars.discourse-cdn.com/v4/letter/r/6f9a4e/32.png) [@rrrrfischer](https://forum.getkirby.com/u/rrrrfischer)\
**Post date:** [January 27, 2017, 6:57pm UTC](https://forum.getkirby.com/t/how-can-i-echo-the-url-of-the-image-from-a-page-or-posts-in-the-header-for-twittercard-or-opengraph/4433/15 "2017-01-27T18:57:53Z")

</div>

Thanks Texnixe, that solved the problem. You are awesome 😀  
Just in case, somebody else wants to use this snippet, there need to be done a small correction (you missed the } before the else).

```
<?php
if(! $page->text()->excerpt(300) == '') {
   echo $page->text()->excerpt(300); 
} else {
   echo $page->title()->html();
}
?>
```

---

<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:** [July 22, 2017, 8:18am UTC](https://forum.getkirby.com/t/how-can-i-echo-the-url-of-the-image-from-a-page-or-posts-in-the-header-for-twittercard-or-opengraph/4433/21 "2017-07-22T08:18:36Z")

</div>

I would just like to point out that theres a good plugin for this that is very powerful. [Meta Tags Plugin](https://forum.getkirby.com/t/meta-tags-plugin/6802)

If you still want to do it by hand then maybe using the code i used prior to finding the plugin might help… you can add your image stuff to it. Does require a couple of extra blueprint entries to capture SEO title & description and keywords:

```
<?php if ($page->isHomePage()): ?>

<title><?= $site->seotitle()->html() ?></title>
<meta name="description" content="<?= $site->seometa() ?>">
<meta name="keywords" content="<?= $site->seokeywords() ?>">
<link rel='canonical' href='<?= $site->url() ?>' />
<meta property='og:site_name' content='<?= $site->title()->html() ?>' />
<meta property='og:title' content="<?= html($site->title()) ?>" />
<meta property='og:url' content="<?= $site->url() ?>" />
<meta name='twitter:card' content='summary' />
<meta name='twitter:title' content="<?= html($site->title()) ?>" />
<meta name='twitter:description' content="<?= $site->seometa() ?>" />
<meta name='twitter:url' content="<?= $site->url() ?>" />

<?php else: ?>

<title><?= html($site->seotitle().' | '.$page->seotitle()) ?></title>
<meta name="description" content="<?= $page->seometa() ?>">
<meta name="keywords" content="<?= $page->seokeywords() ?>">
<link rel='canonical' href='<?= $page->url() ?>' />
<meta property='og:site_name' content='<?= $site->title()->html() ?>' />
<meta property='og:title' content="<?= html($site->seotitle().' | '.$page->seotitle()) ?>" />
<meta property='og:url' content="<?= $site->url() ?>" />
<meta name='twitter:card' content='summary' />
<meta name='twitter:title' content="<?= html($site->title().' | '.$page->seotitle()) ?>" />
<meta name='twitter:description' content="<?= $page->seometa() ?>" />
<meta name='twitter:url' content="<?= $page->url() ?>" />

<?php endif ?>
```

---

<div class="post-metadata">

**Author:** ![tbrand](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/tbrand/32/4587_2.png) [@tbrand](https://forum.getkirby.com/u/tbrand)\
**Post date:** [September 12, 2017, 10:29pm UTC](https://forum.getkirby.com/t/how-can-i-echo-the-url-of-the-image-from-a-page-or-posts-in-the-header-for-twittercard-or-opengraph/4433/22 "2017-09-12T22:29:27Z")

</div>

Does anyone have a suggestion for adding a condition for a Twitter Image?  
For instance if an image is not found for a page use a default image from NAMED URL.  
I can’t seem to get this to work consistently.

Something like this  
`<meta name="twitter:image" content="<?php if(! $page->image()) { echo $image->url(); } else { echo 'DEFAULT IMAGE URL' ?>" />`

Thanks,

---

<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:** [September 13, 2017, 7:04am UTC](https://forum.getkirby.com/t/how-can-i-echo-the-url-of-the-image-from-a-page-or-posts-in-the-header-for-twittercard-or-opengraph/4433/24 "2017-09-13T07:04:12Z")

</div>

The `$image` variable is missing and the not operator works the wrong way round here, so this should work:

```auto
<meta name="twitter:image" content="<?php if($image = $page->image()) { echo $image->url(); } else { echo 'DEFAULT IMAGE URL'; } ?>" />

```

---

<div class="post-metadata">

**Author:** ![tbrand](https://dub1.discourse-cdn.com/flex017/user_avatar/forum.getkirby.com/tbrand/32/4587_2.png) [@tbrand](https://forum.getkirby.com/u/tbrand)\
**Post date:** [September 13, 2017, 11:44am UTC](https://forum.getkirby.com/t/how-can-i-echo-the-url-of-the-image-from-a-page-or-posts-in-the-header-for-twittercard-or-opengraph/4433/25 "2017-09-13T11:44:54Z")

</div>

Thank you,

Sorry I thought I fixed my syntax on the original post, but it looks like I deleted my corrected post by accident. You are right I got the not operator all wrong.

---

<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:22pm UTC](https://forum.getkirby.com/t/how-can-i-echo-the-url-of-the-image-from-a-page-or-posts-in-the-header-for-twittercard-or-opengraph/4433/27 "2025-02-06T17:22:54Z")

</div>

Archived on February 6, 2025, 5:22pm UTC


