I use the plugin wp relative date to display such as Today, Yesterday. But there are problems with dates later, there are brackets

12/11/2015 () 22:10

his code

$output = $before.$the_date.' ('.sprintf(_n('', '', $day_diff, 'wp-relativedate'), number_format_i18n($day_diff)).')'.$after; } 

which I did not try to remove as a current, but it did not work out. As well as to make it so that if this date was the day before 13/12/2015 did not show the time, I use the simple time the_time

 <div class="meta"> <?php relative_post_the_date(); ?> <?php the_time( $d ); ?> </div> 

The function itself looks like this.

 ### Alternative To WordPress the_date(). function relative_post_the_date($d = '', $before = '', $after = '', $display_ago_only = false, $display = true) { global $post; if (empty($d)) { $the_date = mysql2date(get_option('date_format'), $post->post_date); } else { $the_date = mysql2date($d, $post->post_date); } if(gmdate('Y', current_time('timestamp')) != mysql2date('Y', $post->post_date, false)) { $output = $before.$the_date.$after; } else { $day_diff = (gmdate('z', current_time('timestamp')) - mysql2date('z', $post->post_date, false)); if($day_diff < 0) { $day_diff = 32; } if($day_diff == 0) { $output = $before.__('Сегодня', 'wp-relativedate').$after; } elseif($day_diff == 1) { $output = $before. __('Вчера', 'wp-relativedate').$after; } elseif ($day_diff < 7) { if($display_ago_only) { $output = $before.sprintf(_n('', '', $day_diff, 'wp-relativedate'), number_format_i18n($day_diff)).$after; } else { $output = $before.$the_date.' ('.sprintf(_n('', '', $day_diff, 'wp-relativedate'), number_format_i18n($day_diff)).')'.$after; } } elseif ($day_diff < 31) { if($display_ago_only) { $output = $before.sprintf(_n('', '', ceil($day_diff/7), 'wp-relativedate'), number_format_i18n(ceil($day_diff/7))).$after; } else { $output = $before.$the_date.' ('.sprintf(_n('', '', ceil($day_diff/7), 'wp-relativedate'), number_format_i18n(ceil($day_diff/7))).')'.$after; } } else { $output = $before.$the_date.$after; } } if($display) { echo $output; } else { return $output; } } 
  • Explain where brackets are? - tutankhamun
  • @tutankhamun damn forgot completely, here indicated - reddyk

1 answer 1

Brackets are displayed in two places.

  } elseif ($day_diff < 7) { ... } else { /* ---> тут */ $output = $before.$the_date.' ('.sprintf(_n('', '', $day_diff, 'wp-relativedate'), number_format_i18n($day_diff)).')'.$after; } } elseif ($day_diff < 31) { ... } else { /* ---> и тут */ $output = $before.$the_date.' ('.sprintf(_n('', '', ceil($day_diff/7), 'wp-relativedate'), number_format_i18n(ceil($day_diff/7))).')'.$after; } } else { 

Remove from both lines and they will not be displayed (well, if you have page caching, you need to clear the cache, otherwise you will be struggling with windmills for a very long time)

With time, this is not very good. We need verification, which can, of course, be inserted into the <?php the_time( $d ); but it will turn out quite cumbersome. You can write your function in the functions.php file of the current theme and call it instead of the_time() .

For example:

 function special_post_time($d = '') { global $post; $day_diff = (gmdate('z', current_time('timestamp')) - mysql2date('z', $post->post_date, false)); if ($day_diff < 2) { the_time($d); } } 
  • deleted, nothing has changed, nothing caching is not put on wp. - reddyk
  • And everything appeared. The only question that remains is how to make sure that if the date is 24 hours old or older, then do not show the time? - reddyk
  • @reddyk Disconnect on JavaScript page and look again - tutankhamun