首页 文章

如何使用WooCommerce获取当前页面的 Headers

提问于
浏览
2

大家早上好,
我有WooComerce面包屑的问题 . 我想在左侧添加带有当前页面/产品 Headers 的breadcrumbs栏 . 但我不知道我怎么能得到这个头衔 . 我能够添加一些自定义文本并使用以下代码将其移动到右侧:

'wrap_before' => '<nav class="woocommerce-breadcrumb"><h1 id="name">Some text</h1>',
 'wrap_after'  => '</nav>',

你能告诉我一些获得头衔的解决方案吗?

EDIT: Okey我设法得到一些临时结果,但它只返回WooComerce页面名称(商店,类别名称)它不显示产品名称 . 有什么建议 ?

这是代码:

<script type='text/javascript'>
   /* <![CDATA[ */                      
      jQuery(document).ready(function() {
         jQuery("#name").append("<?php echo woocommerce_page_title();?>");
                                    });
                                    /* ]]> */
                                </script>

1 回答

  • 2

    我会使用 woocommerce_get_breadcrumb 过滤器来修改发送到模板的数组的内容 . 您可以从最后获取页面名称并将其放在开头,或添加您想要的任何其他面包屑 . 每个元素都是一个数组,第一个元素是文本,第二个元素是链接 .

    // filter the array that is sent to the breadcrumbs template
    add_filter( 'woocommerce_get_breadcrumb', function( $crumbs ){
        // remove the last element (this is the page name by default)
        $page_crumb = array_pop( $crumbs );
    
        // add it back to the beginning of the array. 
        // you can change this to whatever you want, it's an array( 'Text', '/link' )
        array_unshift( $crumbs, $page_crumb );
    
        // return the modified array
        return $crumbs;
    } );
    

相关问题