Forum

Thread tagged as: Addons, Suggestions

Please make perch_blog_post_url allow dynamic fields in template

Hi,

Our blog posts have different URLs depending on whether they are targeted at UK visitors or US visitors.

UK URL format: https://example.com/news/post-slug US URL format: https://example.com/en-us/news/post-slug

I am trying to generate the correct post URL using the perch_blog_post_url setting (the one labelled "Blog post page path" in Settings).

Currently it is "/news/{postSlug}", which correctly generates the URL for UK posts, but not for US posts. It would be great if we could use dynamic fields in the template, rather than just the static ones. Then I could create a dynamic field called "prefix" or something like that, and then make the template "{prefix}/news/{postSlug}".

Is this a change that it would be possible to make? Or if there is another way to achieve what I am trying to do, please point it out.

Thanks, Mark

Mark Fisher

Mark Fisher 0 points

  • 2 years ago
Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Hello Mark,

One option could be to add the prefix in the template with Conditional Tags: https://docs.grabaperch.com/templates/conditionals/

<a href="<perch:if exists="prefix">/en-us/</perch:if><perch:blog id="postURL">">
   <perch:blog id="postTitle">
</a>
Drew McLellan

Drew McLellan 2638 points
Perch Support

You should be able to use any IDs from the post.html template in the post slug.

But since our URL format is "{prefix}/news/{postSlug}", we cannot put the prefix in the post slug.

I realised I said "if there is another way to achieve what I am trying to do, please point it out", without saying exactly what I am trying to do. To be clear, what I want is to get the "View Post" / "View Draft" buttons working when editing a post (particularly the View Draft button). PerchBlog_Post::substitute_url_vars allows you to use postID, blogID, postTitle, postSlug, postDateTime, postDescRaw, postDescHTML, postDynamicFields, postTags, postStatus, authorID, sectionID, postCommentCount, postImportID, postLegacyURL, postAllowComments, postTemplate, postMetaTemplate, postIsPublished, image, sectionTitle, SectionSlug, sectionPostCount, sectionDynamicFields, perch_image, blogTitle, blogSlug, setSlug and blogDynamicFields.

I could for example but the json_encoded string of postDynamicFields in the URL, but this would be meaningless. Any extra id I put in post.html appears in postDynamicFields, but PerchBlog_Post::substitute_url does not parse postDynamicFields, it just does the following:

if (isset($url_vars[$matches[1]])){
            return $url_vars[$matches[1]];
        }

Where $url_vars is an array with keys in the list above.

So I guess what I was asking in the title of this post is: if PerchBlog_Post::substitute_url_vars does not find the specified key in $url_vars, please make it parse postDynamicFields and see if the key is in there.