将自定义字段外部链接添加到档案类别页

将自定义字段外部链接添加到档案类别页

问题描述:

我有一个woocommerce网站。首先,我想在管理产品页面上添加一个自定义字段,以设置exernal url,我将在我的Archives类别产品页面上使用该字段。将自定义字段外部链接添加到档案类别页

另外我希望理想的情况是在我的管理产品页面设置元框中有这个自定义字段。但是我已经更改了所有存档页面上的链接。

现在我有这个代码是没有做什么,我需要:

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 . '">'; 

} 

我该怎么做才能让它只类档案网页上运作?

感谢

第一步 - 在管理产品页面自定义字段的创建设置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步 - 通过自定义的meta值更换链接仅在产品类别档案页面中。

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文件或也以任何插件文件。

此代码经过测试和工程