Php Woocommerce-编写短代码以显示按产品属性筛选的产品类别页面的链接,并在产品类别页面上显示短代码
我正在编写一些函数来显示自定义字段,该字段的值将显示在产品父类别页面(例如http:website.com/catalog/parent category)的子类别标题之后 我需要这个自定义字段的内容是html和短代码。我正在创建的另一个函数是shortcode,它如下所示:Php Woocommerce-编写短代码以显示按产品属性筛选的产品类别页面的链接,并在产品类别页面上显示短代码,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我正在编写一些函数来显示自定义字段,该字段的值将显示在产品父类别页面(例如http:website.com/catalog/parent category)的子类别标题之后 我需要这个自定义字段的内容是html和短代码。我正在创建的另一个函数是shortcode,它如下所示: [product_attr slug="Straight Drill" label="Straight Drill"] 在这里,我需要显示到按产品属性筛选的产品页面的链接。我希望能够在shortcode属性中编辑
[product_attr slug="Straight Drill" label="Straight Drill"]
在这里,我需要显示到按产品属性筛选的产品页面的链接。我希望能够在shortcode属性中编辑标签,但slug是从产品属性的slug生成的
这就是我在下面处理的问题,我一直无法使短代码起作用,并在子产品标题下显示内容。任何指出错误的帮助都将不胜感激
// Display product attributes from a product category
global $product;
$current_category = $wp_query->get_queried_object();
$category_id = $current_category->term_id;
$category_name = $current_category->name;
$products = get_posts(array('product_cat' => $category_id));
$term_link = get_term_link( $current_category );
// Extended subscription function with subscription type variable
function create_attribute_shortcode( $atts ) {
// Get product attributes
foreach ( $products as $product ) {
$attributes = $product->get_attributes();
if ( ! $attributes ) {
return;
}
foreach ( $attributes as $attribute ) {
$attribute_name = $attribute['name'];
$attribute_slug = $attribute['slug'];
extract( shortcode_atts( array(
'slug' => $attribute_slug,
'label' => ''), $atts));
$link= '<a href="'. esc_url( $term_link ). '/'. $slug .'">'. $label .'</a>';
return $link;
}
}
add_shortcode( 'product-attr', 'create_attribute_shortcode' );
}
add_action('product_cat_add_form_fields', 'tft_taxonomy_add_new_meta_field', 10, 2);
add_action('product_cat_edit_form_fields', 'tft_taxonomy_edit_meta_field', 10, 2);
//Product Cat Create page
function tft_taxonomy_add_new_meta_field() {
?>
<div class="form-field">
<label for="tft_meta_desc"><?php _e('Product Attr list', 'altitude'); ?></label>
<textarea name="tft_meta_desc" id="tft_meta_desc"></textarea>
<p class="description"><?php _e('Enter links and assign labels for product atributes. Example: [product_attr slug="Straight Drill" label="Straight Drill"]', 'altitude'); ?></p>
</div>
<?php
}
//Product Cat Edit page
function tft_taxonomy_edit_meta_field($term) {
//getting term ID
$term_id = $term->term_id;
// retrieve the existing value(s) for this meta field.
$tft_meta_desc = get_term_meta($term_id, 'tft_meta_desc', true);
?>
<tr class="form-field">
<th scope="row" valign="top"><label for="tft_meta_desc"><?php _e('Product Attr list', 'altitude'); ?></label></th>
<td>
<textarea name="tft_meta_desc" id="tft_meta_desc"><?php echo esc_attr($tft_meta_desc) ? esc_attr($tft_meta_desc) : ''; ?></textarea>
<p class="description"><?php _e('Enter links and assign labels for product atributes. Example: [product_attr slug="Straight Drill" label="Straight Drill"]', 'altitude'); ?></p>
</td>
</tr>
<?php
}
add_action('edited_product_cat', 'tft_save_taxonomy_custom_meta', 10, 2);
add_action('create_product_cat', 'tft_save_taxonomy_custom_meta', 10, 2);
// Save extra taxonomy fields callback function.
function tft_save_taxonomy_custom_meta($term_id) {
$tft_meta_desc = filter_input(INPUT_POST, 'tft_meta_desc');
update_term_meta($term_id, 'tft_meta_desc', $tft_meta_desc);
}
// check if page is parent product cat
function has_term_have_children( $term_id = '', $taxonomy = 'product_cat' ) {
// Check if we have a term value, if not, return false
if ( !$term_id )
return false;
// Get term children
$term_children = get_term_children( filter_var( $term_id, FILTER_VALIDATE_INT ), filter_var( $taxonomy, FILTER_SANITIZE_STRING ) );
// Return false if we have an empty array or WP_Error object
if ( empty( $term_children ) || is_wp_error( $term_children ) )
return true;
return false;
}
// Add product attribute lists to underneath product cats
if( is_tax( 'product_cat' ) && has_term_have_children(get_queried_object_id()) ) {
$args = array('child_of' => $termID, 'parent' => $termID);
$termchildren = get_terms( 'product_cat', $args);
add_action( 'woocommerce_after_shop_loop_item_title', 'tft_do_prod_attr_list', 1 );
function tft_do_prod_attr_list() {
$tft_do_meta_desc = get_term_meta($termchildren->term_id, 'tft_meta_desc', true);
$tft_display_meta_desc = do_shortcode($tft_do_meta_desc);
echo '<div class="tft-meta-desc">';
echo $tft_display_meta_desc;
echo '</div>';
}
}
//显示产品类别中的产品属性
全球$产品;
$current_category=$wp_query->get_queryed_object();
$category\u id=$current\u category->term\u id;
$category\u name=$current\u category->name;
$products=get_posts(数组('product_cat'=>$category_id));
$term\u link=get\u term\u link($current\u category);
//具有订阅类型变量的扩展订阅函数
函数创建属性快捷码($atts){
//获取产品属性
foreach($products as$product){
$attributes=$product->get_attributes();
如果(!$attributes){
返回;
}
foreach($attributes作为$attribute){
$attribute_name=$attribute['name'];
$attribute_slug=$attribute['slug'];
提取(短码)附件(数组)(
“slug”=>$attribute\u slug,
'标签'=>'',$atts));
$link='';
返回$link;
}
}
添加_短代码(“产品属性”、“创建_属性_短代码”);
}
添加操作(“产品类别添加表单字段”、“tft分类法添加新元数据字段”,10,2);
添加操作(“产品类别编辑表单字段”、“tft分类法编辑元数据字段”,10,2);
//产品目录创建页面
函数tft_分类法_添加_新建_元_字段(){
?>
对不起,我的意思是我想为链接创建一个短代码,链接的一个示例:(富士航空工具:父猫;钻:子猫;婴儿角度钻:产品属性)。短代码将采用上述格式。我需要在子cat页面的自定义字段中使用这些短代码。我想要的是能够修改短代码中的“标签”,并在每个子cat下显示它们(例如,在“钻”下-婴儿角钻、角钻等)希望这更合理,我的意思是我想为链接创建一个短代码,链接的一个示例:(富士air tools:父猫;钻孔:子猫;婴儿角度钻孔:产品属性)。短代码将采用上述格式。我需要在子cat页面的自定义字段中使用这些短代码。我想要的是能够修改短代码中的“标签”,并在每个子cat下显示它们(例如“钻”下-婴儿角钻、角钻等),希望这更有意义