1
<a href="{{box.learnMoreLink}}"> {{ box.learnMoreText }} </a> 

Currently I have a Matrix field that has two plain-text fields. I want to change the href ({{box.learnMoreLink}}) from plain-text to an entry. I am trying to figure out how to render the entry field. Just changing the field type in the CMS is resulting in content loss. Anyone know the best way to go about this?

2
  • Can you explain why you want to change to an entry field? Is there a link field on the entry field you want to use instead?
    – Brad Bell
    Commented Jul 13, 2017 at 22:53
  • I want to change it to an entry field to make the url dynamic. I tried setting up {{siteUrl}} but that didn't work.
    – user6999
    Commented Jul 17, 2017 at 21:33

1 Answer 1

1

If you change the entry type then you're going to lose content for sure.

If changing that field type to an entry is really what you want, great. You could do something like

{# grab the first entry that we've selected #}
{% set learnMoreLink = box.learnMoreLink.first() %}
{# spit out the url for the first entry #}
<a href="{{ learnMoreLink.url }}">{{ box.learnMoreText }}</a>
4
  • Hey, thanks for replying. I replaced "<a href="{{box.learnMoreLink}}"> {{ box.learnMoreText }} </a>" with your code snippet and it didn't work. The content is still getting lost.
    – user6999
    Commented Jul 17, 2017 at 17:55
  • Have you actually defined the relationship after changing the field type? Commented Jul 17, 2017 at 20:39
  • I am not totally sure what you mean by 'defining the relationship'. Could you please elaborate? Sorry, I am new to craft. All I did was change the field type from 'plain text' to 'entry' in the CMS and then replaced my <a> tag code with your snippet.
    – user6999
    Commented Jul 17, 2017 at 21:27
  • So if you've only changed it from plain text to entry, you're only part way there, next you'll need to edit the entry and then select the entry that you want the button to link to, save that entry and then the code above should work. Plain text and entry store entirely different things in the database. It won't auto transform. Commented Jul 17, 2017 at 21:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.