I want to add a class to the ‘Read more’ link. Actually I have this:
<?php the_content( 'Read more...' ); ?>
Which outputs:
<a href="...?p=14#more-14" class="more-link">Read moreâ¦</a>
How can I add a class (using PHP) to the link, so it outputs:
<a href="...?p=14#more-14" class="more-link my_new_class">Read moreâ¦</a>
What (exactly) happens
When calling
the_content()
inside your template, you are able to call it without any parameters. This means, that the function already has the defaults ofnull
for both arguments: The “more” link text and the boolean switch that allows you to strip teaser content before the “more” link text.The
the_content()
function callsget_the_content()
internally (and passes the arguments in). It then runs all callback functions that are attached to thethe_content
-filter. So basically nothing that is related to the link or anything else (aside from the filter) happens insidethe_content()
– it happens insideget_the_content()
.And there we got the
the_content_more_link
-filter.The filter in core
Filters in action
The two arguments after the filter name (1st arg), are the accessible arguments inside attached callback functions.
Modify the »more«-link
Now the actual/real-life plugin:
Simply add your classes where you read
CUSTOM_CLASSES_HERE
and then upload it to your plugins directory or remove the commentPlugin Name: ...
and use it in your themes functions.php file.You can completely replace the more link using the the_content_more_link filter, just add your class in there, see “custom-class” in the code below:
You can also just add a filter to the original “more…” link, just add this code to your function.php file :
WordPress has a explenation in the codex regarding the read more…
You can find it here:
In a nutshell you can to this:
.
There you can change the class or wrap with a div etc…