WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Modifying twentyeleven_posted_on to show the post time (6 posts)

  1. fuf
    Member
    Posted 2 years ago #

    I'd like to modify the twentyeleven theme so that the "Posted on" meta info below the post title includes the post time as well as the post date.

    I found one solution that suggests adding the_time to content.php like this:

    <div class="entry-meta">
    <?php twentyeleven_posted_on(); ?> at <?php the_time('g:i a') ?>
    </div><!-- .entry-meta -->

    But it seems more logical to modify the twentyeleven_posted_on function directly.

    Normally I can figure out what's going on in a theme's code, but when it comes to functions I am lost. How would I modify the function so that it displays "posted on [the date] at [the time]"? The function looks like this:

    function twentyeleven_posted_on() {
    	printf( __( '<span class="sep">Posted on </span><a href="%1$s" title="%2$s" rel="bookmark"><time class="entry-date" datetime="%3$s" pubdate>%4$s</time></a><span class="by-author"> <span class="sep"> by </span> <span class="author vcard"><a class="url fn n" href="%5$s" title="%6$s" rel="author">%7$s</a></span></span>', 'twentyeleven' ),
    		esc_url( get_permalink() ),
    		esc_attr( get_the_time() ),
    		esc_attr( get_the_date( 'c' ) ),
    		esc_html( get_the_date() ),
    		esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
    		sprintf( esc_attr__( 'View all posts by %s', 'twentyeleven' ), get_the_author() ),
    		esc_html( get_the_author() )
    	);
    }

    Thanks for any help!

  2. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    possibly:
    printf( __( '<span class="sep">Posted on </span><a href="%1$s" title="%2$s" rel="bookmark"><time class="entry-date" datetime="%3$s" pubdate>%4$s at %2$s</time></a><span class="by-author"> <span class="sep"> by </span> <span class="author vcard"><a class="url fn n" href="%5$s" title="%6$s" rel="author">%7$s</a></span></span>', 'twentyeleven' ),
    (that would have the time in the same link as the date)

  3. peredur
    Member
    Posted 2 years ago #

    It is essential that you make changes like these in a child theme.

    That said, you could start with something like this:

    function twentyeleven_posted_on() {
    	printf( __( '<span class="sep">Posted on </span><a href="%1$s" title="%2$s" rel="bookmark"><time class="entry-date" datetime="%3$s" pubdate>%4$s</time></a><span class="by-author"> <span class="sep"> by </span> <span class="author vcard"><a class="url fn n" href="%5$s" title="%6$s" rel="author">%7$s</a></span></span> at %8$s', 'twentyeleven' ),
    		esc_url( get_permalink() ),
    		esc_attr( get_the_time() ),
    		esc_attr( get_the_date( 'c' ) ),
    		esc_html( get_the_date() ),
    		esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
    		sprintf( esc_attr__( 'View all posts by %s', 'twentyeleven' ), get_the_author() ),
    		esc_html( get_the_author() ),
    		twentyeleven_posted_on()
    	);
    }

    This just adds the string 'at', plus the post time to the end of the string constructed by printf(). It's not surrounded by any span tags or anything so contains no CSS hooks. It might therefore stand out a bit: but if it outputs the correct words you can worry about the styling after.

    You might also like to surround twentyeleven_posted_on() with esc_html() for security reasons.

    HTH

    PAE

  4. fuf
    Member
    Posted 2 years ago #

    Thanks a lot alchymyth, that works great. Although I can't say I understand why it works!

    I ended up cutting out all the links and author stuff and changing the date and time around so now it looks like this:

    printf( __( '<time class="entry-date" datetime="%3$s" pubdate>%2$s, %4$s </time>', 'twentyeleven' ),

    Which produces: "1:12 pm, August 23, 2011".

  5. fuf
    Member
    Posted 2 years ago #

    Thanks for your solution also peredur. I'm not entirely sure what you mean about the esc_html() security thing, but since alchymyth's solution uses %2$s to display the time (which I assume corresponds to "esc_attr( get_the_time() )" somehow) am I right that I don't need to add anything else?

    edit: forgot to mention, I am using a child theme, so no worries there.

  6. peredur
    Member
    Posted 2 years ago #

    Oh yes. That's fine. Alchymyth's solution is, as usual, superior to mine.

    The esc_html() business is just a way of protecting yourself against XSS attacks IIRC, because the output is going to appear in the HTML for the page.

    Cheers

    PAE

Topic Closed

This topic has been closed to new replies.

About this Topic