0
votes

I'm looking for solution to display products published date on shop page.

I tried the following code, but it show only for first product

Any help will be greatful

add_action( 'woocommerce_after_shop_loop_item', 'wc_shop_page_product_date', 100 );
function wc_shop_page_product_date() {
    echo the_date('', '<span class="date_published">Published on: ', '</span>', false);
}
2
Try changing the function body to global $product; echo get_the_date('Y-m-d', $product->get_id()); Here is a guide on customizing the format: codex.wordpress.org/Formatting_Date_and_TimeSaqib Amin
Tested your code and it works on all products.LoicTheAztec
Great, working now, but no "Published on:" ?Dau

2 Answers

1
votes

the_date function assumes you are inside a loop that is properly setting up the post data, which in your case seems not to be done. You could manually provide the product id using $product object and use get_the_date() function instead.

Below is the solution that should do for you:

add_action( 'woocommerce_after_shop_loop_item', 'wc_shop_page_product_date', 100 );
function wc_shop_page_product_date() {
    global $product;
    echo '<span class="date_published">Published on: ' . get_the_date('Y-m-d', $product->get_id()) . '</span>';
}

Here is a guide on customizing the date format: codex.wordpress.org/Formatting_Date_and_Time

0
votes
add_action( 'woocommerce_after_shop_loop_item', 'wc_shop_page_product_date', 100 );
function wc_shop_page_product_date() {
  
    echo '<span class="date_published">Published on: ' . get_the_date('Y-m-d', get_the_ID()) . '</span>';
}

This is more efficient than using global $product;