2016-09-17 9 views
3

私は、WebサイトのWebサイトを持っています。まず、adminの製品ページにカスタムフィールドを追加して、external urlをアーカイブカテゴリの製品ページで使用するように設定します。アーカイブカテゴリページへのカスタムフィールド外部リンクの追加

また、管理用製品ページの設定メタボックスにこのカスタムフィールドを追加することをお勧めします。しかし、私はすべてのアーカイブページ上のリンクを変更しているコード。今の

私は私が必要なものをやっていない、このコードがあります。

remove_action('woocommerce_before_shop_loop_item', 'woocommerce_template_loop_product_link_open', 10); 
add_action('woocommerce_before_shop_loop_item', 'mycode_woocommerce_template_loop_product_link_open', 20); 
function mycode_woocommerce_template_loop_product_link_open() { 

    $url = 'https://www.some_domain.com/'; 

    echo '<a href="' . $url . '">'; 

} 

は、どのように私はそれが唯一のカテゴリアーカイブのページで動作させるために何ができるの?

おかげ

答えて

4

ステップ1 - metabox設定管理製品ページのカスタムフィールドの作成:

enter image description here

// Inserting a Custom Admin Field 
add_action('woocommerce_product_options_general_product_data', 'add_custom_text_field_create'); 
function add_custom_text_field_create() { 
    echo '<div class="options_group">'; 

    woocommerce_wp_text_input(array(
     'type'    => 'text', 
     'id'    => 'extern_link', 
     'label'    => __('External Link', 'woocommerce'), 
     'placeholder'  => '', 
     'description'  => __('Insert url', 'woocommerce'), 
    )); 

    echo '</div>'; 
} 

// Saving the field value when submitted 
add_action('woocommerce_process_product_meta', 'add_custom_field_text_save'); 
function add_custom_field_text_save($post_id){ 
$wc_field = $_POST['extern_link']; 
if(!empty($wc_field)) 
    update_post_meta($post_id, 'extern_link', esc_attr($wc_field)); 
} 

ステップ2 - カスタムメタ値によってリンクを交換します製品カテゴリーアーカイブページのみ。

remove_action('woocommerce_before_shop_loop_item', 'woocommerce_template_loop_product_link_open'); 
add_action('woocommerce_before_shop_loop_item', 'custom_wc_template_loop_product_link_open', 10); 
function custom_wc_template_loop_product_link_open() { 
    // For product category archives pages only. 
    if (is_product_category()) { 
     // You get here your custom link 
     $link = get_post_meta(get_the_ID(), 'extern_link', true); 
     echo '<a href="' . $link . '" class="woocommerce-LoopProduct-link">'; 
    //For the other woocommerce archives pages 
    } else { 
     echo '<a href="' . get_the_permalink() . '" class="woocommerce-LoopProduct-link">'; 
    } 
} 

コードは、あなたのアクティブな子テーマ(またはテーマ)のfunction.phpファイルやも任意のプラグインファイルになります。

このコードはテスト済みであり、動作します。

関連する問題