admin管理员组

文章数量:1327945

My Posts dates have the form specified in the WP General settings (d-m-Y), But I want to keep those settings and change the format for Posts only (F Y). How do I do that?

Thanks for your efforts. I am using Customify which includes template-tags.php with the code:

function customify_posted_on() {
        $time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';
    if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
        $time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time><time class="updated" datetime="%3$s">%4$s</time>';
    }

    $time_string = sprintf(
        $time_string,
        esc_attr( get_the_date( 'c' ) ),
        esc_html( get_the_date() ),
        esc_attr( get_the_modified_date( 'c' ) ),
        esc_html( get_the_modified_date() )
    );

    $posted_on = sprintf(
        /* translators: %s: post date. */
        esc_html_x( 'Posted on %s', 'post date', 'customify' ),
        '<a href="' . esc_url( get_permalink() ) . '" rel="bookmark">' . $time_string . '</a>'
    );

Is this what I need to modify and how?

My Posts dates have the form specified in the WP General settings (d-m-Y), But I want to keep those settings and change the format for Posts only (F Y). How do I do that?

Thanks for your efforts. I am using Customify which includes template-tags.php with the code:

function customify_posted_on() {
        $time_string = '<time class="entry-date published updated" datetime="%1$s">%2$s</time>';
    if ( get_the_time( 'U' ) !== get_the_modified_time( 'U' ) ) {
        $time_string = '<time class="entry-date published" datetime="%1$s">%2$s</time><time class="updated" datetime="%3$s">%4$s</time>';
    }

    $time_string = sprintf(
        $time_string,
        esc_attr( get_the_date( 'c' ) ),
        esc_html( get_the_date() ),
        esc_attr( get_the_modified_date( 'c' ) ),
        esc_html( get_the_modified_date() )
    );

    $posted_on = sprintf(
        /* translators: %s: post date. */
        esc_html_x( 'Posted on %s', 'post date', 'customify' ),
        '<a href="' . esc_url( get_permalink() ) . '" rel="bookmark">' . $time_string . '</a>'
    );

Is this what I need to modify and how?

Share Improve this question edited Jul 30, 2020 at 14:41 Antti Koskinen 6,0188 gold badges15 silver badges26 bronze badges asked Jul 26, 2020 at 4:43 BernardBernard 11 bronze badge 2
  • What do you mean change the format for posts only? Where do you want to see a different format? – mozboz Commented Jul 26, 2020 at 9:48
  • I want the date to show as month and year only on posts (era.au/the-nature-of-money), but the complete date dd/mm/yyyy needs to show in other places on the website. – Bernard Commented Jul 27, 2020 at 10:25
Add a comment  | 

1 Answer 1

Reset to default 0

It depends on how this gets rendered in the theme you're using. As you want to change things in one place to look different to the rest of the site, you'll need to track down where that is and change the date format there. Finding where it is might be tricky but once you do it should be clear how to change format of the date.

If you add the theme you're using to your post, someone might be able to track down where to change this and give you more specific instructions.

Here's how I did what you want to do with a clean installation of the 'twentytwenty' theme. These steps might be different but this is just an example of what you need to do if you don't already know there this gets rendered.

Hunting for the code

  1. I looked in the theme for the files which print a single post, which in some themes are called single.php and in this one was called singular.php.
  2. Found that this doesn't do much except output parts that get rendered by the files in template-parts, so I had a look in there. Candidates that seemed like they might contribute to the post being displayed were content.php and entry-header.php.
  3. With a bit of exploration I found entry-header.php was responsible for the header on the posts with the author and date and other stuff, and that it used a function called twentytwenty_the_post_meta to print out that date!
  4. Dug a bit more, found the code in inc/template_tags.php, followed that function call and found this line which prints the date:
    <a href="<?php the_permalink(); ?>"><?php the_time( get_option( 'date_format' ) ); ?></a>

From there, you can see that the_time takes a format string and you can give it any new string you want e.g.:

    <a href="<?php the_permalink(); ?>"><?php the_time( "F Y" ) ); ?></a>

Searching...

Or, another approach if you know how to do it is search through all the files in your theme for e.g. the id/class of the div surrounding the date in your post, or perhaps the the_time function.

本文标签: I want to format my Post dates differently to other dates on my website