WooCommerce single product custom Tab displaying a

2019-08-03 07:02发布

问题:

I have created a custom tab for a single product and would like to know with the code below if there is a way to specify a certain page to act as the content for that tab?

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:

This is possible calling a classic WordPress post content for a defined post ID (page) this way:

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

Code goes in any php file of your active child theme (or theme) or also in any plugin php file.

This code Is tested and works.