WooCommerce 单个产品自定义选项卡显示页面内容

WooCommerce single product custom Tab displaying a page content

我已经为单个产品创建了一个自定义选项卡,我想通过下面的代码了解是否可以指定某个页面作为该选项卡的内容?

add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab' );

function woo_new_product_tab( $tabs ) {

  // Adds the new tab

  $tabs['test_tab'] = array(
    'title'     => __( 'Seller Disclosure', 'woocommerce' ),
    'priority'  => 50,
    'callback'  => 'woo_new_product_tab_content'
  );

  return $tabs;

}

function woo_new_product_tab_content() {

  // The new tab content

  echo '<h2>Coming Soon</h2>';

}

可以这样调用定义的 post ID(页面)的经典 WordPress post 内容:

//Add a new tab in single product pages
add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab' );
function woo_new_product_tab( $tabs ) {
    $tabs['test_tab'] = array(
        'title'     => __( 'Seller Disclosure', 'woocommerce' ),
        'priority'  => 50,
        'callback'  => 'woo_new_product_tab_content'
    );
    return $tabs;
}

// The tab content (with the page content)
function woo_new_product_tab_content() {
    // ==> ==> ==> ==> ==> ==> ==> Replace the ID HERE by your page ID
    $page_id = 324;
    $page_post_object = get_post( $page_id );

    // Get the page content
    $page_content = $page_post_object->post_content;

    // (optionally Get the page title
    $page_title = $page_post_object->post_title;

    // The title (Or the page title)
    echo '<h2>Coming Soon</h2>'; // Or: echo '<h2>' . $page_title . '</h2>';

    // The page content
    echo '<div class="page-content-container">' . $page_content . '</div>';
}

代码在您的活动子主题(或主题)的任何 php 文件或任何插件 php 文件中。

此代码已经过测试并且有效。