如何在WordPress中不裁剪调整文章摘要图片尺寸?
Got it, let's sort out this image cropping issue for your blog's excerpt section. The code snippet you started points to a theme setup function, so we can extend that to get the exact cropped thumbnail size you need. Here's a step-by-step solution:
1. Complete the Custom Image Size Registration
First, finish the function you started to register a custom cropped image size specifically for your excerpt thumbnails. Adjust the width/height values to match what you need (I used 200x150 as an example—tweak these numbers to fit your design):
if ( !function_exists( 'writer_ancora_template_excerpt_theme_setup' ) ) { add_action( 'writer_ancora_action_before_init_theme', 'writer_ancora_template_excerpt_theme_setup', 1 ); function writer_ancora_template_excerpt_theme_setup() { // Register a hard-cropped image size for excerpt thumbnails add_image_size( 'excerpt-thumbnail', 200, 150, true ); // Parameters: Size name, width, height, hard crop (true = strict crop, false = proportional resize) } }
2. Update the Excerpt Template to Use the New Size
Next, find the template file that renders your blog excerpts (this is usually something like content-excerpt.php, template-parts/post/excerpt.php, or similar in your theme's folder). Look for the line that displays the post thumbnail—it might look like this:
the_post_thumbnail( 'medium' ); // Or another default size like 'thumbnail'
Replace that line with your new custom size:
the_post_thumbnail( 'excerpt-thumbnail' );
3. Regenerate Thumbnails (For Existing Images)
If you already have posts with images uploaded, those images won't automatically get the new cropped size. You'll need to use a thumbnail regeneration tool (most WordPress users use a popular plugin for this—just search for "regenerate thumbnails" in the plugin repository) to create the new size for all existing images.
Bonus: Check Theme Customizer First
Quick note: Some ThemeForest themes let you adjust excerpt thumbnail sizes directly in the WordPress Customizer (under Appearance > Customize > Blog or Post Settings). If your theme has this option, you can skip the code changes and just tweak the settings there—it's faster and safer!
内容的提问来源于stack exchange,提问作者Kai Van Landschoot

