Forum

Thread tagged as: Addons, Meta

Automatically generated excerpt for description meta tags

Hello, I am trying to achieve the following in post.php in blog.

<meta name="description" content="<perch:blog id="postDescHTML" words="30" append="..." />" />

That is, an automatically-generated, 30-word excerpt from the body of a post, appended with an ellipsis. I know the above is incorrect - I'm trying to illustrate what output I'd like to see.

I've tried to pass an array to perch_blog_post_field but I can't quite get it right, or maybe that tag doesn't accept an array?? Not sure. I would greatly appreciate any help.

Michael Gravel

Michael Gravel 0 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

It looks right, you just need to add type="textarea" I think.

Thanks for the response. Hmm. It doesn't work. I need to use a page function to achieve the same thing. Must be possible somehow?

Rachel Andrew

Rachel Andrew 394 points
Perch Support

Into which file are you adding this code?

I'm using the blog app, and I'm trying to do this in post.php (NOT in a template). I'd like to get an automatically-generated 30 word excerpt into the description meta tag. I know what I posted above won't work in that context. Is there a way to use a page function to auto generate an excerpt from the currently displayed article? Or maybe that's the wrong approach altogether? Thanks again!

Hi Michael,

I've got this working on my blog but it involves an extra field. I'm not totally sure why I did this and there's probably a more efficient way of achieving it, but it works for me.

I have this in my post.html template:

<perch:blog id="postClip" type="text" label="Excerpt" order="3" words="25" append="…" suppress="true" />

When I'm adding a new post in Perch admin I copy the first paragraph from the postDescHTML field into the postClip field.

Then in my actual post.php page I do this

<meta name="description" content="<?php perch_blog_post_field(perch_get('s'), 'postClip'); ?>"/>

As I say there is probably a better more efficient approach but it works.

Hope that helps.

Thanks for the response, John. Not quite what I'm after but it may have to do for now. Cheers, and thanks again.