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

如何为PHP snippet添加single post模板并显示intro text area?

Hey there! Let's break this down into two straightforward parts: setting up your custom single post template, and getting that intro text area to display on single posts. Here's how to tackle both:

1. Create a Custom Single Post Template
  • Start by duplicating your theme's existing single.php file (if you don't have one, use index.php as a base) and rename it to something descriptive, like single-custom-post.php.
  • At the very top of this new file, add this template header comment so WordPress recognizes it as a selectable template:
<?php
/*
Template Name: Custom Single Post
Template Post Type: post
*/
?>
  • You can now tweak this template to match your desired layout—adjust where elements sit, add custom styling hooks, or remove parts you don't need.
  • To apply it to your posts: Open the post editor in WordPress, look for the "Template" dropdown under Post Attributes in the right sidebar, select your new "Custom Single Post" template, then hit update.
2. Display the Intro Text Area on Single Posts

Most of the time, intro text is stored as a custom field—here's how to pull it into your template:

First, confirm your meta key

If you used a plugin like Advanced Custom Fields (ACF) to create the intro text area, the meta key is the field name you set (e.g., post_intro). If you added it manually, it's the exact name you used for the custom field.

Add the code to your single template

Find the spot where you want the intro text to appear (usually right after the post title or before the main content), then add one of these snippets:

For standard custom fields:

<?php
// Fetch the intro text
$intro_text = get_post_meta(get_the_ID(), 'post_intro', true);

// Only display if there's content
if (!empty($intro_text)) {
    echo '<div class="post-intro">' . wpautop($intro_text) . '</div>';
}
?>

If you're using ACF:

This is a bit cleaner thanks to ACF's built-in functions:

<?php if (get_field('post_intro')) : ?>
    <div class="post-intro"><?php the_field('post_intro'); ?></div>
<?php endif; ?>

If your intro text is the post excerpt:

If you're using the default WordPress excerpt field as your intro, use this:

<?php if (has_excerpt()) : ?>
    <div class="post-intro"><?php the_excerpt(); ?></div>
<?php endif; ?>

Troubleshooting if it's not showing up

  • Double-check that your meta key is exact—it's case-sensitive, so Post_Intro is different from post_intro.
  • Make sure the post you're testing actually has content filled in the intro text area (it sounds obvious, but we've all missed this!).
  • If you're working with a custom post type, update the template header to include it: Template Post Type: post, your_custom_post_type.
  • Clear your WordPress cache (if you use a caching plugin) to ensure your changes load properly.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 08:09:02