DUPLICATE: Link Type URL -> Dynamic Data: Output not as expected

I’ve created this function:

function product_url_suffix($post_title) {
    return str_replace(' ', '-', strtolower($post_title));
}

Which I insert into the output of either External URL or Dynamic Data:

This is what I insert:
https://site.example.com/{echo:product_url_suffix({post_title})}

My expected result:
https://site.example.com/post-title-here

My given results:
https://site.example.com/posttitlehere

It would appear that Bricks is interfering with my echo statement and manipulating the URL link?

WordPress slugifies the post titles in URLs automatically, isn’t it?

What is the need for this custom function?

Hi @xalodile ,

Please may I know why don’t you use {post_url}?
image

Regards,
Jenn

Because the post_url or even site_url does not outline what I need. I need the link to navigate to a different url, with the same slug as the current page.

Current page:
https://site.example.com/layer/post-title-here

Navigate to page:
https://site2.example.com/cake/post-title-here

Thus https://site2.example.com/cake/ would be static, but post-title-here needs to be generated dynamically to reflect the slug of the current post url. It can’t be static too because this relates to a Bricks template that will be used to all posts.

1 Like

Unfortunately not in this example. If I input https://site.example.com/layer/{post_title}, then the output becomes https://site.example.com/layer/post title here (notice the spaces).

If I do https://site.example.com/{echo:strtolower({post_title})}, that will output https://site.example.com/layer/posttitlehere

It’s strange behaviour. strtolower() is only supposed to ensure lower case, but in addition to this function WP seems to be removing the spaces. str_replace() seems to do the same, perhaps WP seems to remove the spaces before the function does its job?

Hi @xalodile ,

Understood.

Please do not use nested dynamic tags in the attributes area.

It’s a bug, you can follow this link WIP: Dynamic data is not rendered on links titles for update.

As a workaround, please create a custom function to output the post slug.

Example:

function my_post_slug() {
  $post = get_post( get_the_ID() ); 
  if ( is_a( $post, 'WP_Post' ) ) {
    return $post->post_name;
  }
  return '';
}

Then use {echo:my_post_slug}

Regards,
Jenn

1 Like

Excellent, thanks for the workaround!