0

How can I add the words "Posted on" before the post data in a Twentyfourteen theme's post? The default is only to show the date and clock icon.

I'm not sure if this is a basic modification or if it's going to be a fix unique to Twentyfourteen.

I found this web page talking about removing the "posted on" text in the Twentyeleven theme (I hoped the themes would be similar). It said to look in the functions.php file for function twentyeleven_posted_on() but I was unable to find anything like that in the Twentyfourteen parent theme's functions.php file. There didn't seem to be anything directly relating to a post's date in the Twentyfourteen functions.php file.

I am using a child theme, so I assume I'm probably going to be putting an altered function in my child theme's functions.php file, but I can't find the original function in Twentfourteen.

The reason I ask is because Twentyfourteen default of only having the date has confused some of my readers; when the post is talking about an event they think the post date is the date of the event.

Solution (Thanks to Tom Woodward and Dave Clements)

Both Tom's and Dave's functions worked, but neither of them did at first because I was placing the new function below the ?> which is the ending tag for PHP and no code would work below it.

This was my existing child theme's functions.php file (just basically copied from the Wordpress support guide on setting up a child theme)

<?php
add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );
function theme_enqueue_styles() {
wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
}

?>

I needed to add the new function in between the last } and the bottom ?>

<?php
add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );
function theme_enqueue_styles() {
wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
}

function add_publish_dates( $the_date, $d, $post ) {
if ( is_int( $post) ) {
$post_id = $post;
} else {
$post_id = $post->ID;
}

return 'Posted on '. date( 'Y-d-m - h:j:s', strtotime( $the_date ) );
}
add_action( 'get_the_date', 'add_publish_dates', 10, 3 );

?>

I eventually ended up using Tom Woodward's code because it placed the little clock icon before the "Posted on" whereas Dave Clement's put it after "Posted on" in between it and the date, which looked odd.

Then I changed the date formatting for the date after return 'Posted on '. date from 'Y-d-m - h:j:s' (which displayed the date as 2016-16-03 - 12:00:00 [which oddly ALWAYS said it was posted at 12am, when the posts were not]) to 'F j, Y' (Which displays the default "March 16, 2016")

<?php
add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );
function theme_enqueue_styles() {
wp_enqueue_style( 'parent-style', get_template_directory_uri() . '/style.css' );
}

function add_publish_dates( $the_date, $d, $post ) {
if ( is_int( $post) ) {
$post_id = $post;
} else {
$post_id = $post->ID;
}

return 'Posted on '. date( 'F j, Y', strtotime( $the_date ) );
}
add_action( 'get_the_date', 'add_publish_dates', 10, 3 );

?>

As I said, I ended up using Tom Woodward's code as a base, but Dave Clements code did exactly what I had asked (just not exactly what I wanted) and he solved the ?> problem for me, so I give Dave the point. But thank you so much to both Tom and Dave!

  • Have you tried using the same functions such as the ones in twentyeleven_posted_on() in your content-*.php templates? – denis.stoyanov Mar 11 '16 at 16:29
  • Thank you for the reply Denis. I don't know enough about the ramifications of taking functions from one theme and applying them to another theme to have the guts to try that. However, your implicit suggestion to look in the content-*.php files is well taken! :) I will look there for some clue about where Twentfourteen keeps its post date functions. – TrinityP Mar 11 '16 at 17:19
1

The post date is added by twentyfourteen_posted_on as shown in content.php. This function is in /inc/template-tags.php and is correctly wrapped in an if ( ! function_exists( 'twentyfourteen_posted_on' ) ) conditional so you can declare the same function in your child theme and modify it to your heart's content.

As such you could add a function like this to your theme's functions.php file and that should do the trick (note the addition of "Posted on " in the printf function:

function twentyfourteen_posted_on() {
        if ( is_sticky() && is_home() && ! is_paged() ) {
                echo '<span class="featured-post">' . __( 'Sticky', 'twentyfourteen' ) . '</span>';
        }

        // Set up and print post meta information.
        printf( 'Posted on <span class="entry-date"><a href="%1$s" rel="bookmark"><time class="entry-date" datetime="%2$s">%3$s</time></a></span> <span class="byline"><span class="author vcard"><a class="url fn n" href="%4$s" rel="author">%5$s</a></span></span>',
                esc_url( get_permalink() ),
                esc_attr( get_the_date( 'c' ) ),
                esc_html( get_the_date() ),
                esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
                get_the_author()
        );
}
  • I certainly appreciate the clear answer, but copying and pasting that into my child theme's functions.php file results in the raw code showing up in on the actual pages in the header. I am using the Wordpress editor menu to do this, so the indentation is altered due to the wordwrap in the editor. Is that the problem? – TrinityP Mar 14 '16 at 17:33
  • Hmm, that shouldn't much matter. Did you add it right at the bottom of the file, not thrown in amongst other functions? Check that it reads the same: look for curly quotes and that kind of thing. Also, make sure there's not a closing PHP tag before the function you pasted in (?>) – Dave Clements Mar 15 '16 at 12:12
  • You hit the nail on the head Dave! It was the closing PHP tag ( ?> ) that was keeping it from working. All I had read was that you put new functions at the bottom of the functions.php file, but didn't bother looking up any basic PHP syntax before doing it. My bad. Thank you for clearing that up for me! – TrinityP Mar 16 '16 at 15:27
0

I took a look at the documentation here and this filter seems to work for 2014.

function add_publish_dates( $the_date, $d, $post ) {
if ( is_int( $post) ) {
    $post_id = $post;
} else {
    $post_id = $post->ID;
}

return 'Posted on '. date( 'Y-d-m - h:j:s', strtotime( $the_date ) );
}
add_action( 'get_the_date', 'add_publish_dates', 10, 3 );
  • Thank you for the suggestion. Is this something that would go in the child theme's functions.php file, or would it go somewhere else? I'm afraid my knowledge of coding for Wordpress is very limited. – TrinityP Mar 14 '16 at 17:35
  • I put it in functions and it worked for me. You could make it a plugin but functions seems the route since it's a child theme anyway. – Tom Woodward Mar 14 '16 at 17:42

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.