There're three filters that control the »more« link, depending on what function/Template Tag is in use. The bad thing is, that they're intercepting each other. The good thing is, that you can simply modify the output of the filter using current_filter() to retrieve the name of the currently attached filter and modify the output.
Then we got the 'excerpt_length'-filter to limit the excerpt length. This one doesn't allow us to add a permalink, but it helps us in combination with the other filters. See the 2nd plugin.
The permalink-more plugin
This plugin adds the permalink to the content or excerpt - depending on what is displayed. It also resets the excerpt_more-filter to output nothing, so it's not interfering with the other filters.
<?php
/** Plugin Name: (#69204) »kaiser« Adds a permalink to the excerpt & content */
/**
* Alters the display of the "more" link
*
* @param string $permalink
* @param string $text
* @return string $html
*/
function wpse69204_more_link( $output )
{
$html .= '<span class="post-more"> ';
$html .= sprintf(
'<a href="%s#more-%s" class="more-link" title="read more" >'
,get_permalink()
,get_the_ID()
);
$html .= '</a></span>';
// Override 'excerpt_more'
if ( 'excerpt_more' === current_filter() )
return;
// Strip the content for the `get_the_excerpt` filter.
$output = wp_trim_words( $output, 300 );
// Append for the excerpt
if ( 'get_the_excerpt' === current_filter() )
return $output.$html;
// The permalink for the `the_content_more_link`-filter.
return $html;
}
# "More" link for the content
add_filter( 'the_content_more_link', 'wpse69204_more_link' );
add_filter( 'get_the_excerpt', 'wpse69204_more_link' );
add_filter( 'excerpt_more', 'wpse69204_more_link' );
The excerpt-more length plugin
If you just want to modify the lenght of the excerpt, you can use a much simpler filter setup. The following plugin does a very nifty job. It reduces the content (we're in the loop and have post data to access) to 300 words. In the next step it counts the letters of each single word. Then it simply returns this (dynamically assigned) number.
<?php
/** Plugin Name: (#69204) »kaiser« Limit excerpt length by word count */
function wpse69204_excerpt_length( $length )
{
$to_count = array_splice( get_the_content(), 300 );
$i = 0;
foreach ( $to_count as $word )
{
$i += strlen( $word );
}
return $i;
}
add_filter( 'excerpt_length', 'wpse69204_excerpt_length' );
Notes
- Both plugins are »zero configuration«. Just upload, activate, done.
- You'll have to use
the_content() or the_excerpt() in your theme to make use of this plugins.