如何在WordPress中自动为每篇文章添加固定模板文本?
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:
Method 1: Use a Custom Function (Recommended for Developers)
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-templateclass in your CSS to match your site’s design - Important: Add this code to a child theme’s
functions.phpfile 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:
- Install and activate a plugin like Insert Headers and Footers or Code Snippets (for more control).
- 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' );
- Add this snippet via your code plugin:
- Go to the plugin’s settings and find the option to insert content after post content.
- Paste your template using the shortcode:
【朋友们,我们的“[post_title]”相关文章怎么样?您喜欢吗?如有疑问请在下方留言。想要获取更多“[post_title]”相关文章,请订阅我们。】 - 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

