2017-05-22 77 views
1

我在Meta標題& Meta元描述中使用了所有內容。我的網站WooCommerce和所有在一個搜索引擎優化包不支持元標題& WooCommerce類別頁面的元描述。如何在WooCommerce類別頁面中添加元標題和元描述

因此,我使用下面的代碼爲Meta標題&爲管理區域中的WooCommerce類別創建了自定義字段。

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); 

上面的代碼在管理區域完美地工作。但如何顯示輸入的元標題&在類別頁面前端的元描述?

我應該在functions.php文件中添加什麼鉤子以便它顯示在前端的類別頁面中?

+0

你使用任何seo插件? –

+0

是所有在一個包裝 – Manoj

回答

2

修改METAS的顯示/過濾 將工作由於此插件修改了所有標題,所以您必須使用 aioseop_title,爲Meta標題過濾。對於Meta 描述你必須使用wp_head

對於元標題

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; 
} 

對於meta描述

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 
    } 
} 

的所有代碼都在functions.php文件的活躍兒童主題(或主題)的。或者也可以在任何插件php文件中使用。
代碼已經過測試和工作。

有用的鏈接:

希望這有助於!

+0

謝謝你!它的工作... – Manoj

+0

當我爲我的一個客戶開發一個Woo商店時,我很久以前面臨同樣的問題,而且我沒有得到任何有用的解決方案,所以我編寫了該教程和答案。並很高興它可以幫助你。不要忘記加入並接受我的回答。 –

+0

已經upvoted和接受 – Manoj

0

請檢查:

// Display details on product category archive pages 
add_action('woocommerce_after_shop_loop', 
'wpm_product_cat_archive_add_meta'); 
function wpm_product_cat_archive_add_meta() { 
$t_id = get_queried_object()->term_id; 
$term_meta = get_option("taxonomy_$t_id"); 
$term_meta_content = $term_meta['custom_term_meta']; 
if ($term_meta_content != '') { 
echo '<div class="woo-sc-box normal rounded full">'; 
    echo apply_filters('the_content', $term_meta_content); 
echo '</div>'; 
} 
} 

謝謝

+0

感謝您的幫助,但沒有工作 – Manoj

0

可以顯示在前端自定義元標題和meta描述使用以下鉤子。

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; 

    } 

您可以隨時使用其他掛鉤的當你正在使用的所有在一個搜索引擎優化,因此所有WP默認勾繼template structure

+0

嗨,感謝您的幫助,但它不工作 – Manoj

+0

仍然沒有運氣.. – Manoj

+0

檢查此代碼裏面,存檔產品。php global $ wp_query; $ cat_obj = $ wp_query-> get_queried_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); 呼應$ title_meta和$ desc_meta –

相關問題