0

How do I add the date before the entry title in Twenty Twelve with just functions.php?

Right now I have to add and modify many files in the child theme such as content.php, content-aside.php, content-image.php, content-link.php, content-quote.php, content-status.php.

Is there a way to do it with filters? I tried to put it in the_content but the date displays below the title.

1

Base on @Tunji answer. You can add some conditions to the function to detect home-page, feeds...

function display_extra_title( $title, $id = null ) {

    $date = the_date('', '', '', false);

    if(is_home()){
        $title = get_bloginfo( 'name' );
        return $date . $title;
    }
    else{
        return $date . $title;
    }

}
add_filter( 'the_title', 'display_extra_title', 10, 2 );

In order to properly return the_date and not echoing it, you must set the last parameter to false. See more details here

If you don't want to display the date in the menu widget... You can surround $date in a span and add css to not display it when it's the menu, sidebar...

$title = '<span class="title_date">'.$date.'</span> '.$title;

In the css (adjust and add all what you need:

.sidebar .title_date{ display:none;}
  • Thanks. Few things I see: The first post doesn't display the date. The post heading is replaced by the date and title of my site. The date needs to be a different div. Menu and sidebar items also display the title of my site (maybe because they too get replaced). How do I solve all of these? – Ramanan Dec 12 '16 at 7:14
  • I edit my answer. – Benoti Dec 12 '16 at 8:54
0

You can use the_title filter. Example below.

function display_extra_title( $title, $id = null ) {

    $date = the_date();

    return $date . $title;
}
add_filter( 'the_title', 'display_extra_title', 10, 2 );
  • Doesn't work: For home page, it just displays one date. – Ramanan Dec 10 '16 at 17:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.