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

如何在WordPress中自动为每篇文章添加固定模板文本?

How to Automatically Add a Fixed Template to the End of Every WordPress Post

Hey there! Great question—automating this in WordPress is totally doable with a couple of solid methods. Let me walk you through the most reliable options:

The cleanest way is to hook into WordPress’s the_content filter to dynamically insert your template with the post title at the end of every post. This keeps your code centralized and avoids relying on extra plugins.

Here’s the code you can use:

function add_post_footer_template( $content ) {
    // Only apply to individual post pages (skip pages, archives, etc.)
    if ( is_single() && in_the_loop() && is_main_query() ) {
        $post_title = get_the_title();
        $template = '<div class="post-footer-template">
            <p>【朋友们,我们的“' . esc_html( $post_title ) . '”相关文章怎么样?您喜欢吗?如有疑问请在下方留言。想要获取更多“' . esc_html( $post_title ) . '”相关文章,请订阅我们。】</p>
        </div>';
        $content .= $template;
    }
    return $content;
}
add_filter( 'the_content', 'add_post_footer_template' );

Key Notes:

  • is_single() ensures this only runs on single post pages (not pages, category archives, etc.)
  • esc_html($post_title) sanitizes the post title to prevent potential security issues
  • You can style the .post-footer-template class in your CSS to match your site’s design
  • Important: Add this code to a child theme’s functions.php file or use a code snippet plugin (like Code Snippets) to avoid losing changes when updating your parent theme.

Method 2: Use a Plugin (For Non-Coders)

If you don’t want to work with code, you can use a plugin to insert dynamic content at the end of posts. Here’s how:

  1. Install and activate a plugin like Insert Headers and Footers or Code Snippets (for more control).
  2. First, create a shortcode to pull the post title (if your plugin doesn’t support dynamic post data natively):
    • Add this snippet via your code plugin:
      function post_title_shortcode() {
          return esc_html( get_the_title() );
      }
      add_shortcode( 'post_title', 'post_title_shortcode' );
      
  3. Go to the plugin’s settings and find the option to insert content after post content.
  4. Paste your template using the shortcode:
    【朋友们,我们的“[post_title]”相关文章怎么样?您喜欢吗?如有疑问请在下方留言。想要获取更多“[post_title]”相关文章,请订阅我们。】
    
  5. Save your settings—your template will now appear automatically at the end of every post.

Bonus: Exclude Specific Posts

If you want to skip certain posts, add a custom field (e.g., exclude_footer_template set to yes) to those posts, then modify the Method 1 function to check for it:

function add_post_footer_template( $content ) {
    if ( is_single() && in_the_loop() && is_main_query() ) {
        // Skip posts with the exclude custom field set to 'yes'
        $exclude = get_post_meta( get_the_ID(), 'exclude_footer_template', true );
        if ( $exclude !== 'yes' ) {
            $post_title = get_the_title();
            $template = '<div class="post-footer-template">
                <p>【朋友们,我们的“' . esc_html( $post_title ) . '”相关文章怎么样?您喜欢吗?如有疑问请在下方留言。想要获取更多“' . esc_html( $post_title ) . '”相关文章,请订阅我们。】</p>
            </div>';
            $content .= $template;
        }
    }
    return $content;
}
add_filter( 'the_content', 'add_post_footer_template' );

Either method will get your template added automatically. Let me know if you need help troubleshooting any part of this!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 09:06:27