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.

2 Answers 2

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;} 
2
  • 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? Commented Dec 12, 2016 at 7:14
  • I edit my answer. Commented Dec 12, 2016 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 ); 
1
  • Doesn't work: For home page, it just displays one date. Commented Dec 10, 2016 at 17:12

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.