Hướng dẫn how do i get the date and time to show on wordpress? - Làm cách nào để hiển thị ngày và giờ trên wordpress?

Adding the date and time to the WordPress Loop seems like a simple enough task, right?

Well, yes. It can be a case of coding a simple template tag and letting WordPress do the work for you. But sometimes you can run into problems along the way.

In this quick tip, I'll show you the different functions WordPress gives you for displaying the date and time and tell you which ones to use if you run into snags.

If you need a quick refresher on using PHP for WordPress, check out my free course on the topic.

The Available Template Tags

WordPress gives you four functions to output the date and/or time. These are:

  • the_date(): By default, it will echo the date of the post in the format F j, Y, so if the post was published on 20 November 2018, it would echo November 20, 2018.
  • get_the_date(): This fetches the date and doesn't echo it out. To echo it, you'd use echo get_the_date(), which gives you the same result as the_date(). It's useful if you're already using echo in your code. It can also help you get round the problem of dates not being displayed, as you'll see shortly.
    • 'post', 'category_name' => 'newsletters', 'posts_per_page' => 8 ) ); while ($query->have_posts()) : $query->the_post(); ?>
    0 and 
      'post', 'category_name' => 'newsletters', 'posts_per_page' => 8 ) ); while ($query->have_posts()) : $query->the_post(); ?>
    1: These fetch the time by default, but if you specify date formatting, you can also include the date. You could even use this just to output the date if you configured the formatting to do so, but it would more sense to use the_date() or echo get_the_date().

Formatting the Date

Each function has a default format, which you can override if you need to. To do this, you'll need to use standard PHP date and time formatting.

Here are some examples, all for a post published on 20 November 2018.

  • the_date() would output November 20, 2018 by default.
    • 'post', 'category_name' => 'newsletters', 'posts_per_page' => 8 ) ); while ($query->have_posts()) : $query->the_post(); ?>
    5 would output Tuesday, 20th Nov 2018.
    • 'post', 'category_name' => 'newsletters', 'posts_per_page' => 8 ) ); while ($query->have_posts()) : $query->the_post(); ?>
    6 would output 2:03 pm.
    • 'post', 'category_name' => 'newsletters', 'posts_per_page' => 8 ) ); while ($query->have_posts()) : $query->the_post(); ?>
    7 would output 14:03.
    • 'post', 'category_name' => 'newsletters', 'posts_per_page' => 8 ) ); while ($query->have_posts()) : $query->the_post(); ?>
    8 would output 2:03 pm, Tues, 20 November 18.

Troubleshooting Dates in the Loop: Missing Dates in an Archive Page

If you are using the_date() to output dates on an archive page, and you find that the date isn't being displayed with some posts, that's because the_date() doesn't repeat the date for subsequent posts published on the same day as a previous one.

Sometimes you might want to keep it like this, if you don't want to repeat the date for every post published that day. But if you want to ensure that all posts have their date output with the title and any other content you're outputting, you'll need to use another function. You can use any of the other three functions above.

The simplest option is to replace the_date() with echo get_the_date(). If you wanted to add the time, either 

    'post', 'category_name' => 'newsletters', 'posts_per_page' => 8 ) ); while ($query->have_posts()) : $query->the_post(); ?>
0 or 
4 will work.

Note: If you're puzzled by the fact that the_date() throws up this problem but 

    'post', 'category_name' => 'newsletters', 'posts_per_page' => 8 ) ); while ($query->have_posts()) : $query->the_post(); ?>
0 doesn't, it's because posts published on the same date weren't published at the same time. You'd have to go to a lot of effort, either scheduling posts, editing the publication times, or co-ordinating efforts between two bloggers, for that to happen!

I had this problem in the front page template of a theme I coded for a client. In this template were a number of loops, all leading to different content types on the site and all coded using 

7. The problem wasn't apparent until the day they added two posts (not something they normally did). They were puzzled as to why the second post's date didn't appear on the home page and kept editing it, refreshing it, and republishing it until they gave up and asked me.

Here's the original code:

    'post', 'category_name' => 'newsletters', 'posts_per_page' => 8 ) ); while ($query->have_posts()) : $query->the_post(); ?>

I edited the function so it read like this:

    'post', 'category_name' => 'newsletters', 'posts_per_page' => 8 ) ); while ($query->have_posts()) : $query->the_post(); ?>

In the line beginning 

8, I replaced 
9 with 
', '' );

// The same thing can be achieved by writing the span tags around the get_the_date() and get_the_time() functions.
echo ''.get_the_date( 'j F, Y').'';
echo ''.get_the_time( 'j F, Y').'';

?>
0. It fixed the problem.

So if you ever find that dates aren't showing up in your archives, this might be the solution.

Some Tips for Displaying the Date and Time

As mentioned earlier in the article, you can use four different functions to output the publication date for posts on archive pages.

Using the the_date() and get_the_date() Functions

Using the_date() will output the date, but it will only output the date once for multiple posts published on the same day. The function get_the_date() returns the date for all posts, even if they are published on the same date. However, you need to use echo get_the_date() to output the date.

Using the 
    'post', 'category_name' => 'newsletters', 'posts_per_page' => 8 ) ); while ($query->have_posts()) : $query->the_post(); ?>
0 and 
    'post', 'category_name' => 'newsletters', 'posts_per_page' => 8 ) ); while ($query->have_posts()) : $query->the_post(); ?>
1 Functions

The function 

    'post', 'category_name' => 'newsletters', 'posts_per_page' => 8 ) ); while ($query->have_posts()) : $query->the_post(); ?>
0 will also output the date separately for each post. However, this function will output the time according to the format specified as the value of 
', '' );

// The same thing can be achieved by writing the span tags around the get_the_date() and get_the_time() functions.
echo ''.get_the_date( 'j F, Y').'';
echo ''.get_the_time( 'j F, Y').'';

?>
9 option by default. You can use it to output the date by calling it as the_date()0. The 
    'post', 'category_name' => 'newsletters', 'posts_per_page' => 8 ) ); while ($query->have_posts()) : $query->the_post(); ?>
1 function works in a similar manner, but it returns the date instead of outputting it. This means that you will have to use the_date()2 to output the date. You could simply replace all occurrences of the_date()3 with the_date()0 wherever you want to show the date.

Các tham số bổ sung cho & nbsp; ________ 4 & nbsp;

Không giống như các chức năng khác, & nbsp; ____ 4 & nbsp; chấp nhận bốn tham số. Các tham số thứ hai và thứ ba được sử dụng để chỉ định chuỗi sẽ được hiển thị trước và sau ngày. Chức năng tương tự có thể được sao chép cho các chức năng khác bằng cách sử dụng mã bên dưới:

', '' );

// The same thing can be achieved by writing the span tags around the get_the_date() and get_the_time() functions.
echo ''.get_the_date( 'j F, Y').'';
echo ''.get_the_time( 'j F, Y').'';

?>

Nếu bạn muốn tìm hiểu thêm về WordPress, hãy xem các khóa học và hướng dẫn khác của chúng tôi ở đây trên Envato Tuts+!

Bài đăng này đã được cập nhật với các đóng góp từ & NBSP; Monty Shokeen. Monty là một nhà phát triển đầy đủ, người cũng thích viết các hướng dẫn và tìm hiểu về các thư viện JavaScript mới.

Bạn có thấy bài đăng này hữu ích?

Hướng dẫn how do i get the date and time to show on wordpress? - Làm cách nào để hiển thị ngày và giờ trên wordpress?

Nhà phát triển và nhà văn WordPress, Birmingham UK

Rachel McCollin là một nhà phát triển WordPress, người viết sách, bài báo và hướng dẫn về thiết kế và phát triển web, tập trung vào WordPress và phát triển đáp ứng và di động. Cô điều hành một cơ quan thiết kế web ở Birmingham, UK và đã xuất bản ba cuốn sách về WordPress, bao gồm WordPress: Đẩy các giới hạn, một nguồn tài nguyên tiên tiến cho các nhà phát triển WordPress. Cô hiện đang viết cuốn sách thứ tư của mình.

Làm cách nào để có được ngày và thời gian trong WordPress?

WordPress cung cấp cho bạn bốn chức năng để xuất ngày và/hoặc thời gian.Đó là: The_date (): Theo mặc định, nó sẽ lặp lại ngày của bài đăng ở định dạng f j, y, vì vậy nếu bài đăng được xuất bản vào ngày 20 tháng 11 năm 2018, nó sẽ lặp lại ngày 20 tháng 11 năm 2018.Lấy ngày và không lặp lại nó.the_date() : By default, it will echo the date of the post in the format F j, Y , so if the post was published on 20 November 2018, it would echo November 20, 2018. get_the_date() : This fetches the date and doesn't echo it out.

Làm cách nào để hiển thị thời gian trên WordPress?

Công cụ cho quản trị viên..
Chuyển đến Bộ điều khiển Cài đặt trên mạng từ bảng điều khiển WordPress của bạn ..
Trong các cài đặt chung của người Viking, bạn sẽ thấy một hộp thả xuống cho Time Timezone.Nhấp vào hộp này và chọn vùng chính xác cho vị trí của bạn.....
Chọn định dạng ngày ngày mà bạn muốn hiển thị ..
Chọn định dạng thời gian của người Viking.