• What’s the best, future-proof way to have the main page show excerpts (manual or automatically calculated by WordPress) instead of the full content?

    When I set my site up a few years ago I created a child theme, copied the entire content.php file, and altered the post-content if-statement to the following:

    if ( is_single() ) {
    the_content();
    } else {
    the_excerpt();
    }

    That worked until a recent theme update. Just wondering if there’s a better way to override just this class without having to override the entire content.php file.

    Thanks,
    Bryan

    The page I need help with: [log in to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Theme Author Anders Norén

    (@anlino)

    Hi @bdam55,

    Hemingway still uses content.php for output on single posts and pages, so your previous solution should still work post 2.0.0. I just did a quick test in my local development environment with the latest version of Hemingway to make sure.

    — Anders

    Thread Starter bdam55

    (@bdam55)

    Thanks Anders. Yup, all I had to do was re-do what I did previously: copy the new content.php file and overwrite that section. Not hard to do but I was just wondering if there’s a better way to do this that doesn’t essentially overload the entire content.php file. The answer may just be ‘no’ which is perfectly fine, I just wanted to make sure I’m doing it the ‘right’ way.

    Theme Author Anders Norén

    (@anlino)

    @bdam55 Awesome. You could add a function hooked into the_content filter to your functions.php file, and add the following conditional there:

    if ( ! is_single() ) return get_the_excerpt();

    I haven’t tested it, but I think it should work. And agreed – it’s always preferable not to overwrite template files unless it’s absolutely necessary.

    — Anders

    Thread Starter bdam55

    (@bdam55)

    Thanks for the suggestion Anders. I tried a few variations this evening but any use of get_the_excerpt() or the_excerpt() seems to send the site into a death-spiral. Even when including the in_the_loop() and is_main_query() conditionals.

    I suspect some kind of recursion is going on creating an infinite loop. After 10 seconds or so it loads the first post title/excerpt but then times out.

    I had some luck just modifying the $content string but that quickly turned into ‘reinvent the WP wheel that creates an excerpt’. I might give it another shot at some point and if I do I’ll post it here.

    Thanks again,
    Bryan

    • This reply was modified 4 years, 9 months ago by bdam55.
    Theme Author Anders Norén

    (@anlino)

    @bdam55 Yeah, I gave it a try this morning as well without much luck. I’m assuming get_the_excerpt() hooks into the the_content at some point, causing the loop, so you can get around it like this:

    function theme_filter_the_content( $content ) {
    
    	if ( ! is_single() ) {
    		remove_filter( 'the_content', 'theme_filter_the_content' );
    		return apply_filters( 'the_excerpt', get_the_excerpt() );
    		add_filter( 'the_content', 'theme_filter_the_content' );
    	}
    
    	return $content;
    
    }
    add_filter( 'the_content', 'theme_filter_the_content' );

    But the resulting excerpt doesn’t match the output of the_excerpt, so you’re probably correct in that you would need to manually apply many of filters applied to the_excerpt in order to get this approach working. It’s probably not worth the hassle.

    Sounds good!

    — Anders

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Best Way To Support Excerpts On Main Page’ is closed to new replies.