admin管理员组

文章数量:1410697

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:

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?

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:

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?

Share Improve this question asked Feb 2, 2019 at 21:56 AndrewL64AndrewL64 2034 silver badges18 bronze badges 3
  • Are you trying to display only the excerpt on the front-end or in Gutenberg itself? – phatskat Commented Feb 2, 2019 at 22:29
  • @phatskat In the front-end blog page. – AndrewL64 Commented 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 Commented Feb 2, 2019 at 22:46
Add a comment  | 

3 Answers 3

Reset to default 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;

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

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

This php functions print the content and the excerpt respectively

本文标签: block editorHow to display only the excerpt in the blogposts page with Gutenberg