Support » Fixing WordPress » Adding subtitles with code, not a plugin

  • Resolved Bad_Egg

    (@bad_egg)


    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/ . 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 1 year, 3 months ago by Bad_Egg.
    • This topic was modified 1 year, 3 months ago by Bad_Egg.

    The page I need help with: [log in to see the link]

Viewing 9 replies - 1 through 9 (of 9 total)
  • 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.)

    Thread Starter Bad_Egg

    (@bad_egg)

    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

    (@bad_egg)

    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:

    I figure the problem is the code I inserted is incorrect. Please help again.
    Thanks,
    Michelle

    • This reply was modified 1 year, 3 months ago by Bad_Egg.
    • This reply was modified 1 year, 3 months ago by Bad_Egg.
    Thread Starter Bad_Egg

    (@bad_egg)

    I tried to add a screenshot via a link but it’s not working here. ??

    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/ 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.

    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

    (@bad_egg)

    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

    • This reply was modified 1 year, 2 months ago by Bad_Egg.
    • This reply was modified 1 year, 2 months ago by Bad_Egg.

    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.

    Thread Starter Bad_Egg

    (@bad_egg)

    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/. 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.