【问题标题】:How to do custom URL and Breadcrumb in WooCommerce?如何在 WooCommerce 中进行自定义 URL 和面包屑导航?
【发布时间】:2018-01-10 19:12:49
【问题描述】:

我想为基于 My WooCommerce 的网站自定义 WooCommrece 面包屑和 URL。

我的网站在 WooCommerce 上运行。我正面临将面包屑和 URL 自定义为太长的问题。当我访问任何产品或类别页面时,URL 会很长,并且由于类别的名称和类别的 slug 很长,面包屑也会很长。

所以,我需要这样的解决方案,例如在 woocommerce 中为面包屑添加替代类别名称并为 URL 添加替代项。

这对 WooCoommerce 有帮助吗? 我可以自定义 WooCoomerce 产品 URL 和 Breadcrumb 吗?

【问题讨论】:

  • 到目前为止你尝试了什么?任何代码?
  • 是的,我正在努力。而且我已经很接近完成这件事了……之后,我会在这里更新……

标签: php wordpress woocommerce


【解决方案1】:

我已经完成了替代标题的面包屑的开发。

我的代码在下面..

/*
* Custom title for category
*/
add_action('product_cat_add_form_fields','custom_taxonomy_add_new_meta_field', 10, 1);

//admin side extra field
function custom_taxonomy_add_new_meta_field() {
?>
<div class="form-field">
    <label for="alternative_title"><?php _e('Alternative Title', 'store-front-child'); ?></label>
    <input type="text" name="alternative_title" id="alternative_title">
    <p class="description"><?php _e('Enter Alternative Title for Breadcrumb', 'store-front-child'); ?></p>
</div>
<?php
}

add_action('product_cat_edit_form_fields', 'custom_taxonomy_edit_meta_field', 10, 1);
//admin side extra field when edit
function custom_taxonomy_edit_meta_field($term) {
$term_id = $term->term_id;
$alternative_title = get_term_meta($term_id, 'alternative_title', true);
$alternative_slug = get_term_meta($term_id, 'alternative_slug', true);
?>
<tr class="form-field">
    <th scope="row" valign="top"><label for="alternative_title"><?php _e('Alternative Title', 'store-front-child'); ?></label></th>
    <td>
        <input type="text" name="alternative_title" id="alternative_title" value="<?php echo esc_attr($alternative_title) ? esc_attr($alternative_title) : ''; ?>">
        <p class="description"><?php _e('Enter Alternative Title for Breadcrumb', 'store-front-child'); ?></p>
    </td>
</tr>
<?php
}

add_action('edited_product_cat', 'custom_save_taxonomy_custom_meta', 10, 1);
add_action('create_product_cat', 'custom_save_taxonomy_custom_meta', 10, 1);
//save alternative title to database
function custom_save_taxonomy_custom_meta($term_id) {
$alternative_title = filter_input(INPUT_POST, 'alternative_title');
update_term_meta($term_id, 'alternative_title', $alternative_title);
}

add_filter( 'woocommerce_get_breadcrumb', 'custom_change_breadcrumb' );
//filter the woocommerce breadcrub text for category
function custom_change_breadcrumb( $crumbs ) {
global $post;
echo $post->id;
$new_crumb_array = array();
if( !empty( $crumbs ) ):
    foreach ( $crumbs as $crumb ):
        $new_crumb = array();
        $new_crumb[0] = $crumb[0];
        $new_crumb[1] = $crumb[1];
        $cat = get_term_by( 'name', $crumb[0], 'product_cat' );
        $alternative_title = get_term_meta( $cat->term_id, 'alternative_title', true );
        if( !empty( $alternative_title ) ){
            $new_crumb[0] = $alternative_title;
        }
        $new_crumb_array[] = $new_crumb;
    endforeach;
endif;

return $new_crumb_array;
}

【讨论】:

    猜你喜欢
    • 2011-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多