Forum
Conditionally output "Read more" depending on word count
Hello,
Is it possible to conditionally output something based on the word count of the field?
For example, on a "list" page I am outputting a truncated bio of a profile like this:
<perch:content id="biography" words="42" append=" …"/>
This is fine, but I would like to add a "read more" link only if the word count is over the limit of 42. For instance, like this:
<perch:content id="biography" words="42" append=" <a href="/some-page/<perch:content id="slug" type="slug" />">View Full Profile</a>"/>
Obviously the above does not work because it's outputting as a string, but it demos what I would like to happen.
Is something like this possible?
I think you'd to write a template filter to do that.
Thanks, I have it working except I'm not sure how to wrap a link from the HTML template around the "read more" text. Is this possible?
So this is the template filter working, without the link, in
filters.php
…And I want it to do something like this (but this obviously doesn't work)…
Whoops, found the instruction "Accessing the template tag" in the documentation.
I now have it working except the the output does not seem to be returning as markup, despite setting this to "true". So it's now outputting rather literally as
sometext here <a href="somelink">…read more</a>
rather than actually outputting the anchor markup. I'll keep working on this and post a solution once I crack it.You should set mark up to true like this
Thanks Dexter, I've set that variable but it doesn't help (see below).
I'm also confused at why this variable exists alongside the
$valueIsMarkup
variable insidepublic function filterBeforeProcessing($value, $valueIsMarkup = false)
Hi Jay you'd do it like so: (This works for me anyway)
That's awesome. Works for me too. Thanks so much!