• Resolved JZL

    (@lorebion)


    Hi I’ve used +10 hours of trying to figure this thing out (and also making the site smartphone friendly) and I finally decided to ask for help because I was screwing myself up by not eating food until much later than usually.

    I’ve tried all kinds of CSS & PHP codes, adding them to my child themes, functions.php and style.css

    Question:
    How do I add a line of text under a title of a post, before the content?

    I tried to add a custom field for it, so I wrote this in a new custom field:

    Name: after-title

    Value: what I want the content to be under the title

    and I added this to my functions.php:

    add_action(‘loop_start’,’text_after_title’)
    function text_after_title() {
    global $post;
    echo get_post_meta( $post->ID, ‘after-title’, true);
    }

    but what it does is making the text appear above the title ??

Viewing 11 replies - 1 through 11 (of 11 total)
  • Site link?

    Hi,
    Add this bit of code to you child theme’s functions.php

    add_action('__after_content_title','my_extra_line');
    function my_extra_line(){
    echo "My extra line of content added after the titles of each post."; //Adjust
    }
    Thread Starter JZL

    (@lorebion)

    @ Menaka
    Wow, thank you so much! It’s not related to the Custom Fields but it will do.

    I see that the line is above the Featured Image. Is there a code to put it under the Featured Image?

    Also how do you edit the font size of the line?

    @Customizr, it’s undergoing maintenance, so can’t show

    Hi,
    Simply add a low priority (a number higher than 20).

    add_action('__after_content_title','my_extra_line',100);//Added priority of 100
    function my_extra_line(){
    echo "My extra line of content added after the titles of each post."; //Adjust
    }
    Thread Starter JZL

    (@lorebion)

    Thank you so much, there’s a party here now, lol.

    Is there a function to style the line?

    For example, I would like to have the line in smaller text than my content, and I would like to add a date, so every post I make, the date is automatically set.

    Again, thank you so much, even if you can’t help me further!

    Hi lorebion,
    Glad that it worked for you.
    You can add the styling in the string. Like this?

    add_action('__after_content_title','my_extra_line',100);//Added priority of 100
    function my_extra_line(){
    echo '<span style="font-size: 70%;">My extra line of content added after the titles of each post.</span>'; //Adjust
    }

    With respect to displaying the date of publishing, why don’t you use the post metas?

    Thread Starter JZL

    (@lorebion)

    Menaka, you are awesome! After all the confusions and frustrations, these baby steps really makes me happy!

    The reason I don’t use the post metas is because I don’t know how to remove the url link that comes with the post-metas and I also don’t know how to adjust and design the way the post-metas look like.
    (Spent a lot of time trying to figure that out. So I instead started to get into manual coding, which seems more intuitive)
    But if there’s a way to style and edit the post metas in a less complicated way, I would love to learn!

    Here’s how the codes look now:

    add_action(‘__after_content_title’,’my_extra_line’,100);
    function my_extra_line(){
    echo ‘<span style=”font-size: 70%; span class = “hidden-xs”; br class= “visible-xs”;> Written by Mike ??? · ??? [amount of comments] ??? · ??? [published date] </span>’;
    }

    I added these because I saw they were for mobile responsiveness purposes

    span class = “hidden-xs”; br class= “visible-xs”;

    ( I have no idea if it does anything but the site is still functional lol)

    So here’s my next challenge:
    I want the date to be set when I publish a post, and the amount of comments in the blog

    [amount of comments] ??? · ??? [published date]

    I’ve tried to replace [published date] with

    the_time(‘jS F Y’)

    but I get a syntax error.

    I don’t expect you to be able to help me further Menaka but it would be really great if you could!

    Thank you for all the help!

    Hi lorebion,
    I would suggest that you turn on the post metas and customize them. It is easier. There are hooks for post metas as well.

    If you still want to go this route, you will gave to use get_post_meta() to get out the published date and other details, involving considerable work.

    Thread Starter JZL

    (@lorebion)

    Ok I’ve tried to edit the post metas with this Customizr Remove Post Metas Article

    but I can’t get a result as I want which looks like this:

    Written by Mike ??? · ??? [Amount of Comments] ??? · ??? [publish date]

    where there are no URLs to the date and author.

    Does it really involve a lot of work?

    It’s just the ‘amount of comments’ and ‘publish date’ that is missing…

    Hi,
    I agree now that it is easier to add extra line than use post metas for your requirement. So, here goes.

    add_action('__after_content_title','my_extra_line',100);
    function my_extra_line(){
    echo '<span style="font-size: 70%;"> Written by '. get_the_author()  .'  ·   '. get_comments_number()  .' · '. get_the_date('jS F Y') .'</span>';//Adjust
    }
    Thread Starter JZL

    (@lorebion)

    That is so great Menaka! You’ve made another human being really happy!

    I forgot to mention one last thing…

    Will start a new thread on it because of different topic:
    how to separate functions from homepage and posts?

    Anyways… having struggled with this for almost 2 weeks to make it look like that, I’m really grateful for your help. Thank you!!

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Adding 1 line of text under a post title’ is closed to new replies.