Site logo

Archived topic

Add date AND time to posts?

12 replies · Started by Kev on April 4, 2023

Viewing posts 1–13 of 13

Hey folks,

I'm building a site that's basically gonna be a microblog, so I need to display the date and time of the post, as there will often be multiple posts per day. So instead of saying "04 April 2023" in the post meta, I'd like it to say "04 April 2023 at 14:25" for example.

Is there a way to do this easily?

Thanks,

Kev

Forgot to enable notifications...

Doh! I just figured out I can do it in WP Setting by using a custom time format. No need to respond to this.

Glad you've figured it out :)

Hey folks,

Sorry to re-open this. I changed the WP settings so that it displays date and time with j F Y H:i which worked fine for posts, as it would display something like 4 April 2023 23:12 problem is when it comes to comments. Because they already include in the time in them, their meta appears like this - 4 April 2023 23:19 at 23:19.

Is there a way to fix this so that the date and time are displayed correctly everywhere?

Thanks,

Kev

Adding that caused a critical error on my site. :(

May we know how you added it?

It's a child theme, I added it via functions.php

My existing functions file is below. I appended it to the end.


<?php

// Gutenberg custom stylesheet
add_theme_support('editor-styles');
add_editor_style( 'style.css' );
add_editor_style( 'editor-style.css' );

// Remove block library stylesheet
function remove_block_library_css(){
    wp_dequeue_style( 'wp-block-library' );
} 
add_action( 'wp_enqueue_scripts', 'remove_block_library_css' );

// Remove "Archive:", "Category:" etc.
add_filter( 'get_the_archive_title', function ($title) {
      if ( is_category() ) {
              $title = single_cat_title( '', false );
          } elseif ( is_tag() ) {
              $title = single_tag_title( '', false );
          } elseif ( is_author() ) {
              $title = '<span class="vcard">' . get_the_author() . '</span>' ;
          } elseif ( is_tax() ) { //for custom post types
              $title = sprintf( __( '%1$s' ), single_term_title( '', false ) );
          } elseif (is_post_type_archive()) {
              $title = post_type_archive_title( '', false );
          }
      return $title;
  });

// Shortcode for inserting the site's contact email
add_shortcode( 'contact_email', 'contact_email' );

function contact_email( $atts ) {
    return '23g@qrk.one'; // Change this value when it's time to update your email everywhere!
}

// Shortcode for generating the post title
add_shortcode( 'get_title', 'get_title' );

function get_title( $atts ) {
    return esc_attr( get_the_title( get_the_ID() ) );
}

// Add reply link to RSS feed
add_filter( "the_content_feed", "feed_comment_via_email" );

function feed_comment_via_email($content)
{
   $content .= "<p><a href=\"mailto: " . do_shortcode( ' [contact_email] ' ) . "?subject=Reply to '" . do_shortcode( ' [get_title] ' ) . "'" . "\">Reply via email</a></p>";
   return $content;
}

// Remove mobile menu and drop-down JS.
add_action( 'wp_enqueue_scripts', 'tu_remove_menu_scripts', 50 );
function tu_remove_menu_scripts() {
    wp_dequeue_script( 'generate-menu' );
    wp_dequeue_script( 'generate-dropdown' );
}

// Disable comments RSS feed
add_action( 'after_setup_theme', 'head_cleanup' );

function head_cleanup(){

    // Add default posts and comments RSS feed links to head.
    add_theme_support( 'automatic-feed-links' );

    // disable comments feed
    add_filter( 'feed_links_show_comments_feed', '__return_false' ); 

}

// Auto-generate titles on posts based on date and time
add_filter('default_title', function ($title) {
    global $post_type;
    if ('post' == $post_type) {
        return date('d F Y H:i');
    }
    return $title;
} );

I see. Will you be able to access it and remove it?

If you're not able to access your site, access the file through FTP.

I tested the code here and it's working from my end: https://generatepress.com/forums/topic/add-date-and-time-to-posts/#post-2597527

Can you check if there are any slanted quotation marks when your copying and pasting? Sometimes copying and pasting converts the straight quotation marks to slanted ones and that would cause issues.

Weirdly it worked the second time. No idea why. Maybe something did go wrong when I pasted it, sorry about that.

Anyway, it's in now, but the dates are still showing incorrectly. Here's an example - https://log.kevquirk.com/04-april-2023-2312/

As you can see, the date at the top of the page is working correctly, but the date in the comment meta is wrong.

I've managed to fix this by using a block element and the WP Date block and customising that. Thanks.

I see. Glad you resolved it! You're welcome, Kev!

This archived topic is closed to new replies.