• The theme I’ve been using had a hero banner on posts that used the manual excerpt under the title as an option. A different theme I switched to uses a “page header description” instead, so my need is to have the manual excerpts move over to the “page header description” spot. I have a code function to make that work, but I need some advice.

    The code works, except that if there is no manual excerpt on a post, the code pulls up the beginning of the post up to about 200 characters. I want posts that don’t have a manual except to be blank under the title. Here is the code I’m using, and I’d like to know how it could state “if the excerpt is manual, show it; if there’s no manual excerpt, then add nothing”. Thanks for any help you can give. I’ve tried several things, but the only thing I get is either the manual excerpt or the opening of the post on all posts.

    /**
     * Use the excerpt in the page header.
     * 
     * @param string $description The existing description.
     * 
     * @return string
     */
    add_filter( 'mai_page_header_description', function( $description ) {
    	if ( is_singular() || ! has_excerpt() ) {
    		$description .= get_the_excerpt( get_the_ID() );
    	}
    
    	return $description;
    });
Viewing 2 replies - 1 through 2 (of 2 total)
  • I think you need to remove is_singular() from your condition, because that’s always true on the single post.

    The OR || condition evaluates from left to right. When it finds that is_singular() is true, it doesn’t need to check whether !has_excerpt() is true. It currently always executes get_the_excerpt() which falls back to trimmed post content when it finds no excerpt.

    You also would want to remove the ! before has_excerpt(). Leaves you with “if the post has an excerpt, display it.”

    if (has_excerpt()) {
      $description .= get_the_excerpt(get_the_ID());
    }
    Thread Starter David Borrink

    (@davidborrink)

    That did it, Ricky. Thank you for the explanation of how the parts work. It’s one thing to see examples, but if you don’t know WHY they interact or that something isn’t necessary, it’s hard to know unless someone tells you. I really appreciate you taking the time to do that.

    – David

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Trying to show only manual excerpts, not auto excerpts’ is closed to new replies.