Title: Adding subtitles with code, not a plugin
Last modified: March 30, 2021

---

# Adding subtitles with code, not a plugin

 *  Resolved [Bad_Egg](https://wordpress.org/support/users/bad_egg/)
 * (@bad_egg)
 * [5 years, 2 months ago](https://wordpress.org/support/topic/adding-subtitles-with-code-not-a-plugin/)
 * Hi,
 * I want to add some code to my child theme that would allow me to add a subtitle
   on SOME but not all of my posts (not interested in a plugin). You can see in 
   the link at bottom that I have a subtitle, but that it appears below the time
   stamp. I want to create an option to have a subtitle appear above the time stamp.
 * I found this: [https://generatepress.com/forums/topic/how-to-add-subtitle-below-the-title-of-the-blog-post/](https://generatepress.com/forums/topic/how-to-add-subtitle-below-the-title-of-the-blog-post/).
   But he doesn’t say in which php file to add the code. And I don’t know if it’s
   up to date.
 * Can anyone please tell me if there is a way to modify my child theme to provide
   the option for a subtitle? Thanks.
 * P.S. I’m not a coder, so please make it simple to understand. 🙂
    -  This topic was modified 5 years, 2 months ago by [Bad_Egg](https://wordpress.org/support/users/bad_egg/).
    -  This topic was modified 5 years, 2 months ago by [Bad_Egg](https://wordpress.org/support/users/bad_egg/).
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fadding-subtitles-with-code-not-a-plugin%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 9 replies - 1 through 9 (of 9 total)

 *  [George Appiah](https://wordpress.org/support/users/gappiah/)
 * (@gappiah)
 * [5 years, 2 months ago](https://wordpress.org/support/topic/adding-subtitles-with-code-not-a-plugin/#post-14280058)
 * The GeneratePress forum post you linked to suggests using a custom field, which
   is perhaps the easiest (albeit, not the most elegant) way to do this. The post
   also linked to the WordPress support article on custom fields.
 * The code provided would display the custom field (created in the WordPress editor)
   on your website. The author suggested to “hook” the code into your site, that’s
   why no specific file was mentioned: basically, if you know how to do this “hook”,
   you’ll know what to do 🙂
 * As an alternative to “hooking”, you can just paste the code directly where you
   want the custom field to be displayed. For your use-case, that would go into 
   the `content-single.php` file in your theme (for posts), at the location shown
   below (note that this file may not exist in your child theme, and you may have
   to grab a copy of this file from your parent time into your child theme and edit
   the child theme’s copy.)
 * ![](https://i0.wp.com/i.snipboard.io/skxMaI.jpg?ssl=1)
 *  Thread Starter [Bad_Egg](https://wordpress.org/support/users/bad_egg/)
 * (@bad_egg)
 * [5 years, 2 months ago](https://wordpress.org/support/topic/adding-subtitles-with-code-not-a-plugin/#post-14280269)
 * Thank you, George, for taking the time to respond. Will try this and get back
   to you if it fails (or I fail to understand correctly – 🙂 ).
 * Michelle
 *  Thread Starter [Bad_Egg](https://wordpress.org/support/users/bad_egg/)
 * (@bad_egg)
 * [5 years, 2 months ago](https://wordpress.org/support/topic/adding-subtitles-with-code-not-a-plugin/#post-14280316)
 * Okay, not working. What am I doing wrong?
 * I copied the content-single.php to my child theme. The code to add is:
 *     ```
       <?php
       // Get custom field value
       $subtitle = get_post_meta( get_the_ID(), 'your-custom-field', true );
       // Display $subtitle if not empty
       if ( ! empty( $subtitle ) ) {
           echo '<p class="custom-subtitle">' . $content . '</p>';
       } 
       ?>
       ```
   
 * where ‘your custom-field” is the name of the custom field.
 * In my case, the name is ‘subtitle’. So I added (in the spot you indicated):
 *     ```
       <?php
       // Get custom field value
       $subtitle = get_post_meta( get_the_ID(), 'subtitle', true );
       // Display $subtitle if not empty
       if ( ! empty( $subtitle ) ) {
           echo '<p class="custom-subtitle">' . $content . '</p>';
       } 
       ?>
       ```
   
 * So now it looks like this:
 *     ```
       <div class="entry-container">
       		<header class="entry-header">
       			<h1 class="entry-title"><?php the_title(); ?></h1>
       			<?php
       			// Get custom field value
       			$subtitle = get_post_meta( get_the_ID(), 'subtitle', true );
       			// Display $subtitle if not empty
       			if ( ! empty( $subtitle ) ) {
           echo '<p class="custom-subtitle">' . $content . '</p>';
       } 
       ?>
       			<?php clean_box_entry_meta(); ?>
       		</header><!-- .entry-header -->
       ```
   
 * And now my post dashboard looks like this:
    [](https://mademers.com/wp-content/uploads/SS-custom.jpg)
 * I figure the problem is the code I inserted is incorrect. Please help again.
   
   Thanks, Michelle
    -  This reply was modified 5 years, 2 months ago by [Bad_Egg](https://wordpress.org/support/users/bad_egg/).
    -  This reply was modified 5 years, 2 months ago by [Bad_Egg](https://wordpress.org/support/users/bad_egg/).
 *  Thread Starter [Bad_Egg](https://wordpress.org/support/users/bad_egg/)
 * (@bad_egg)
 * [5 years, 2 months ago](https://wordpress.org/support/topic/adding-subtitles-with-code-not-a-plugin/#post-14285181)
 * I tried to add a screenshot via a link but it’s not working here. ??
 *  [George Appiah](https://wordpress.org/support/users/gappiah/)
 * (@gappiah)
 * [5 years, 2 months ago](https://wordpress.org/support/topic/adding-subtitles-with-code-not-a-plugin/#post-14286599)
 * Sorry, I’ve been busy here.
 * The forum doesn’t support attachments. But you can upload your screenshot to 
   a free image hosting service like [https://snipboard.io/](https://snipboard.io/)
   and share the link here.
 * I’m settling down now to work — I’ll install your theme and get you a working
   code later in the day.
 *  [George Appiah](https://wordpress.org/support/users/gappiah/)
 * (@gappiah)
 * [5 years, 2 months ago](https://wordpress.org/support/topic/adding-subtitles-with-code-not-a-plugin/#post-14286859)
 * It’s me again 🙂
 * Use this simple code instead (tested, works):
 * `<?php echo get_post_meta($post->ID, 'subtitle', true); ?>`
 * Where `subtitle` is the name (key) of your custom field. You want to wrap the
   code in a div or h2 tag so you can easily style it.
 * As I mentioned earlier, this works… but it’s not a particularly elegant solution.
   A more elegant solution would be to have the subtitle box right below the title
   box, instead of being stuck at the bottom of the page in custom fields. But that
   calls for creating a custom **meta box**, which is beyond the skillset of this
   non-coder homeschooling Dad 😀
 *  Thread Starter [Bad_Egg](https://wordpress.org/support/users/bad_egg/)
 * (@bad_egg)
 * [5 years, 2 months ago](https://wordpress.org/support/topic/adding-subtitles-with-code-not-a-plugin/#post-14294783)
 * Hi George,
 * That works! Many, many thanks.
 * But now my headline is disappearing. I think I messed up my default custom fields—
   on the post I was trying to create the subtitle on, I didn’t realize the custom
   fields were default and I deleted some. But then I realized my mistake and set
   them back but now I’m missing my headlines on a single post page.
 * Do you know where I might find the code to fix it? If not, I’ll start a separate
   thread. Sigh
 * P.S. I uploaded the screenshot to my own website and added the link — as as link,
   which was likely incorrect — but it was edited out by the moderators, I think.
   Will try again here as it shows the custom fields that remain: [https://mademers.com/wp-content/uploads/SS-custom.jpg](https://mademers.com/wp-content/uploads/SS-custom.jpg)
    -  This reply was modified 5 years, 2 months ago by [Bad_Egg](https://wordpress.org/support/users/bad_egg/).
    -  This reply was modified 5 years, 2 months ago by [Bad_Egg](https://wordpress.org/support/users/bad_egg/).
 *  [George Appiah](https://wordpress.org/support/users/gappiah/)
 * (@gappiah)
 * [5 years, 2 months ago](https://wordpress.org/support/topic/adding-subtitles-with-code-not-a-plugin/#post-14294977)
 * > But now my headline is disappearing. I think I messed up my default custom 
   > fields — on the post I was trying to create the subtitle on, I didn’t realize
   > the custom fields were default and I deleted some. But then I realized my mistake
   > and set them back but now I’m missing my headlines on a single post page.
 * You probably mistakenly removed the line of code that displays the headline in
   the `content-single.php` file. Please check the file to be sure the following
   code exists. If not, add it:
 * Title/Headline code:
 * `<h1 class="entry-title"><?php the_title(); ?></h1>`
 * Sub-title code (modified):
 * `<h2 class="subtitle"><?php echo get_post_meta($post->ID, 'subtitle', true); ?
   ></h2>`
 * I’ve modified the sub-title code slightly to make it easy for us to style the
   subtitle differently: you ​probably want to make it smaller than the title, but
   larger than the body paragraph text. Let’s first get both the title and subtitle
   to show, then we can work on the styling.
 * ![](https://i0.wp.com/snipboard.io/gMY2OF.jpg?ssl=1)
 *  Thread Starter [Bad_Egg](https://wordpress.org/support/users/bad_egg/)
 * (@bad_egg)
 * [5 years, 2 months ago](https://wordpress.org/support/topic/adding-subtitles-with-code-not-a-plugin/#post-14295606)
 * Bingo! I had accidentally deleted the H1 code. Oops. And what a relief that it
   was such a minor mistake.
 * I added the H heading to the subtitle, but I had to use H4: H2 was already the
   size used for the headline, H3 I already have customized, but the default H4 
   works great: [https://mademers.com/time-is-no-excuse-for-our-past-sins/](https://mademers.com/time-is-no-excuse-for-our-past-sins/).
   Now I have a subtitle exactly where I want it.
 * Thank you very much for your time and trouble. I continually marvel that there
   exists forums like this and strangers willing to help. Now I can get to work 
   adding subtitles to several other posts!
 * Cheers,
    Michelle

Viewing 9 replies - 1 through 9 (of 9 total)

The topic ‘Adding subtitles with code, not a plugin’ is closed to new replies.

## Tags

 * [custom fields](https://wordpress.org/support/topic-tag/custom-fields/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 9 replies
 * 2 participants
 * Last reply from: [Bad_Egg](https://wordpress.org/support/users/bad_egg/)
 * Last activity: [5 years, 2 months ago](https://wordpress.org/support/topic/adding-subtitles-with-code-not-a-plugin/#post-14295606)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
