With this WordPress related PHP-OOP Answer, I found my solution for what I's seeking. But with the same function my_excerpt();
I want to pass another anchor tag (<a>
) inside the <p>
tag.
Right now, calling my_excerpt()
is embracing the db texts with a paragraph tag (<p>here comes the excerpt</p>
). And if I add my anchor tag like the following:
// Echoes out the excerpt
public static function output() {
the_excerpt(); ?>
<a class="read-more" href="<?php the_permalink(); ?>" title="<?php the_title(); ?>" rel="bookmark"><?php _e( 'Read More »', 'my-theme'); ?></a>
<?php
}
it's showing the "Read More" just the bottom of the except texts. Inspect Element shows like the following:
<p>here comes the excerpt.</p>
<a href="post-link">Read More</a>
How can I make changes to the function or class so that I can have the "Read More" link inside the paragraph, like:
<p>here comes the excerpt.<a href="post-link">Read More</a></p>
Additionally I'm also trying to remove the [...]
part from the excerpt generating by the my_excerpt();
. So I tried the following:
function change_excerpt($content) {
$content = str_replace( '[...]','>',$content ); // remove [...], replace with ...
$content = strip_tags( $content ); // remove HTML
return $content;
}
add_filter('my_excerpt','change_excerpt');
I doesn't do anything to the view. But if I change it to:
add_filter('the_excerpt','change_excerpt');
Then unaware, I get the previously desired anchor tag [inside paragraph], because the filter removed the paragraph tag completely.
here comes the excerpt.<a href="post-link">Read More</a>
But it doesn't do anything with [...]
portion. :(
So, my furnished Question is:
How can I put the anchor tag inside the paragraph tag, or remove the paragraph tag, and remove the [...]
part from the my_excerpt()
function?
try this snippet
Include in your functions.php
function kc_excerpt( $length_callback = '', $more_callback = '' ) {
if ( function_exists( $length_callback ) )
add_filter( 'excerpt_length', $length_callback );
if ( function_exists( $more_callback ) )
add_filter( 'excerpt_more', $more_callback );
$output = get_the_excerpt();
$output = apply_filters( 'wptexturize', $output );
$output = apply_filters( 'convert_chars', $output );
$output = '<p>' . $output . '</p>';
echo $output;
}
function kc_excerpt_more( $more ) {
return '<a class="read-more" href="'. get_permalink() .'" title="'. get_the_title() .'" rel="bookmark">Read More</a>';
}
function kc_excerpt_more_2($more) {
return '...';
}
function kc_exdefault($length) {
return 10;
}
function kc_ex100($length) {
return 100;
}
And call this function from your template file
<?php kc_excerpt('kc_exdefault', 'kc_excerpt_more'); ?>
or
<?php kc_excerpt('kc_ex100', 'kc_excerpt_more_2'); ?>