Set the date
-
Is it possible to add the date under the title?
I tried to add $post->post_date but the it is not formatted in italian mode so day + mm + yyyy
How to do? Thanks
-
Hi Marco.
Yes, of course you can. You just need to tweak the template you’re using (which I believe you already know how to, because you were able to append the
$post->post_date
value) and add the date:$date_format = get_option( 'date_format' ); $date = strtotime( $post->post_date ); $date = date( $date_format, $date );
In the previous snippet,
$date_format
uses your WordPress current setup to determine how to format a date. I’d assume that it’ll use the Italian locale, but I’m not 100% sure. If you want to use a different format, just either edit the date format in your WordPress Settings screen, or set your own format manually:$date_format = 'Y m, d';
In PHP documentation website, you’ll find a comprehensive list of all the accepted “letters” for formatting dates.
Hope this helps!
Regards,
DavidHi, thanks for your reply.
Can you pls help me to add your code inside my custom template i attach you?
I need the date under the title and if possible also a button that links to the article.
Thanks a lot for your help
Bye
Marco<?php /** * You must declare this variable in your templates. It contains the current * related post that is about to be displayed. */ global $post; // Open link A tag that points to the post itself $open_link = sprintf( '<a class="related_post_link" href="%s" title="%s">', get_permalink( $post->ID ), esc_attr( apply_filters( 'the_title', $post->post_title, $post->ID ) ) ); $close_link = '</a>'; ?> <article class="post-<?php echo $post->ID; ?> post type-post status-publish entry" itemscope="itemscope"><?php if ( has_post_thumbnail( $post->ID ) ) { ?> <div class="featured-image alignleft"> <?php echo $open_link; echo get_the_post_thumbnail( $post->ID, 'post-thumbnail' ); echo $close_link; ?> </div> <?php } ?> <h2 class="entry-title"><?php echo $open_link; echo apply_filters( 'the_title', $post->post_title, $post->ID ); echo $close_link; ?></h2> <div class="vcex-blog-entry-date"> <?php echo apply_filters( 'date', $post->post_date, $post->ID ); ?> </div> </article>
Sure,
Just add the snippet I shared above right after the
global $post;
declaration and echo the$date
right after the title:... global $post; $date_format = get_option( 'date_format' ); $date = strtotime( $post->post_date ); $date = date( $date_format, $date ); ... <h2 class="entry-title"><?php echo $open_link; echo apply_filters( 'the_title', ... echo ' ' . $date; echo $close_link; ?></h2> ...
hi, thanks but i see the date only in english and not in italian… can you pls help me?
<?php /** * You must declare this variable in your templates. It contains the current * related post that is about to be displayed. */ global $post; $date_format = 'Y m, d'; $date_format = get_option( 'date_format' ); $date = strtotime( $post->post_date ); $date = date( $date_format, $date ); // Open link A tag that points to the post itself $open_link = sprintf( '<a class="related_post_link" href="%s" title="%s">', get_permalink( $post->ID ), esc_attr( apply_filters( 'the_title', $post->post_title, $post->ID ) ) ); $close_link = '</a>'; ?> <article class="post-<?php echo $post->ID; ?> post type-post status-publish entry" itemscope="itemscope"><?php if ( has_post_thumbnail( $post->ID ) ) { ?> <div class="featured-image alignleft"> <?php echo $open_link; echo get_the_post_thumbnail( $post->ID, 'post-thumbnail' ); echo $close_link; ?> </div> <?php } ?> <h2 class="entry-title"><?php echo $open_link; echo apply_filters( 'the_title', $post->post_title, $post->ID ); echo $close_link; ?></h2> <div class="vcex-blog-entry-date"> <?php echo ''. $date; ?> </div> </article>
Oops! Don’t use PHP’s
date
function, but WordPress’date_i18n
function (you can see the documentation in the Codex):... $date_format = get_option( 'date_format' ); $date = strtotime( $post->post_date ); $date = date_i18n( $date_format, $date ); ...
perfect, thanks! It works now.
- The topic ‘Set the date’ is closed to new replies.