問題描述

我目前在 WooCommerce 外掛的函式檔案中有這個程式碼。

function my_theme_wrapper_start()
{
  echo the_breadcrumb();
  echo '<section role="main"><div class="wrap">';
}

function my_theme_wrapper_end()
{
  echo '</div></section>';
}

function mytheme_prepare_woocommerce_wrappers()
{
  remove_action( 'woocommerce_before_main_content', 'woocommerce_output_content_wrapper', 10 );
  remove_action( 'woocommerce_after_main_content', 'woocommerce_output_content_wrapper_end', 10);

  add_action( 'woocommerce_before_main_content', 'my_theme_wrapper_start', 10 );
  add_action( 'woocommerce_after_main_content', 'my_theme_wrapper_end', 10 );
}

add_action( 'wp_head', 'mytheme_prepare_woocommerce_wrappers' );

add_theme_support( 'woocommerce' );

現在我想在 Section.main 類之外重新定位 WooCommerce 麵包屑,但我看不到如何做到這一點。有沒有辦法做到這一點?

這是我為 WooCommerce 麵包屑編寫的程式碼…

function woocommerce_breadcrumb( $args = array() ) {

    $defaults = apply_filters( 'woocommerce_breadcrumb_defaults', array(
        'delimiter'   => ' / ',
        'wrap_before' => '<nav class="bcrumb" itemprop="breadcrumb">',
        'wrap_after'  => '</nav>',
        'before'      => '',
        'after'       => '',
        'home'        => _x( 'Home', 'breadcrumb', 'woocommerce' ),
    ) );

    $args = wp_parse_args( $args, $defaults );

    woocommerce_get_template( 'shop/breadcrumb.php', $args );
}

我已經嘗試將 section.main 標籤放在’before’ 行中,但這只是在麵包屑中新增了一個 section 標籤,它不會重新定位導航痕跡。

最佳解決方案

好的,我似乎有工作。我在函式檔案中新增了這個…

//Reposition WooCommerce breadcrumb
function woocommerce_remove_breadcrumb(){
remove_action(
    'woocommerce_before_main_content', 'woocommerce_breadcrumb', 20);
}
add_action(
    'woocommerce_before_main_content', 'woocommerce_remove_breadcrumb'
);

function woocommerce_custom_breadcrumb(){
    woocommerce_breadcrumb();
}

add_action( 'woo_custom_breadcrumb', 'woocommerce_custom_breadcrumb' );

然後新增…

do_action('woo_custom_breadcrumb');

… 我想要麵包屑顯示。

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。