【问题标题】:add_action to specific page type or post ID on WordPressadd_action 到特定页面类型或 WordPress 上的帖子 ID
【发布时间】:2020-10-05 19:37:47
【问题描述】:

我正在使用以下 WooCommerce 短代码从页面上的某个类别输出产品数据:

<?php echo do_shortcode('[products category="'. $category .'" columns="1"]'); ?>

默认情况下,此代码不包含产品简短描述,但我可以在functions.php文件中添加以下内容以启用简短描述:

add_action('wc_single_excerpt' , 'woocommerce_template_single_excerpt', 10 , 2);

但是,这将在每个我不想做的 WooCommerce 产品循环中输出产品简短描述。

我的问题是,我怎样才能只输出产品简短描述,即例如 ID 为 123 的页面上的 add_action?

【问题讨论】:

    标签: php wordpress


    【解决方案1】:

    有条件地调用这个函数woocommerce_template_single_excerpt

    add_action('wc_single_excerpt' , function() {
       // if it's in a loop
       if( get_the_ID() == 123 ) {
         woocommerce_template_single_excerpt();
       }
    }, 10 , 2);
    

    如果它回答了你的问题,请告诉我

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-02
      相关资源
      最近更新 更多