Add "Read More" link to post excerpt in Classic Recent Posts element

Hi,

A few weeks ago I was able to add an excerpt to the Recent Posts element. Now I am trying to figure out how to add a “Read More” button below that excerpt.

I followed the instructions in the article recommended on this ticket, however now the page where I am displaying the “Classic Recent Posts” element has malfunctioned. There are gray boxes everywhere and everything is separated. You can see the page here (p.s. the X Recent Posts element is only visible on mobile)

Hello @SeekGod,

Thanks for writing in!

Please be advised that each of the post item in the classic recent posts element is already made as a link to the respective post items. Adding a link below each of the post items may destroy the element structure. If you have nested the <a> tag will also not work because it is not valid to nest <a> tag.

What you could do is to just place a “read more” text because it is already link along with the whole post item. Could you please provide your custom classic recent posts element so that we can check it?

Best Regards.

Ok I can do that, but then the problem is that there are other places (i.e. the Category Page itself) where the posts are being displayed via the default x theme post display. On those pages the element is NOT made a link, and thus the need for the “read more” link.

If I use this code below then it displays the “Read More” link. It looks fine on the Category page itself, but not on the custom page where I am using the Classic Recent Posts element. How can I work around this?

//display read more link when there is a manually entered exceprt
function excerpt_read_more($excerpt) {
if (has_excerpt() && !is_attachment()) {
$excerpt .= ‘… Read More’;
}
return $excerpt;
}
add_filter(‘get_the_excerpt’, ‘excerpt_read_more’);

Hi @SeekGod,

That read more code will only work on Wordpress’ default recent posts. And the shortcode that came with the theme is a different one, it’s intentionally doesn’t include a read more, nor an excerpt.

If you’re able to add the excerpt to recent posts shortcode then it’s probably customization, you can also add your read-more text in that way instead of the above code. Please do that, add the read more text add the end of excerpt code in your custom recent posts code.

Thanks!

Hi there,

yes, you’re right. That code works perfectly on the default posts listing but not on the classic recent posts element.

I’m considering just taking @RueNeI’s suggestion regarding the Classic Recent Post element. But then my question is, is there a way to use this code for the default posts listing, but to exclude it from affecting the X classic recent posts element?

This is the code that I am adding in my functions.php.

//display read more link when there is a manually entered excerpt

function excerpt_read_more($excerpt) {
    if (has_excerpt() && !is_attachment()) {
        $excerpt .= '&hellip; <a href="' . get_permalink() . '">Read More</a>';
    }
    return $excerpt;
}
add_filter('get_the_excerpt', 'excerpt_read_more');

Hi @SeekGod,

For manual excerpt, I suggest that you add the read more link together with your excerpt.

For example

Thanks

Hi Paul,

I am using the native x theme recent posts element.

I just have one last question, how can I make it so that the code I posted above does not take affect on the custom page where I am displaying posts using the native Classic Recent Posts element? I need to do this because the code is interfering with YOUR THEME element. The page id is 8743.

-Joshua

Hi @SeekGod,

Can you please update the previous code to this?

//display read more link when there is a manually entered excerpt

function excerpt_read_more($excerpt) {
    if ( is_home() && has_excerpt() && !is_attachment() ) {
        $excerpt .= '&hellip; <a href="' . get_permalink() . '">Read More</a>';
    }
    return $excerpt;
}
add_filter('get_the_excerpt', 'excerpt_read_more');

Let us know how it goes!

Hi @thai,

I tried it but now it does not add the “Read More” link. You can see the page here https://god.net/life-word/

Hi Joshua,

You can try this code instead.

//display read more link when there is a manually entered excerpt

function excerpt_read_more($excerpt) {
    if ( !is_page(8743) && has_excerpt() && !is_attachment() ) {
        $excerpt .= '&hellip; <a href="' . get_permalink() . '">Read More</a>';
    }
    return $excerpt;
}
add_filter('get_the_excerpt', 'excerpt_read_more');

While we are happy to help with the occasional quick tweak here or there, we are well into the custom development realm, which is outside the scope of support we can offer. If you need more in depth changes, you may wish to consult with a developer. X is quite extensible with child themes, so there are plenty of possibilities. Thanks for understanding.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.