在WordPress中针对特定文章ID禁用Jetpack Carousel遇阻求助
is_page() Isn't Working in Your functions.php Code for Disabling Jetpack Carousel Great question! Let’s break down why this is happening and how to fix it. The core issue here is timing—functions like is_page() depend on WordPress’s global query being fully initialized, and if you’re running your code too early in the load sequence (like directly in functions.php without hooking it to a later action), the query hasn’t been set up yet. That’s why even wp_reset_query() doesn’t help—there’s no query to reset at that point!
Jetpack’s jp_carousel_maybe_disable filter needs to be hooked at a point where we can reliably check the current post/page ID. Here’s how to resolve this:
Step 1: Hook Your Code to the Right Action
Instead of running your code directly in functions.php, attach it to an action that fires after the main query is initialized. Solid choices are wp or template_redirect—both run after WordPress has parsed the request and set up the global query.
Step 2: Check the Current Post ID Directly
Instead of relying solely on is_page(), use get_queried_object() to fetch the current post/page object and verify its ID. This is more reliable in contexts where conditional tags might behave unexpectedly.
Example Working Code
Drop this complete snippet into your functions.php:
add_action( 'wp', 'disable_jetpack_carousel_for_specific_page' ); function disable_jetpack_carousel_for_specific_page() { // Replace 123 with your target page/post ID $target_id = 123; // Get the current queried object $current_post = get_queried_object(); // Check if we're on the target page/post if ( $current_post && $current_post->ID === $target_id ) { add_filter( 'jp_carousel_maybe_disable', '__return_true' ); } }
Why This Works:
- The
wpaction fires after WordPress loads the main query, soget_queried_object()correctly returns the current page/post. - We only add the
jp_carousel_maybe_disablefilter when we’re on the exact page we want to target, ensuring the carousel is disabled only there. - To target multiple IDs, adjust the check to use
in_array( $current_post->ID, [123, 456, 789] )instead.
Why wp_reset_query() Didn’t Help
wp_reset_query() is meant to restore the global $wp_query after running a custom query (like new WP_Query()). Since your problem was that the main query hadn’t been initialized yet, resetting it does nothing—there was no query state to revert to.
内容的提问来源于stack exchange,提问作者rmbaumer

