在WooCommerce页面上显示创建和修改的产品日期 [英] Show created and modified product date on WooCommerce pages

查看:68
本文介绍了在WooCommerce页面上显示创建和修改的产品日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找在商店页面上显示产品当前日期的解决方案.

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

我已经尝试过了,但是什么也没显示

I have tried with this, but it shows nothing at all

add_action( 'woocommerce_single_product_summary','bloomer_echo_product_date',25 );
 
function bloomer_echo_product_date() {
if ( is_product() ) {
   echo get_the_date('', '<span class="date_published">Updated on: ', '</span>', false);
   }
}


感谢您的帮助.


Any help is appreciated.

推荐答案

以下代码显示了在单个产品页面和归档/商店页面上创建和修改产品的日期

The following code shows the date the product was created and modified on the single product page and archive/Shop page

注意:时间/日期显示可以根据

Note: the time/date display can be adjusted based on DateTime::format PHP function.

当前使用的

  • Y-年份的全数字表示形式,为4位数字
  • m-一个月的数字表示,前导零
  • d-每月的某天,两位数字,前导零
  • H-带有前导零的小时的24小时格式
  • i-以零开头的分钟
  • s-带有前导零的秒数

所以您得到:

function woocommerce_product_date() {
    // Get the global product object
    global $product;

    // Is a WC product
    if ( is_a( $product, 'WC_Product' ) ) {
        // Output date created & date modified
        echo sprintf( '<p>' . __( 'Date created: %s', 'woocommerce' ) . '</p>', $product->get_date_created()->date( 'Y-m-d H:i:s' ) );
        echo sprintf( '<p>' . __( 'Date modified: %s', 'woocommerce' ) . '</p>', $product->get_date_modified()->date( 'Y-m-d H:i:s' ) );
    }
}
// Display on single product page
add_action( 'woocommerce_single_product_summary', 'woocommerce_product_date', 10, 0 );
// Display on archive/shop page
add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_product_date', 10, 0 );

这篇关于在WooCommerce页面上显示创建和修改的产品日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆