• Resolved Ashley Michèlle

    (@ashleymichelle)


    This is my current functions.php custom function:

    function custom_excerpt($text) {  // custom 'read more' link
       if (strpos($text, '[...]')) {
          $excerpt = strip_tags(str_replace('[...]', '<div class="readmore"><a class="readmore" href="'. get_permalink( get_the_ID() ) . '"><small>CONTINUE READING</small></a></div>', $text), "<a>");
       } else {
          $excerpt = '' . strip_tags($text) . '<div class="readmore"><a class="readmore" href="'. get_permalink( get_the_ID() ) . '"><small>CONTINUE READING</small></a></div>';
       }
       return $excerpt;
    }
    add_filter('the_excerpt', 'custom_excerpt');

    You can see it in action here: https://ohmybonbon.com.

    I would like to see only the manual excerpts displayed, and if there is no manual excerpt, I would like it to only display the ‘continue reading’ link, rather than an automatic excerpt (as it is currently doing).

    I’m sure this is probably incredibly easy, but my brain isn’t working. So, of course, help is greatly appreciated!

Viewing 7 replies - 1 through 7 (of 7 total)
  • Change this line:

    $excerpt = '' . strip_tags($text) . '<div class="readmore"><a class="readmore" href="'. get_permalink( get_the_ID() ) . '"><small>CONTINUE READING</small></a></div>';

    to this:

    $excerpt = '<div class="readmore"><a class="readmore" href="'. get_permalink( get_the_ID() ) . '"><small>CONTINUE READING</small></a></div>';

    Thread Starter Ashley Michèlle

    (@ashleymichelle)

    Thanks PhPCentre! The only problem with that is, now it doesn’t display the manually set excerpt… any way to work around this?

    I hope you did not replace the entire code in the function with the line of code I suggested above. Your finally code should like this:

    function custom_excerpt($text) {  // custom 'read more' link
       if (strpos($text, '[...]')) {
          $excerpt = strip_tags(str_replace('[...]', '<div class="readmore"><a class="readmore" href="'. get_permalink( get_the_ID() ) . '"><small>CONTINUE READING</small></a></div>', $text), "<a>");
       } else {
          $excerpt = '<div class="readmore"><a class="readmore" href="'. get_permalink( get_the_ID() ) . '"><small>CONTINUE READING</small></a></div>';
       }
       return $excerpt;
    }
    add_filter('the_excerpt', 'custom_excerpt');

    On second thought, this is what you will do. While saving your manual excerpt in the post edit screen, type for example ‘[..]’ at the end of the excerpts, Do not use the string ‘[…]` as it will be interpreted as a symbol or any other string that will also be interpreted as a symbol. Then update the last codes I posted replacing the two occurrence of string'[…]’ with ‘[..]’. It ‘s working on my end.

    Thread Starter Ashley Michèlle

    (@ashleymichelle)

    Is there not a way to have this completed entirely in the function, rather than requiring a manual entry of [..]?

    Basically, I want it to be something like ‘if post has custom excerpt, displays custom excerpt & read more link (as I have in my original code), and if post has no custom excerpt, display no except, only read more link’.

    Then use this instead:

    function custom_excerpt($text) {  // custom 'read more' link
    	global $post;
    	if ( !empty( $post->post_excerpt ) ) {
    		$excerpt = '' . strip_tags($text) . '<div class="readmore"><a class="readmore" href="'. get_permalink( get_the_ID() ) . '"><small>CONTINUE READING</small></a></div>';
    	} else {
    		$excerpt = '<div class="readmore"><a class="readmore" href="'. get_permalink( get_the_ID() ) . '"><small>CONTINUE READING</small></a></div>';
    	}
    	return $excerpt;
    }
    add_filter('the_excerpt', 'custom_excerpt');

    Thread Starter Ashley Michèlle

    (@ashleymichelle)

    Thank you SO much, PhPCentre! This is exactly what I was after!

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Display only manual excerpt on index (function).’ is closed to new replies.