Site logo

[Resolved] Add date AND time to posts?

Home Forums Support [Resolved] Add date AND time to posts?

Home Forums Support Add date AND time to posts?

Viewing 13 posts - 1 through 13 (of 13 total)
  • Author
    Posts
  • #2596559
    Kev

    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

    #2596560
    Kev

    Forgot to enable notifications…

    #2596569
    Kev

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

    #2596693
    Ying
    Staff
    Customer Support

    Glad you’ve figured it out 🙂

    #2597490
    Kev

    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

    #2597527
    Fernando
    Customer Support

    Hi Kev,

    Can you try adding this snippet:

    add_filter( 'get_comment_date', 'db_reformat_comment_date' );	
    function db_reformat_comment_date( $dateForm ) {
        $dateForm  = date("j F Y H:i");	
        return $dateForm;
    }

    Adding PHP: https://docs.generatepress.com/article/adding-php/#code-snippets

    #2597537
    Kev

    Adding that caused a critical error on my site. 🙁

    #2597556
    Fernando
    Customer Support

    May we know how you added it?

    #2597568
    Kev

    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;
    } );
    
    #2597572
    Fernando
    Customer Support

    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.

    #2597583
    Kev

    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.

    #2597812
    Kev

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

    #2598760
    Fernando
    Customer Support

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

Viewing 13 posts - 1 through 13 (of 13 total)
  • You must be logged in to reply to this topic.