Php 如何在WooCommerce类别页面中添加元标题和元描述
我正在为我的网站的元标题和元描述使用多合一SEO包插件。我的网站有WooCommerce和多合一SEO包,不支持WooCommerce类别页面的元标题和元描述 所以,我使用下面的代码为管理区域中的WooCommerce类别的Meta Title和Meta Description创建自定义字段Php 如何在WooCommerce类别页面中添加元标题和元描述,php,wordpress,woocommerce,hook-woocommerce,Php,Wordpress,Woocommerce,Hook Woocommerce,我正在为我的网站的元标题和元描述使用多合一SEO包插件。我的网站有WooCommerce和多合一SEO包,不支持WooCommerce类别页面的元标题和元描述 所以,我使用下面的代码为管理区域中的WooCommerce类别的Meta Title和Meta Description创建自定义字段 function wh_taxonomy_add_new_meta_field() { ?> <div class="form-field"> <l
function wh_taxonomy_add_new_meta_field() {
?>
<div class="form-field">
<label for="wh_meta_title"><?php _e('Meta Title', 'wh'); ?></label>
<input type="text" name="wh_meta_title" id="wh_meta_title">
<p class="description"><?php _e('Enter a meta title, <= 60 character', 'wh'); ?></p>
</div>
<div class="form-field">
<label for="wh_meta_desc"><?php _e('Meta Description', 'wh'); ?></label>
<textarea name="wh_meta_desc" id="wh_meta_desc"></textarea>
<p class="description"><?php _e('Enter a meta description, <= 160 character', 'wh'); ?></p>
</div>
<?php
}
//Product Cat Edit page
function wh_taxonomy_edit_meta_field($term) {
//getting term ID
$term_id = $term->term_id;
// retrieve the existing value(s) for this meta field.
$wh_meta_title = get_term_meta($term_id, 'wh_meta_title', true);
$wh_meta_desc = get_term_meta($term_id, 'wh_meta_desc', true);
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="wh_meta_title"><?php _e('Meta Title', 'wh'); ?></label></th>
<td>
<input type="text" name="wh_meta_title" id="wh_meta_title" value="<?php echo esc_attr($wh_meta_title) ? esc_attr($wh_meta_title) : ''; ?>">
<p class="description"><?php _e('Enter a meta title, <= 60 character', 'wh'); ?></p>
</td>
</tr>
<tr class="form-field">
<th scope="row" valign="top"><label for="wh_meta_desc"><?php _e('Meta Description', 'wh'); ?></label></th>
<td>
<textarea name="wh_meta_desc" id="wh_meta_desc"><?php echo esc_attr($wh_meta_desc) ? esc_attr($wh_meta_desc) : ''; ?></textarea>
<p class="description"><?php _e('Enter a meta description', 'wh'); ?></p>
</td>
</tr>
<?php
}
add_action('product_cat_add_form_fields', 'wh_taxonomy_add_new_meta_field', 10, 1);
add_action('product_cat_edit_form_fields', 'wh_taxonomy_edit_meta_field', 10, 1);
// Save extra taxonomy fields callback function.
function wh_save_taxonomy_custom_meta($term_id) {
$wh_meta_title = filter_input(INPUT_POST, 'wh_meta_title');
$wh_meta_desc = filter_input(INPUT_POST, 'wh_meta_desc');
update_term_meta($term_id, 'wh_meta_title', $wh_meta_title);
update_term_meta($term_id, 'wh_meta_desc', $wh_meta_desc);
}
add_action('edited_product_cat', 'wh_save_taxonomy_custom_meta', 10, 1);
add_action('create_product_cat', 'wh_save_taxonomy_custom_meta', 10, 1);
function wh\u taxonomy\u add\u new\u meta\u field(){
?>
//在产品类别存档页面上显示详细信息
添加操作(
“一次又一次的购物”,
“wpm\u产品\u目录\u存档\u添加\u元”
);
功能wpm\u产品\u目录\u存档\u添加\u元数据(){
$t\u id=get\u queryed\u object()->term\u id;
$term_meta=get_选项(“分类法_u$t_id”);
$term_meta_content=$term_meta['custom_term_meta'];
如果($term\u meta\u content!=''){
回声';
回显应用过滤器('the_content',$term_meta_content);
回声';
}
}
您可以使用以下挂钩在前端显示自定义元标题和元描述
add_action('woocommerce_after_shop_loop','display_custom_meta_info');
function display_custom_meta_info(){
global $wp_query;
$cat_obj = $wp_query->get_queried_object();
$title_meta = get_term_meta($cat_obj->term_id
,'wh_meta_title',true);
$desc_meta = get_term_meta($cat_obj->term_id
,'wh_meta_desc',true);
$woocommerce_taxonomy_archive_description = $title_meta.$desc_meta;
return $woocommerce_taxonomy_archive_description;
}
您始终可以使用其他挂钩来修改图元的显示,方法如下:
由于您使用的是一体式SEO,因此没有任何wp默认挂钩/过滤器
将工作作为这个插件修改所有的标题,所以你必须使用
,筛选元标题。和元标题
您必须使用的描述
对于元标题
add_filter('aioseop_title', 'wh_alter_pro_cat_title', 1);
function wh_alter_pro_cat_title($title)
{
global $paged;
if (is_product_category())
{
$page = get_query_var('page');
if ($paged > $page)
{
$page = $paged;
}
$term = get_queried_object();
$title = get_term_meta($term->term_id, 'wh_meta_title', true);
$title = !empty($title) ? $title : $term->name;
$page_part = (!empty($page) && ($page > 1)) ? ' | ' . 'Page ' . $page : '';
$title .= ' | ' . get_bloginfo('name') . $page_part;
}
return $title;
}
用于元描述
add_action('wp_head', 'wh_alter_pro_cat_desc', 5);
function wh_alter_pro_cat_desc()
{
if (is_product_category())
{
$term = get_queried_object();
$productCatMetaDesc = get_term_meta($term->term_id, 'wh_meta_desc', true);
if (empty($productCatMetaDesc))
return;
?>
<meta name="description" content="<?= $productCatMetaDesc; ?>">
<?php
}
}
add_action('wp_head','wh_alter_pro_cat_desc',5);
函数wh_alter_pro_cat_desc()
{
如果(是产品类别())
{
$term=get_queryed_object();
$productCatMetaDesc=get_term_meta($term->term_id,'wh_meta_desc',true);
if(空($productCatMetaDesc))
返回;
?>
检查里面的代码,archive-product.php全局$wp_query;$cat_query=$wp_query->get_queryed_object();$title_meta=get_post_meta($cat_obj->term_id;,'wh_meta_title',true);$desc meta get_post_meta($cat_obj->term_id;'wh meta u desc true)在catid中,你能现在检查代码全局$wp_query;$cat_obj=$wp_query->get_queryed_object();$title_meta=get_post_meta($cat_obj->term_id,'wh_meta_title',true)$desc meta get_post_meta($cat_obj->term_id,'wh meta desc true);还尝试将静态类别ID替换为$cat_obj->term_ID,并检查是否有问题。我已经删除了;在放置上述代码之前,它仍然不工作。获得更正的全局$wp_query;$cat_obj=$wp_query->get_queryed_object();$title_meta=get_term_meta($cat_obj->term_ID,'wh meta_title',true)$desc\u meta=get\u term\u meta($cat\u obj->term\u id,'wh\u meta\u desc',true);这应该一直有效,直到运气不好,我已经把上面的代码放在了wp content/plugins/woocommerce/templates/archive-product.php的顶部,这是正确的位置吗?你使用过任何seo插件吗?是的,在一个seo包中,很长一段时间以前,当我为我的一个客户开发一个Woo商店时,我遇到了同样的问题,我没有得到任何有用的解决方案,所以我写了t帽子教程和答案。很高兴它能帮助你。别忘了投票并接受我的答案。
add_action('wp_head', 'wh_alter_pro_cat_desc', 5);
function wh_alter_pro_cat_desc()
{
if (is_product_category())
{
$term = get_queried_object();
$productCatMetaDesc = get_term_meta($term->term_id, 'wh_meta_desc', true);
if (empty($productCatMetaDesc))
return;
?>
<meta name="description" content="<?= $productCatMetaDesc; ?>">
<?php
}
}