0

I use a function to modify the 'read more' link for posts.

function modify_read_more_link() {
return '<p class="read-more"><a href="' . get_permalink() . '">CONTINUE READING &raquo</a></p>';
}
add_filter( 'the_content_more_link', 'modify_read_more_link' );

I want to use different text for the feed, and wonder whether this can be done using a conditional if is_feed() tag?

(I don't know enough, and my experiments either don't work or halt page loads.)

2 Answers 2

1

These are some tricks:

 /* Modify the read more link on the_excerpt() */

 function et_excerpt_length($length) {
 return 220;
 }
add_filter('excerpt_length', 'et_excerpt_length');

/* Add a link  to the end of our excerpt contained in a div for styling 
purposes and to break to a new line on the page.*/

 function et_excerpt_more($more) {
 global $post;
 return '<div class="view-full-post"><a href="'. get_permalink($post->ID) . 
'" class="view-full-post-btn">View Full Post</a></div>;';
}
add_filter('excerpt_more', 'et_excerpt_more');

Thank you

1
  • Thanks. I think that'll apply to onsite posts as well as feed.
    – glvr
    May 20, 2017 at 6:55
0

Default WP lets you choose between the excerpt or the full post content to include in the feed (under Settings -> Reading). There's no option to include the content up to the more-link, unless you have some plugin that does this for you. Hence, it's no use trying to change the more-link text for the feed, because it will be ignored.

So, if you want your feed to include the_content up to the read more text, you will have to build it yourself using a filter, like this:

add_filter ('the_content', 'wpse267435_readmore', 1, 10);
function wpse267435_readmore ($content) {
    if (is_feed()) {
        ... do stuff with $content ...
       }
    return $content;
    }

Now, doing stuff with content involves cutting it off at <!--more-->, assuming you are not using any more complex tags. This is straightforward PHP:

$cut_off = strpos ($content,'<!--more-->');
$content = substr ($content, 0, $cut_off);

Now you can add any read more link you want:

$content = $content . '<a href="....">My special link </a>';

Beware 1 Feeds are xml files. In principle html anchor tags are invalid xml. Most browsers will render your link correctly, but they may also generate their own links based on other tags in the xml file.

Beware 2 Normally the filter should process before any shortcodes are evaluated. But if you have a plugin or theme messing with priorities the '' may already be gone by the time you are trying to find it in the_content. In that case, lower the priority on the filter.

3
  • Its a fine example. I would like to add that the name of the function and the second argument you send to add_filter should be the same. May 19, 2017 at 12:18
  • @JesperNilsson You are completely right. Changed it.
    – cjbj
    May 19, 2017 at 12:31
  • @cjbj... Thanks for the comprehensive info. My modified theme is currently respecting the <!--more--> tag, and when set to 'full text' is displaying the 'read more' text set in the filter in my question. Clearly this is due to an earlier mod (although I'm not sure just why, and haven't yet found just what I changed). I normally use a hand-edited excerpt with a function to add a 'read onsite' link, and hence wasn't aware of the <!--more--> tag not being available in feeds.
    – glvr
    May 20, 2017 at 7:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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