Slick Slider 最新文章循环轮播功能无法正常运行求助
Hey there, let's figure out why your latest posts carousel is breaking when you switch from static content to a WP_Query loop. White screens in WordPress usually stem from uncaught PHP errors, so let's break down the most likely culprits and fix them step by step.
1. First: Turn on PHP Error Reporting
Before diving into code, enable debug mode to see exactly what's causing the crash. Open your wp-config.php file and add these lines right above /* That's all, stop editing! Happy publishing. */:
define('WP_DEBUG', true); define('WP_DEBUG_LOG', true); define('WP_DEBUG_DISPLAY', true);
This will show you the exact error message instead of a blank screen—super helpful for pinpointing issues.
2. Fix Undefined Custom Functions
Looking at your code, you're using the_post_custom_thumbnail()—this is a custom function from the Aquila theme that doesn't exist in your current setup. That's almost certainly throwing a fatal error and causing the white screen.
Replace that custom function with WordPress's native the_post_thumbnail() instead. Here's your revised code with that fix:
<?php $args = [ 'posts_per_page' => 2, 'post_type' => 'post', ]; $post_query = new WP_Query( $args ); ?> <div class="posts-carousel px-5"> <?php if ( $post_query->have_posts() ) : while ( $post_query->have_posts() ) : $post_query->the_post(); ?> <div class="card"> <?php if ( has_post_thumbnail() ) { // Use native WordPress function instead of Aquila's custom one the_post_thumbnail( 'featured-thumbnail', [ 'sizes' => '(max-width: 350px) 350px, 233px', 'class' => 'w-100', ] ); } else { ?> <img src="https://via.placeholder.com/510x340" class="w-100" alt="Card image cap"> <?php } ?> <div class="card-body"> <?php the_title( '<h3 class="card-title">', '</h3>' ); ?> </div> </div> <?php endwhile; endif; wp_reset_postdata(); ?> </div>
3. Check for Missing Image Sizes
If you still run into issues, make sure your theme supports the featured-thumbnail image size. If it doesn't, you can either:
- Replace
'featured-thumbnail'with a default size like'medium'or'large' - Add this code to your theme's
functions.phpto register the size:add_action( 'after_setup_theme', 'register_custom_image_sizes' ); function register_custom_image_sizes() { add_image_size( 'featured-thumbnail', 350, 233, true ); // Width, Height, Crop }
4. Double-Check WP_Query Syntax
Your WP_Query arguments look correct, but just to be safe:
- Ensure all quotes and commas are properly placed (no missing closing brackets or semicolons)
- If you're using a custom post type later, verify the slug is correct (right now you're using
'post'which is default, so that's fine)
Final Notes
Once you make these changes, your carousel should load without the white screen. If you still have issues, check the debug log (found in wp-content/debug.log) for specific error messages—they'll point you straight to the problem.
内容的提问来源于stack exchange,提问作者Mr Toad

