php - 如何使用 WooCommerce 获取当前页面的标题

标签 php wordpress woocommerce breadcrumbs

大家早上好,
我对 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/

相关文章:

jquery - 检测 WordPress 编辑帖子页面的富文本编辑器 (tinyMCE) 中的更改事件

wordpress - WooCommerce 自定义支付网关

javascript - 使用 Bootstrap 将所有对象对齐在 div 的中间

wordpress - Woocommerce新客户管理员通知电子邮件

php - Woocommerce:要替换哪个 Hook 已弃用 "woocommerce_add_order_item_meta"

带循环的 PHP cURL

php - UTF-8 xml 文件显示乱码

php - 选择 ID 存在于另一列 MySQL 中的项目

php - jquery中如何读取json数组中的数据?

javascript - 重复 innerHTML.replace