2

Site design I am working on the first word of ever page title is bold. The code looks like this;

<h1 class="section__heading"><strong>Discover</strong> Riaflex Today</h1>

I am struggling to find a simple solution to do this in Craft.

If I setup a simple rich text field then it wraps "p" tags around it. I have tried to used Markdown but this is the same and also end up with ** in the <'title'> output.

Anyone done something similar or can offer a fairly simple approach. I had a quick attempt at editing radactor but dropped it as it didn't seem to work and remove 'p' tags.

Thanks.

8
  • Why don't you use a simple text field?
    – KSPR
    Commented Aug 18, 2016 at 11:46
  • Do you mean a plain text field? If you do then this is because it won't allow the user to bold the first word. Commented Aug 18, 2016 at 11:48
  • Yep. How about a separate plain text field for the title and then a rich text field for the rest?
    – KSPR
    Commented Aug 18, 2016 at 11:50
  • The problem is that the title needs to have the first word bold. Commented Aug 18, 2016 at 11:57
  • Ah. now I get it.
    – KSPR
    Commented Aug 18, 2016 at 11:59

1 Answer 1

5

You can grab the first word with the split filter according to this thread

{% set array = entry.title|split(' ', 2) %}
<h1 class="section__heading">
   <strong>{{ attribute(array, 0) }}</strong>
   {{ attribute(array, 1) }}
</h1>

I didn't test this tough.

Edit: there is a Twig Fiddle demonstrating it.

1
  • Yes thanks KSPR. I found that solution shortly before you posted. Seems to work well. Commented Aug 18, 2016 at 12:51

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.