Forum

Thread tagged as: Question, Blog

Blog Archive Year in title

Similar to this code to get a blog tag into the <title> tag, how do I get the archive year?

elseif (perch_get('tag')) { echo '<title>Articles tagged with ';
    perch_blog_tag(perch_get('tag'));
    echo '</title>';
    }
Neil Duddridge

Neil Duddridge 1 points

  • 5 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Do you have the year in the URL?

Yes, its blog/archive.php?year=2016

Drew McLellan

Drew McLellan 2638 points
Perch Support

You can use perch_get('year') then.

Thanks, I had tried using that but looks like I had error in the syntax when I was originally trying to figure it out (PHP not a particular strong point of mine)

This works

 elseif (perch_get('year')) { echo '<title>Articles in ';
    echo perch_get('year');
    echo '</title>';
    }

Thanks again

I've just tidied that up now to this

elseif (perch_get('year')) { echo '<title>Articles in ' . perch_get('year') . '</title>';
    }
Drew McLellan

Drew McLellan 2638 points
Perch Support

Be sure to HTML encode the values from the URL before writing them to the page. Otherwise anyone can inject code into your page by modifying the query string.

Thanks Drew, where would I do this? Can you give me an example?

Drew McLellan

Drew McLellan 2638 points
Perch Support

This is really beyond the scope of Perch, but you can use htmlspecialchars().

https://php.net/manual/en/function.htmlspecialchars.php

Ok, as a heads up is this what you meant. It still outputs ok on the front end.

elseif (perch_get('year')) { echo '<title>Articles in ' . htmlspecialchars(perch_get('year')) . '</title>';
    }

I can see that this produces the following (safe) result if you try to add some code to inject at the end of the string, correct?

<title>Articles in 2016&lt;body&gt;&lt;/body&gt;</title>