WordPress Type主题:最新文章显完整内容其余显摘要求助
Hey there! Since you’re solid with HTML/CSS but still getting the hang of PHP, let’s walk through a straightforward fix for this issue—no messy syntax errors, promise.
Step 1: Add a Post Counter to Your Index Loop
First, we need to track which post we’re on in the homepage loop. Open your index.php file and find the start of the WordPress loop (it’ll look like while ( have_posts() ) : the_post();). We’ll initialize a counter right before the loop, then increment it inside the loop:
<?php // Initialize post counter BEFORE the loop $post_count = 0; while ( have_posts() ) : the_post(); // Increment counter for each post $post_count++; // Load the content-list template get_template_part( 'content', 'list' ); endwhile; ?>
Step 2: Modify content-list.php to Show Full Content for the First Post
Next, open content-list.php and find the line where it outputs the post content (either the_excerpt() or the_content()). Replace that line with this conditional check:
<?php // Check if we're on the homepage AND this is the first post if ( is_front_page() && $post_count === 1 ) { // Show full content for the first post the_content(); } else { // Show excerpt for all other posts the_excerpt(); } ?>
Why This Works
- The
$post_countvariable keeps track of which post we’re rendering in the loop. - The
is_front_page()check ensures this only applies to your homepage (so pagination pages or other archive pages won’t accidentally show full content for their first post). - We’re using strict equality (
===) to avoid any weird type mismatch issues with PHP.
Quick Notes
- Always use a child theme: If you modify the parent theme files directly, your changes will be lost when the theme updates. Create a child theme first, then copy
index.phpandcontent-list.phpinto it to make your edits. - Troubleshooting syntax errors: If you ran into issues before, double-check that you’re wrapping PHP code in
<?php ?>tags properly, and that the counter variable is initialized outside the loop (not inside).
That should do it! Your homepage’s latest post will show full content, and every other post will stick to the excerpt.
内容的提问来源于stack exchange,提问作者Rini CS

