Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

The code from the RichText documentation appends the 'Continue reading…' link in a new block and on a new line:

{% for entry in craft.entries.section('blog') %}
        <a href="{{ entry.url }}">{{ entry.title }}</a>
        {{ entry.richTextFieldHandle.getPage(1) }}
        {% if entry.richTextFieldHandle.getTotalPages() > 1 %}
            <a href="{{ entry.url }}">Continue reading…</a>
        {% endif %}
{% endfor %}

Any ideas how to append the 'Continue reading…' link inside the final </p> tag generated by Redactor at the end of the final paragraph, i.e. as a continuation of the Excerpt text, and on the same line?

share|improve this question

3 Answers 3

up vote 4 down vote accepted

I'm a total novice with Craft/Twig and I was hoping for some sort of simple and neat append function in Twig!

However, I've got close to a working solution using CSS, by overriding the Extract's <p> tags' usual behavior with display: inline, and enclosing the 'Continue reading…' in a <span> tag.

Edit:
The following (including some additional logic for a 'Summary' field) seems to work well…

<div class="excerpts">

    {% if entry.summary %}
        {{ entry.summary }}
    {% else %}
        {{ entry.body.page(1) }}
    {% endif %}

    {% if entry.body.getTotalPages() > 1 or entry.summary %}
        <span><a href="{{ entry.url }}" rel="nofollow">Continue reading…</a></span>         
    {% endif %}

</div>

…and…

.excerpts p:last-of-type { display: inline; }
share|improve this answer
    
THIS is the way to go, Brian! I was so much in my Craft thinking that I didn't understand the problem as a whole. All you actually want to do is to STYLE the "continue reading..." link. And as we all know, styling should be done using css, semantically the link belongs outside of your paragraphs. Additionally I'd add rel="nofollow" to the link, as you don't want to rank for "Continue reading" at search engines. –  carlcs Jul 29 at 15:41
    
Oh, and it's p:last-of-type { display: inline; }... –  carlcs Jul 29 at 16:03
    
Thanks Christian! –  Brian L Jul 29 at 16:19
    
Now you only have to wait 2 days before you can accept your answer :D In the meantime, you could edit it to make it even more useful for future readers (add a complete css code snippet etc.). –  carlcs Jul 29 at 16:29

I would write a Craft plugin with a twig extension.

{% if entry.richTextFieldHandle.getTotalPages() > 1 %}
  {{ entry.richTextFieldHandle.getPage(1)|continueReading('Continue reading…', entry.url)|raw }}
{% else %}
  {{ entry.richTextFieldHandle }}
{% endif %}

continueReading plugin left as an exercise for the reader.

share|improve this answer
    
Marion, what about making continueReading an exercise for the answerer? –  carlcs Jul 29 at 14:50
    
@ChristianSeelbach quite possible, but no promises. –  Marion Newlevant Jul 29 at 14:52
    
YEY! Redactor is no easy to deal with. Still struggling on how to add AND remove inline tags with a custom button. –  carlcs Jul 29 at 15:00

To solve this properly you'd need to write a Craft plugin which also holds some sort of Redactor plugin.

But as a quick solution, how about using the striptags filter? It'll remove all your tags though, meaning that you will also loose all other paragraph and styling tags.

{#
 # This comment is only necessary because of this bug:
 # http://meta.craftcms.stackexchange.com/questions/72/temporary-removal-of-twig-syntax-highlighting
 #}

<p>
    {{ entry.richTextFieldHandle.getPage(1)|striptags }}

    {% if entry.richTextFieldHandle.getTotalPages() > 1 %}
        <a href="{{ entry.url }}">Continue reading…</a>
    {% endif %}
</p>

You could also try to apply slice filters. I'm not sure, but maybe this automatically leads to a raw output of your rich text, making all other tags not working as well.

{#
 # This comment is only necessary because of this bug:
 # http://meta.craftcms.stackexchange.com/questions/72/temporary-removal-of-twig-syntax-highlighting
 #}

<p>
    {{ entry.richTextFieldHandle.getPage(1)|slice(4)|slice(0, -4) }}

    {% if entry.richTextFieldHandle.getTotalPages() > 1 %}
        <a href="{{ entry.url }}">Continue reading…</a>
    {% endif %}
</p>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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