godaddy-wordpress / primer-child-escapade

Escapade is a Primer child theme with a unique sidebar navigation.
12 stars 9 forks source link

How to adjust the auto preview for the stream on the homepage #79

Closed tylerregas closed 5 years ago

tylerregas commented 5 years ago

This really looks great, but the summary is way too short and unbalances the entire layout. How can I adjust the number of characters that are displayed or just switch the theme back to standard MORE tag functionality? I poked around the code, but can't find the function that handles this. TIA!

image

EvanHerman commented 5 years ago

Hi @tylerregas

If you take a look at the parent theme (Primer) there is a snippet of code inside of inc/hooks.php, which sets the excerpt length. In the doc block above that function you'll see a paragraph about overriding the excerpt length and setting your own custom length.

/**
 * Remove Primer hooks and setup custom hooks
 */
function remove_primer_hooks() {

    remove_filter( 'excerpt_length', 'primer_excerpt_length' );

    /**
     * Set a custom excerpt length
     */
    add_filter( 'excerpt_length', function() {

        return 100;

    } );

}
add_action( 'init', 'remove_primer_hooks' );

You'll want to add that snippet into a custom MU plugin that you create. We have a tutorial on setting up an MU plugin on your site here: https://godaddy.github.io/wp-primer-theme/tutorials-and-examples/tutorials/mu-plugin.html.

Once added into the MU plugin you should see the excerpt lengths will increase on your blog post page. You can tweak the 100 value in the function above to tweak the length of your excerpts. 100 = 100 words.

Let us know if that helps!

tylerregas commented 5 years ago

Ah! THERE it is :) THANKS A MILLION!!!

EvanHerman commented 5 years ago

No problem at all - you are very welcome! Have a wonderful week!

tylerregas commented 5 years ago

Well, it took this knuckle dragger (me) a few days to get around to this, and when I looked, I couldn't find the code. That's likely because I didn't realize that the PRIMER theme is the parent to the ESCAPADE child theme, which is probably why it's got very little code in the theme files.

Duh.

So, I found the snippet and started wondering what child themes had to do with Multi-User (MU) functionality, which is when I discovered Automattic's amazingly weird choice to use the MU acronym for Must-Use plug-ins, something I had only seen added by other plug-ins.

Sooooo, long story short, I've modified the Primer code for the mo, then I'll get the child mod set, and then I'll try to figure out how to remove the auto summary and replace it with classic content, something I've never had to do. Everything about Primer and Escapade is really fantastic, except the excerpting. I really prefer the classic use of the More tag to control initial content.

If anyone has any ideas on how that might work, drop me a line :) Thanks!!

EvanHerman commented 5 years ago

Hi @tylerregas

Using the <!--more--> tag is a bit different with the new block editor. You can't just drop the <!--more--> tag into the post editor, and it also requires swapping out the_excerpt() for the_content() inside of primer/templates/parts/loop.php. Reference

I think that the easiest way to implement what you are looking for is to alter the snippet that I provided in the previous response.

/**
 * Remove Primer hooks and setup custom hooks
 */
function remove_primer_hooks() {

    remove_filter( 'excerpt_length', 'primer_excerpt_length' );

    /**
     * Mock swap the_excerpt for the_content
     */
    add_filter( 'the_excerpt', function( $excerpt ) {

        return get_the_content();

    } );

    /**
     * Hide the (more) link generated by the More block
     */
    add_filter( 'the_content_more_link', '__return_null' );

}
add_action( 'init', 'remove_primer_hooks' );

In the example above, instead of filtering excerpt_length as we did in the previous example, we're filtering the entire excerpt using the_excerpt filter and returning the entire post content with the get_the_content() function. Beneath that we're hiding the custom link generated by the More block.

That should allow you to use the More block provided by WordPress core to have more control over where your excerpts are trimmed.

More Block

More Block

Let me know if that helps!

Evan

tylerregas commented 5 years ago

Thanks, Evan! I'll give it a go tomorrow when I'm not running around like a chicken with my head cut off :) That's such a horrible visual metaphor.