2

I'm building a custom child theme using the Twenty Nineteen default WordPress theme as the parent.

I added some text in the excerpt section available in the Editor as follows:

enter image description here

But the full content of each post is displayed on the blog/posts page instead of just the excerpt.


What is the proper way of adding/using excerpts in the new Gutenberg editor?

3
  • Are you trying to display only the excerpt on the front-end or in Gutenberg itself?
    – phatskat
    Feb 2, 2019 at 22:29
  • @phatskat In the front-end blog page.
    – AndrewL64
    Feb 2, 2019 at 22:30
  • One more question - is this for single pages or the blog post list page? Also, which theme are you using?
    – phatskat
    Feb 2, 2019 at 22:46

3 Answers 3

1

You should use a custom page for this. For reference, we can modify the default (twentynineteen) post content block. Depending on your theme, you may need to modify single.php, or a content block included from there. In the twentynineteen theme, we can see this on line 24:

get_template_part( 'template-parts/content/content', 'single' );

Following that, we can look into template-parts/content/content-single.php at line 23:

 23         the_content(
 24             sprintf(
 25                 wp_kses(
 26                     /* translators: %s: Name of current post. Only visible to screen readers */
 27                     __( 'Continue reading<span class="screen-reader-text"> "%s"</span>', 'twentynineteen' ),
 28                     array(
 29                         'span' => array(
 30                             'class' => array(),
 31                         ),
 32                     )
 33                 ),
 34                 get_the_title()
 35             )
 36         );
 37  

So, you could replace this call to the_content with something like this:

the_excerpt(); // Echo the excerpt directly.
$excerpt = get_the_excerpt(); // Store the excerpt in $excerpt.

// Retrieve the "raw" exceprt - this has not passed any filters etc,
// and instead comes directly from the database.
$post = get_post();
echo $post->post_excerpt;
1

The twentynineteen front page is controlled by the file /wp-content/themes/twentynineteen/index.php. It contains the code

while (have_posts()) {
  the_post();
  get_template_part('template-parts/content/content');
}

This code applies the template from the .php file in template-parts/content/content.php. (Note template-parts/content is a sub-directory of the theme directory) Change content to content-excerpt as follows

while (have_posts()) {
  the_post();
  get_template_part('template-parts/content/content-excerpt');
}

Now the .php file template-parts/content/content-excerpt.php is loaded instead, which is what you want

0

you need modify your template, in your PHP change the_content() to the_excerpt()

This php functions print the content and the excerpt respectively

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.