Last Updated Date in SERPs & double h1 in single post

Hello Themeco,

I have 2 issues:

1.Following a tutorial I need to find this piece of code:

<?php the_time('F j Y'); ?> which should be in single.php and replace it with this:

Last updated on <time datetime="<?php the_modified_time('Y-m-d'); ?>"><?php the_modified_time('F jS, Y'); ?></time>

The goal is to show last update date in Google SERPs. (instead of the date when the post was published to help CTR)

I don’t have this line of code in the single.php file, so how can I do it ?

2. My second problem is about h1 tag in every type of page (single post, category page…).

I always have 2 h1 the name of my Website (only necessary on the homepage) and the name of the post / Category… (the good one).
How to get rid of the website name h1 in very type of page ?

Thank you

1 Like

Hi,

May I know what stack are you using?

Can you provide us your site url.

Thanks

Hello Paul,

I’m using Ethos :slight_smile:

Thank you

Hello There,

Thanks for writing in!

What you are trying to accomplish requires a template customization, we would highly to suggest that you use a child theme. This allows you to make code changes that won’t be overwritten when an X update is released. After your child theme is setup, please review how we recommend making template changes in Customization Best Practices.

1.) To resolve your issue, after the child theme is set up, please add the following code in your child theme’s functions.php file

// Add last updated time to single.php
// =============================================================================
function add_last_updated_time() { ?>

  <p><small>Last updated on <time datetime="<?php the_modified_time('Y-m-d'); ?>"><?php the_modified_time('F jS, Y'); ?></time></small></p>

<?php }
add_action('x_before_the_content_end', 'add_last_updated_time');
// =============================================================================

2.) To resolve your h1 issue, assuming you have your child theme active and ready, please follow the following steps below:
1] Using Notepad or TextEdit or Sublime Text or any text editor, please create a new file in your local machine.
2] Insert the following code into that new file

<?php

// =============================================================================
// VIEWS/GLOBAL/_BRAND.PHP
// -----------------------------------------------------------------------------
// Outputs the brand.
// =============================================================================

$site_name        = get_bloginfo( 'name' );
$site_description = get_bloginfo( 'description' );
$logo             = x_make_protocol_relative( x_get_option( 'x_logo' ) );
$site_logo        = '<img src="' . $logo . '" alt="' . $site_description . '">';

?>

<?php if ( is_home() || is_front_page() ) : ?>

  <?php echo '<h1 class="visually-hidden">' . $site_name . '</h1>'; ?>

<?php endif; ?>

<a href="<?php echo home_url( '/' ); ?>" class="<?php x_brand_class(); ?>" title="<?php echo $site_description; ?>">
  <?php echo ( $logo == '' ) ? $site_name : $site_logo; ?>
</a>

3] Save the file named as _brand.php
4] Upload this file to your server in the child theme’s folder wp-content/themes/x-child/framework/legacy/cranium/headers/views/global/

Hope this helps. Please let us know how it goes.

1 Like

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.

Opened as per user request

Hi there - I have added the last updated code which works great! But can you please show me how to get it to show at the top of the page rather than the bottom?
Thanks
Mel

Hi Mel,

On RueNel’s first given code, please update the x_before_the_content_end to x_before_the_content_begin

Thanks,

Perfect!! Thank you very much :slight_smile:

Glad we could help.

Cheers!

Just wondering how i would get the last updated date to show on pages only - it is currently also showing on posts?
Thanks
Mel

Hello Mel,

Thanks for updating this thread. To make sure that the last updated will only display in pages and not on post, please make use of this code instead:

// Add last updated time to page.php
// =============================================================================
function add_last_updated_time() { ?>

  <?php if ( is_page() ) : ?>

  	<p><small>Last updated on <time datetime="<?php the_modified_time('Y-m-d'); ?>"><?php the_modified_time('F jS, Y'); ?></time></small></p>

  <?php endif; ?>

<?php }
add_action('x_before_the_content_end', 'add_last_updated_time');
// =============================================================================

Hope this helps. Please let us know how it goes.

Perfect! Thank you very much :smile:

Glad we could be of a help. :slight_smile:

Hi,
First of all, thanks for great code and all support!

I have a question - is it possible to display the last updated date in line with post’s meta (so it’d look like ‘In [category] by [author] / [originally published date] / [last updated date] / [comments]’ ? (and will it work for display of last updated date in SERPs)?

Second question is, if I use the @RueNel 's code and and the date displays before content (with @friech 's code), is there a way to decrease the gap appearing between the words ‘Last updated on [date]’ and my content (which is a picture opening the post)?

Many thanks!
rayman_15

Hey @rayman_15,

I would assume that you are using Ethos stack. To have something like “In [category] by [author] / [originally published date] / [last updated date] / [comments]” in ethos, you do not need my code. The post meta functions should be mofied directory. If your child theme is already set up, please add the following code in your child theme’s functions.php file

// Custom Ethos Entry Meta
// =============================================================================

if ( ! function_exists( 'x_ethos_entry_meta' ) ) :
  function x_ethos_entry_meta() {

    //
    // Author.
    //

    $author = sprintf( ' %1$s %2$s</span>',
      __( 'by', '__x__' ),
      get_the_author()
    );


    //
    // Date.
    //

    $date = sprintf( '<span><time class="entry-date" datetime="%1$s">%2$s</time></span>',
      esc_attr( get_the_date( 'c' ) ),
      esc_html( get_the_date() )
    );


    //
    // Categories.
    //

    if ( get_post_type() == 'x-portfolio' ) {
      if ( has_term( '', 'portfolio-category', NULL ) ) {
        $categories        = get_the_terms( get_the_ID(), 'portfolio-category' );
        $separator         = ', ';
        $categories_output = '';
        foreach ( $categories as $category ) {
          $categories_output .= '<a href="'
                              . get_term_link( $category->slug, 'portfolio-category' )
                              . '" title="'
                              . esc_attr( sprintf( __( "View all posts in: &ldquo;%s&rdquo;", '__x__' ), $category->name ) )
                              . '"> '
                              . $category->name
                              . '</a>'
                              . $separator;
        }

        $categories_list = sprintf( '<span>%1$s %2$s',
          __( 'In', '__x__' ),
          trim( $categories_output, $separator )
        );
      } else {
        $categories_list = '';
      }
    } else {
      $categories        = get_the_category();
      $separator         = ', ';
      $categories_output = '';
      foreach ( $categories as $category ) {
        $categories_output .= '<a href="'
                            . get_category_link( $category->term_id )
                            . '" title="'
                            . esc_attr( sprintf( __( "View all posts in: &ldquo;%s&rdquo;", '__x__' ), $category->name ) )
                            . '"> '
                            . $category->name
                            . '</a>'
                            . $separator;
      }

      $categories_list = sprintf( '<span>%1$s %2$s',
        __( 'In', '__x__' ),
        trim( $categories_output, $separator )
      );
    }


    //
    // Comments link.
    //

    if ( comments_open() ) {

      $title  = apply_filters( 'x_entry_meta_comments_title', get_the_title() );
      $link   = apply_filters( 'x_entry_meta_comments_link', get_comments_link() );
      $number = apply_filters( 'x_entry_meta_comments_number', get_comments_number() );

      $text = ( 0 == $number ) ? __( 'Leave a Comment', '__x__' ) : sprintf( _n( '%s Comment', '%s Comments', $number, '__x__' ), $number );

      $comments = sprintf( '<span><a href="%1$s" title="%2$s" class="meta-comments">%3$s</a></span>',
        esc_url( $link ),
        esc_attr( sprintf( __( 'Leave a comment on: &ldquo;%s&rdquo;', '__x__' ), $title ) ),
        $text
      );

    } else {

      $comments = '';

    }


    //
    // Last Updated
    //
    $lastupdate = '<span><time datetime="' . the_modified_time('Y-m-d'). '">' . the_modified_time('F jS, Y') . '</time></span>';


    //
    // Output.
    //

    if ( x_does_not_need_entry_meta() ) {
      return;
    } else {
      printf( '<p class="p-meta">%1$s%2$s%3$s%4$s%5$s</p>',
        $categories_list,
        $author,
        $lastupdate,
        $date,
        $comments
      );
    }

  }
endif;
// =============================================================================

We would loved to know if this has work for you. Thank you.

1 Like

Many thanks, unfortunately the code you just provided isn’t working, what I get is something like this:

The last updated date is displayed below the post title, but not inline with other parts of meta

Hi there,

The code doesn’t have any echo to output the date under the title. Would you mind providing your entire child theme’s functions.php content? And please provide your FTP login credentials so I could test it.

Thanks!

Thanks for writing!

Unfortunately, I am not using any ftp client so I can’t provide you with credentials…

This is the current content of functions.php :

<?php

// =============================================================================
// FUNCTIONS.PHP
// -----------------------------------------------------------------------------
// Overwrite or add your own custom functions to Pro in this file.
// =============================================================================

// =============================================================================
// TABLE OF CONTENTS
// -----------------------------------------------------------------------------
//   01. Enqueue Parent Stylesheet
//   02. Additional Functions
// =============================================================================

// Enqueue Parent Stylesheet
// =============================================================================

add_filter( 'x_enqueue_parent_stylesheet', '__return_true' );



// Additional Functions
// =============================================================================


// Add last updated time to single.php
// =============================================================================
function add_last_updated_time() { ?>

  <p><small>Last updated on <time datetime="<?php the_modified_time('Y-m-d'); ?>"><?php the_modified_time('F jS, Y'); ?></time></small></p>

<?php }
add_action('x_before_the_content_end', 'add_last_updated_time');
// =============================================================================