大家早上好,
我对 WooComerce 面包屑有疑问。我想在左侧添加当前页面/产品标题的面包屑栏。但我不知道如何才能获得这个头衔。我可以添加一些自定义文本并使用以下代码将其移至右侧:
'wrap_before' => '<nav class="woocommerce-breadcrumb"><h1 id="name">Some text</h1>',
'wrap_after' => '</nav>',
你能建议我一些解决方案来获得标题吗?
编辑:好吧,我设法获得一些临时结果,但它仅返回 WooComerce 页面名称(商店、类别名称),不显示产品名称。有什么建议吗?
代码如下:
<script type='text/javascript'>
/* <![CDATA[ */
jQuery(document).ready(function() {
jQuery("#name").append("<?php echo woocommerce_page_title();?>");
});
/* ]]> */
</script>
最佳答案
我将使用 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;
} );
关于php - 如何使用 WooCommerce 获取当前页面的标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33768972/