Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress 对店铺/类别页面使用不同的产品标题_Wordpress_Woocommerce_Variations - Fatal编程技术网

Wordpress 对店铺/类别页面使用不同的产品标题

Wordpress 对店铺/类别页面使用不同的产品标题,wordpress,woocommerce,variations,Wordpress,Woocommerce,Variations,一、 我正在寻找一种解决方案,在不影响单个产品页面标题的情况下,为车间页面使用不同的产品标题。 下面是我想要实现的一个例子 你们可以在上面的链接中看到商店页面上的产品名称是不同的,我知道这个网站是在不同的CMS中建立的,但我们能在Woocommerce中实现类似的结果吗 以下是您可以做的。将以下代码添加到theme functions.php文件中。代码将在产品数据->常规选项下添加自定义标题选项。如果在管理中未添加自定义标题,它将显示默认标题 在后端添加自定义字段 // Display

一、 我正在寻找一种解决方案,在不影响单个产品页面标题的情况下,为车间页面使用不同的产品标题。 下面是我想要实现的一个例子
你们可以在上面的链接中看到商店页面上的产品名称是不同的,我知道这个网站是在不同的CMS中建立的,但我们能在Woocommerce中实现类似的结果吗

以下是您可以做的。将以下代码添加到theme functions.php文件中。代码将在产品数据->常规选项下添加自定义标题选项。如果在管理中未添加自定义标题,它将显示默认标题

在后端添加自定义字段

    // Display the custom text field
    function mos_create_custom_field() {
        $args = array(
        'id' => 'shop_page_field_title',
        'label' => __( 'Shop Page product title' ),
        'class' => 'mos-custom-field',
        'desc_tip' => true,
        'description' => __( 'Shop Page product title.', 'ctwc' ),
        );
        woocommerce_wp_text_input( $args );
    }
    add_action( 'woocommerce_product_options_general_product_data', 'mos_create_custom_field' );
    add_action( 'woocommerce_product_options_inventory_product_data', 'mos_create_custom_field' );
保存自定义字段值

// Save the custom field
function mos_save_custom_field( $post_id ) {
    $product = wc_get_product( $post_id );
    $title = isset( $_POST['shop_page_field_title'] ) ? $_POST['shop_page_field_title'] : '';
    $product->update_meta_data( 'shop_page_field_title', sanitize_text_field( $title ) );
    $product->save();
}
add_action( 'woocommerce_process_product_meta', 'mos_save_custom_field' );
// remove the default action for product title on shop page
remove_action( 'woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title' );

// add the custom action to show the custom product title
add_action( 'woocommerce_shop_loop_item_title', 'custom_woocommerce_template_loop_product_title', 99 );

function custom_woocommerce_template_loop_product_title() {
    global $post;

    $product = wc_get_product( $post->ID );
    $title = $product->get_meta( 'shop_page_field_title' );
    if( $title ) {
        // Only display our field if we've got a value for the field title
        echo '<h2 class="woocommerce-loop-product__title">' . esc_html( $title ) . '</h2>';
    } else {
        // else display the defaul title
        echo '<h2 class="woocommerce-loop-product__title">' . esc_html( $product->get_title() ) . '</h2>';
    }
}
显示在商店页面上

// Save the custom field
function mos_save_custom_field( $post_id ) {
    $product = wc_get_product( $post_id );
    $title = isset( $_POST['shop_page_field_title'] ) ? $_POST['shop_page_field_title'] : '';
    $product->update_meta_data( 'shop_page_field_title', sanitize_text_field( $title ) );
    $product->save();
}
add_action( 'woocommerce_process_product_meta', 'mos_save_custom_field' );
// remove the default action for product title on shop page
remove_action( 'woocommerce_shop_loop_item_title', 'woocommerce_template_loop_product_title' );

// add the custom action to show the custom product title
add_action( 'woocommerce_shop_loop_item_title', 'custom_woocommerce_template_loop_product_title', 99 );

function custom_woocommerce_template_loop_product_title() {
    global $post;

    $product = wc_get_product( $post->ID );
    $title = $product->get_meta( 'shop_page_field_title' );
    if( $title ) {
        // Only display our field if we've got a value for the field title
        echo '<h2 class="woocommerce-loop-product__title">' . esc_html( $title ) . '</h2>';
    } else {
        // else display the defaul title
        echo '<h2 class="woocommerce-loop-product__title">' . esc_html( $product->get_title() ) . '</h2>';
    }
}
//删除商店页面上产品标题的默认操作
删除操作(“woocommerce\u shop\u loop\u item\u title”、“woocommerce\u template\u loop\u product\u title”);
//添加自定义操作以显示自定义产品标题
添加操作('woocommerce\u shop\u loop\u item\u title','custom\u woocommerce\u template\u loop\u product\u title',99);
功能自定义\商业\模板\循环\产品\标题(){
全球$员额;
$product=wc\U get\U product($post->ID);
$title=$product->get_meta('shop_page_field_title');
如果($标题){
//仅当字段标题有值时才显示字段
echo'.esc_html($title)。'';
}否则{
//否则,显示诽谤的标题
echo'.esc_html($product->get_title())。';
}
}

代码经过测试,运行良好。希望能有所帮助

非常感谢你帮助我。该代码在简单产品上非常有效,但对于可变产品,似乎需要对代码进行一些修改。我们几乎所有的产品都是可变的。在这种情况下需要你的帮助谢谢!代码也适用于可变产品。你需要解释一下,如果你看到任何错误,因为我不能像这样理解为什么它不能与你一起工作。这段代码在Product data->General options中添加了一个自定义字段,但“Gereral”仅在简单产品中可用,如果我切换到Variable Product,则此选项卡将消失。也许我们需要在不同的选项卡中添加该自定义字段,用于库存或Extraok等可变产品。更新了答案。添加了钩子添加动作('woocommerce\u product\u options\u inventory\u product\u data'、'mos\u create\u custom\u field');您真是太棒了,这段代码运行得非常完美:)