在Wordpress + Woocommerce网站上,我有一个菜单,其中包含一些自定义链接,可以使用高级查询分类法调用特定商店页面网址。
这些是我用于这些自定义菜单条目的示例链接:
https://example.com/product-category/cat1/?pa_attrib1=value1&pa_attrib2=value3 https://example.com/product-category/cat2/?pa_attrib1=value2&pa_attrib3=value4` https://example.com/product-category/cat2/?pa_attrib1=value5&pa_attrib4=value5,value7,value8,value9
通过这种方式,我可以为各个商店部门配置“直接”条目。
现在我需要在商店和存档页面中以类似的方式显示网址中设置的属性的名称和值:
类别名称 - 属性1名称:value1 - Atttribute 2名称:value2,...
我想我需要在functions.php中使用钩子 woocommerce_archive_description ,但我不知道如何回忆并显示我需要的值
add_action ( 'woocommerce_archive_description', 'show_term_description', 20 );
function show_term_description() {
echo term_description( $term_id, $taxonomy ); // print Category Name
// ...
// ...
}
感谢
答案 0 :(得分:1)
这可以通过在钩子函数中插入以下注释代码来完成:
add_action ( 'woocommerce_archive_description', 'show_term_description', 20 );
function show_term_description() {
// Only for product category archive pages
if( ! is_product_category()) return;
global $wp;
// Get the requested Url category/subcategories
$request = $wp->request;
// Set them in an array
$request = explode( '/', $request );
// Remove 'product-category' from the array
if( 'product-category' == $request[0] )
unset($request[0]);
// Starting html formatting
$html = '<p><strong>';
// The main category and sub-categories names
foreach( $request as $category ){
// Get the category name
$category_term = get_term_by( 'slug', $category, 'product_cat' );
// Set category and subcategories in an array
$categories_name[] = $category_term->name;
}
// Formatting the category/subcategories in an html string
$html .= implode( '</strong> - <strong>', $categories_name );
// The product attributes names and related values
if( ! empty($_GET) ){
$html .= '</strong> - <strong>'; // Formatting html
$count = 0; // Initializing the counter
$attr_count = count($_GET); // get the length of the array
// Loop through the attribute/values from the $_GET
foreach($_GET as $taxonomy => $values ){
$count++;
$terms_names = array(); // Initializing
// If the taxonomy doesn't exist,
if( ! taxonomy_exists( $taxonomy ) ){
continue; // We go to next attribute
}
// Set the attribute terms in an array
$term_slugs = explode( ',', $values );
// Loop through the attribute term
foreach( $term_slugs as $term_slug ){
// Get the WP_Term object
$term = get_term_by( 'slug', $term_slug, $taxonomy );
if( ! term_exists( $term->term_id, $taxonomy ) )
continue; // We go to next term
// Set The term name in an array
$terms_names[] = $term->name;
}
// If there is no corresponding terms for the taxonomy
if( sizeof($terms_names) == 0 ){
continue; // We go to next attribute
}
// Add the attribute label name to the output
if( $count > 1 ) $html .= ' - ';
$html .= wc_attribute_label( $taxonomy );
// Formatting the term names in a string and add them to the output
$html .= ':</strong> ' . implode( ', ', $terms_names) . '<strong>';
}
}
// Outputing The html
echo $html.'</p>';
echo $attr_count .' | '.$count;
}
代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。
经过测试和工作......