Editing your theme file is unlikely to work, but it depends on your theme. Most themes get the read more link through the_content()
or the_excerpt()
. Both of these have filters where you could add an onclick attribute the generated link.
You could probably also accomplish this through jQuery. There’s likely some sort of jQuery selector that isolates the read more link. Add the .click
method to that selection.
Your initial post made sense to me, you could use the is_home()
template tag in the filter callback to decide whether to insert the onclick attribute or not. Adding it instead to the single post like you ask in your second post has me a bit confused. Single posts normally do not have read more links. Do you mean an archive page, such as search results, where only one post is found?
If the latter, you can get the number of posts returned from the global $wp_query
object. You should first check $wp_query->is_main_query()
to avoid adding onclick to the wrong items, like widget excerpts. There are other template tags you can check, like is_archive()
and is_search()
to determine context.