WooCommerce类别页面上的自定义页面标题和描述

问题描述 投票:0回答:1

我使用高级自定义字段在我的所有页面,帖子,类别和产品上添加自定义页面标题字段。

为了删除默认的<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/branded-medicines

在类别页面上,它似乎取自页面上显示的第一个产品的标题...对于上面的页面,页面标题显示为:Bimizza(Desogestrel Ethinylestradiol)平板电脑品牌医药|晨兴药业

而不仅仅是:品牌医学|晨兴药业

我认为函数中的wp_head会定位所有Wordpress页面的头部,然后删除标题标签并添加自定义标签......实际上这似乎正确地做了这个,但它只是从第一个产品而不是从第一个产品添加数据类别。该类别看起来像这是任何帮助:

enter image description here

其中的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' );
php wordpress advanced-custom-fields
1个回答
2
投票

the_field()只能在循环中使用,这就是为什么它正在这样做,因为它从当前循环项目中提取数据。如果要定位分类法的字段,则需要将对该分类法的引用传递给该函数。

我建议这样的事情:

$term = get_queried_object();
the_field('seo_page_title', $term);

这是ACF文档中的相关页面:https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/

© www.soinside.com 2019 - 2024. All rights reserved.