php - WooCommerce 类别页面上的自定义页面标题和描述
问题描述
我已使用高级自定义字段在我的所有页面、帖子、类别和产品上添加自定义页面标题字段。
为了删除默认<title>
标签并确保该字段正确通过,我在我的 functions.php 文件中使用了以下函数:
/* Remove Default <title> tag */
remove_action( 'wp_head', '_wp_render_title_tag', 1 );
// Add new <title> and description tags
function child_theme_head_script() { ?>
<title><?php the_field('seo_page_title'); ?></title>
<meta name="description" content="<?php the_field('seo_page_description'); ?>"/>
<!-- Your HTML goes here -->
<?php }
add_action( 'wp_head', 'child_theme_head_script' );
除了我的类别页面之外,这在整个网站上都可以正常工作:http: //staging.morningsidepharm.com/products/brand-medicines
在类别页面上,它似乎取自页面上出现的第一个产品的标题... 对于上面的页面,页面标题显示为:Bimizza(去氧孕烯炔雌醇)片剂品牌药 | 晨兴制药
不仅仅是:品牌药| 晨兴制药
我认为wp_head
在函数中会针对所有 Wordpress 页面的头部,然后删除标题标签并添加自定义标签......实际上似乎这样做是正确的,但它只是添加来自第一个产品而不是类别的数据. 该类别看起来像这样,它有什么帮助:
其中的 SEO 文件对应于:<?php the_field('seo_page_title'); ?>
和<?php the_field('seo_page_description'); ?>
谁能指出我正确的方向?我不确定我要去哪里错了......
** 更新 ** ** 更新 **
我已经尝试过这个,但它似乎没有效果......
/* Remove Default <title> tag */
remove_action( 'wp_head', '_wp_render_title_tag', 1 );
// Add new <title> and description tags
$term = get_queried_object();
$title = get_field('seo_page_title', $term);
$desc = get_field('seo_page_description', $term);
function child_theme_head_script() { ?>
<title><?php echo $title; ?></title>
<meta name="description" content="<?php echo $desc; ?>"/>
<!-- Your HTML goes here -->
<?php }
add_action( 'wp_head', 'child_theme_head_script' );
解决方案
the_field()
只能在循环中使用,这就是它这样做的原因,因为它从当前循环项中提取数据。如果要定位分类的字段,则需要将对该分类的引用传递给函数。
我建议这样的事情:
$term = get_queried_object();
the_field('seo_page_title', $term);
这是 ACF 文档中的相关页面:https ://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/
推荐阅读
- php - 如何将表单验证错误放在警告框中?
- linux - Spring Boot Index jsp 在本地主机上工作,但在 linux 服务器中找不到
- selenium - 通过 PID 进程杀死 GeckoDriver [Java]
- java - 无法将 Jersey 重定向迁移到 Spring 重定向
- php - Nginx 位置正在重定向到主 proxy_pass
- drop-down-menu - 如何在下拉列表中仅显示已安装模块的菜单
- ios - 为应用内购买配置了所有设置,但我的产品数量仍然为 0 - Swift 4
- ajax - 关于 Framework7-vue-cli Ajax 拦截器
- jsoup - Jsoup 不读取/加载整个页面
- leaflet - 传单地图上下文菜单位置