You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress开启「显示完整文章」时如何隐藏首页视频?

Hey there! Let's figure out how to fix this—you want your WordPress homepage to show full posts, but those embedded videos are killing load times. We need to hide the videos on the homepage only, while keeping them fully visible on individual post pages. Here are three solid approaches to get this done:

1. Use a PHP Filter (Best for Performance)

This method modifies the post content before it loads on the homepage, completely removing the video code so it doesn't even load. Perfect for real speed gains.

You'll need to add this code to your child theme's functions.php file, or use a code snippets plugin (like Code Snippets) to avoid losing changes when your theme updates:

add_filter( 'the_content', 'hide_homepage_videos' );
function hide_homepage_videos( $content ) {
    // Check if we're on the main blog page or static front page
    if ( is_home() || is_front_page() ) {
        // Remove iframe embeds (adjust regex if your videos use different markup)
        $content = preg_replace( '/<iframe[^>]*>.*?<\/iframe>/i', '', $content );
        // Remove native HTML5 video tags if you use those too
        $content = preg_replace( '/<video[^>]*>.*?<\/video>/i', '', $content );
        // Optional: Add a friendly placeholder so visitors know there's a video
        $content .= '<p style="color:#666; font-style:italic;">[Video hidden on homepage — click the post title to watch]</p>';
    }
    return $content;
}
  • Pro tip: If your videos use custom classes or embed wrappers (like YouTube shorts in a <div class="post-video">), tweak the regex patterns to target that specific markup instead.
2. Wrap Videos in a Custom Shortcode

This gives you granular control over which videos to hide—great if you only want to exclude specific videos, not all of them.

First, add this code to your functions.php or code snippets plugin:

function conditional_video_shortcode( $atts, $content = null ) {
    // Show the video only if we're NOT on the homepage
    if ( !is_home() && !is_front_page() ) {
        return $content;
    }
    // On homepage, show a helpful placeholder instead
    return '<p style="padding:15px; background:#f8f9fa; border-radius:4px;">Click to view the full post and watch the video!</p>';
}
add_shortcode( 'conditional_video', 'conditional_video_shortcode' );

Then, when editing your posts, wrap your video embed code like this:

[conditional_video]
<iframe width="560" height="315" src="https://www.youtube.com/embed/your-video-id" frameborder="0" allowfullscreen></iframe>
[/conditional_video]
  • Bonus: Customize the placeholder text or styling to match your theme's look and feel.
3. CSS Hack (Quick, But Not Ideal for Performance)

This method visually hides videos on the homepage, but they'll still load in the background—so it won't help with load speed much. Use this only as a temporary fix or if you can't edit PHP.

Add this CSS to your theme's customizer (Appearance > Customize > Additional CSS):

/* Hide videos on homepage */
.home .entry-content iframe,
.home .entry-content video {
    display: none !important;
}

/* Optional: Add a placeholder where the video was */
.home .entry-content iframe:after,
.home .entry-content video:after {
    content: "[Video available in full post]";
    display: block;
    padding: 10px;
    color: #888;
    font-style: italic;
}
  • Note: Since the video still loads in the background, this won't improve page speed. Stick to the PHP methods if performance is your main priority.

内容的提问来源于stack exchange,提问作者BernyErnie

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 08:07:41