将自定义字段外部链接添加到存档类别页面 [英] Adding a custom field external link to archives category pages

查看:100
本文介绍了将自定义字段外部链接添加到存档类别页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个woocommerce网站。首先,我想在管理产品页面上添加一个自定义字段,以设置要在档案类别产品页面上使用的外部网址

I have a woocommerce web site. First I would like to add a custom field on admin product pages to set an exernal url that I will use on my Archives category product pages.

我也希望在管理产品页面设置metabox上具有此自定义字段。但是我更改了所有档案页面上的链接的代码。

Also I would like ideally to have this custom field on my admin product pages settings metabox. But the code I have change the link on all archives pages.

现在,我的代码没有满足我的需要:

For now I have this code that is not doing what I need:

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

}

如何使它在类别存档页面上工作

How can I do to make it work on category archives pages only?

谢谢

推荐答案

第一步-创建自定义字段在管理产品页面的设置metabox中:

Step1 - Creation of a custom field in the admin product pages setting metabox:

// 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-更换


Step 2 - Replacing the link by a custom meta value in product category archives pages only.

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文件

此代码已通过测试并有效

This code is tested annd works

这篇关于将自定义字段外部链接添加到存档类别页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆