我需要获取 WooCommerce 中商店页面的标题。我的意思是页面:
site.com/shop/
我试过这个:
global $post;
if ( class_exists( 'WooCommerce' ) && is_shop() ) :
$post_type = get_post_type_object( get_post_type( $post ) );
$title = $post_type -> labels -> name;
endif;
这里将使用变量
$title
:
<title><?php //custom title ?></title>
但它实际上给出了对象的名称,而页面的名称不同。
另一个解决方案是制作一个
header-shop.php
文件。但到目前为止,这对我来说不起作用,我为所有类型的页面创建了一个自定义函数,这些函数也将在其他网站上使用。
所以,我只需要获取商店页面的标题,就像我使用函数
get_the_title()
获取 WordPress 页面的标题一样。
我已经找到解决办法了。如果函数
get_the_title
与参数 get_option( 'woocommerce_shop_page_id' )
一起使用,则可以很好地用于商店页面
global $post;
if ( class_exists( 'WooCommerce' ) && is_shop() ) :
$title = get_the_title( get_option( 'woocommerce_shop_page_id' ) );
endif;