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

时间:2017-07-08 17:29:15

标签: php wordpress woocommerce tabs product

我为单个产品创建了一个自定义标签,如果有办法指定某个页面作为该标签的内容,我想知道下面的代码吗?

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>';

}

1 个答案:

答案 0 :(得分:1)

这可以通过这种方式为定义的帖子ID(页面)调用经典的WordPress帖子内容:

//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文件。

此代码经过测试并有效。

相关问题