Support » Fixing WordPress » Put a "read more" link inside custom excerpts

  • Hello! I’m a WP and PHP newbie, so my question might be obvious, but I spent hours trying to find a solution to my problem and I can’t get it to work properly.
    My problem: I’m working on a Child Theme, and I set-up everything in my theme’s functions.php file so that if an excerpt is created automatically, then a properly formatted “read more” link is displayed. In my functions.php file I have:

    // Replaces the excerpt "more" text by a link
    function new_excerpt_more($more) {
           global $post;
    	return '<a class="moretag" href="'. get_permalink($post->ID) . '"> <span class="">[' .__( 'Read more', 'mychildtheme' ).' &raquo;]</span></a>';
    add_filter('excerpt_more', 'new_excerpt_more');

    So far so good. But I want to use automatic excerpts as a fallback, I like writing custom excerpts and I want both (automatic and custom) excerpts to have the same styling. On my home page I have the following php code:

    <?php if ( ! has_excerpt() ) {
    	} else {
          	echo '<a class="moretag" href="'. get_permalink($post->ID) . '"> <span class="">[' .__( 'Read more', 'mychildtheme' ).' &raquo;]</span></a>';}; ?>

    The problem is that when I have an automatic excerpt, then my “read more” link is inside the <p> tag, like this:
    <p>this is an automatic excerpt blah blah blah [Read more]</p>
    But when I use custom excerpt my link is outside the last paragraph, like this:
    <p>this is a custom excerpt blah blah blah</p><a class"moretag"...
    I really would like to have them both inside the last paragraph, possibily preceded by something like (…).

    If you could point me to the right direction I’d be happy to learn how to work with the_excerpt

  • The topic ‘Put a "read more" link inside custom excerpts’ is closed to new replies.