子类别缩略图和自定义的输出网格 URL
Output grid of child category thumbnails and custom URL
我在 WooCommerce 中有一个名为经销商 (ID 93) 的产品类别。该产品类别目前有大约 50 个子项,每个子项都有一个徽标作为其缩略图。我在每个类别上添加了一个自定义字段,以将 URL 粘贴到经销商网站。我想创建一个页面来自动填充这些子类别并显示缩略图网格。我希望每个缩略图 link 到 dealer_website URL 并在新的 window.
中打开
这是我用来向产品类别添加自定义元字段的代码:
// Custom field for product categories
// Add term page
function custom_product_taxonomy_add_new_meta_field() {
// this will add the custom meta field to the add new term page
?>
<div class="form-field">
<label for="term_meta[custom_term_meta]"><?php _e( 'Dealer Website', 'dealer_website' ); ?></label>
<input type="text" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="">
<p class="description"><?php _e( 'Make sure to include the whole URL here','dealer_website' ); ?></p>
</div>
<?php
}
add_action( 'product_cat_add_form_fields', 'custom_product_taxonomy_add_new_meta_field', 10, 2 );
// Edit term page
function custom_product_taxonomy_edit_meta_field($term) {
// put the term ID into a variable
$t_id = $term->term_id;
// retrieve the existing value(s) for this meta field. This returns an array
$term_meta = get_option( "taxonomy_$t_id" ); ?>
<tr class="form-field">
<th scope="row" valign="top"><label for="term_meta[custom_term_meta]"><?php _e( 'Dealer Website', 'dealer_website' ); ?></label></th>
<td>
<input type="text" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="<?php echo esc_attr( $term_meta['custom_term_meta'] ) ? esc_attr( $term_meta['custom_term_meta'] ) : ''; ?>">
<p class="description"><?php _e( 'Make sure to include the whole URL here','dealer_website' ); ?></p>
</td>
</tr>
<?php
}
add_action( 'product_cat_edit_form_fields', 'custom_product_taxonomy_edit_meta_field', 10, 2 );
// Save extra taxonomy fields callback function.
function save_taxonomy_custom_meta( $term_id ) {
if ( isset( $_POST['term_meta'] ) ) {
$t_id = $term_id;
$term_meta = get_option( "taxonomy_$t_id" );
$cat_keys = array_keys( $_POST['term_meta'] );
foreach ( $cat_keys as $key ) {
if ( isset ( $_POST['term_meta'][$key] ) ) {
$term_meta[$key] = $_POST['term_meta'][$key];
}
}
// Save the option array.
update_option( "taxonomy_$t_id", $term_meta );
}
}
add_action( 'edited_product_cat', 'save_taxonomy_custom_meta', 10, 2 );
add_action( 'create_product_cat', 'save_taxonomy_custom_meta', 10, 2 );
这部分工作得很好,但我不知道如何让它输出到自定义页面模板上的缩略图网格中,所有 link 到 dealer_url 并打开它在新 window.
我现在正在使用短代码来完成部分工作,但我不知道如何提取 dealer_website url 而不是类别 url:
[product_categories parent="93" columns="5" number="100" hide_empty="0"]
获取子项对象
$terms_objects = get_terms(
array(
'taxonomy' => 'product_cat',
'child_of' => 93,
'hide_empty' => false,
)
);
遍历缩略图网格内的条款对象,获取经销商 URL 字段和缩略图 ID,然后是缩略图 URL
// Loop over the childrens.
foreach( $terms_objects as $term_object ) {
$dealer_url = get_option( 'taxonomy_' . $term_object->term_id );
$term_thumbnail_id = get_term_meta( $term_object->term_id, 'thumbnail_id', true );
$term_thumbnail_url = ( ! empty( $term_thumbnail_id ) && is_numeric( $term_thumbnail_id ) ) ? wp_get_attachment_image_url( (int) $term_thumbnail_id, 'thumbnail' ) : '';
// check if the $term_thumnail_url and the $dealer_url are not empty then the rest is HTML
if ( ! empty( $term_thumbnail_url ) && ! empty( $dealer_url ) ) {
?>
<a href="<?php echo esc_url( $dealer_url ); ?>" target="_blank" ><img src="<?php echo esc_url( $term_thumbnail_url ); ?>" ></a>
<?php
}
}
对于经销商 URL 字段,最好也使用 termmeta table 而不是选项 table。
我在 WooCommerce 中有一个名为经销商 (ID 93) 的产品类别。该产品类别目前有大约 50 个子项,每个子项都有一个徽标作为其缩略图。我在每个类别上添加了一个自定义字段,以将 URL 粘贴到经销商网站。我想创建一个页面来自动填充这些子类别并显示缩略图网格。我希望每个缩略图 link 到 dealer_website URL 并在新的 window.
中打开这是我用来向产品类别添加自定义元字段的代码:
// Custom field for product categories
// Add term page
function custom_product_taxonomy_add_new_meta_field() {
// this will add the custom meta field to the add new term page
?>
<div class="form-field">
<label for="term_meta[custom_term_meta]"><?php _e( 'Dealer Website', 'dealer_website' ); ?></label>
<input type="text" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="">
<p class="description"><?php _e( 'Make sure to include the whole URL here','dealer_website' ); ?></p>
</div>
<?php
}
add_action( 'product_cat_add_form_fields', 'custom_product_taxonomy_add_new_meta_field', 10, 2 );
// Edit term page
function custom_product_taxonomy_edit_meta_field($term) {
// put the term ID into a variable
$t_id = $term->term_id;
// retrieve the existing value(s) for this meta field. This returns an array
$term_meta = get_option( "taxonomy_$t_id" ); ?>
<tr class="form-field">
<th scope="row" valign="top"><label for="term_meta[custom_term_meta]"><?php _e( 'Dealer Website', 'dealer_website' ); ?></label></th>
<td>
<input type="text" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="<?php echo esc_attr( $term_meta['custom_term_meta'] ) ? esc_attr( $term_meta['custom_term_meta'] ) : ''; ?>">
<p class="description"><?php _e( 'Make sure to include the whole URL here','dealer_website' ); ?></p>
</td>
</tr>
<?php
}
add_action( 'product_cat_edit_form_fields', 'custom_product_taxonomy_edit_meta_field', 10, 2 );
// Save extra taxonomy fields callback function.
function save_taxonomy_custom_meta( $term_id ) {
if ( isset( $_POST['term_meta'] ) ) {
$t_id = $term_id;
$term_meta = get_option( "taxonomy_$t_id" );
$cat_keys = array_keys( $_POST['term_meta'] );
foreach ( $cat_keys as $key ) {
if ( isset ( $_POST['term_meta'][$key] ) ) {
$term_meta[$key] = $_POST['term_meta'][$key];
}
}
// Save the option array.
update_option( "taxonomy_$t_id", $term_meta );
}
}
add_action( 'edited_product_cat', 'save_taxonomy_custom_meta', 10, 2 );
add_action( 'create_product_cat', 'save_taxonomy_custom_meta', 10, 2 );
这部分工作得很好,但我不知道如何让它输出到自定义页面模板上的缩略图网格中,所有 link 到 dealer_url 并打开它在新 window.
我现在正在使用短代码来完成部分工作,但我不知道如何提取 dealer_website url 而不是类别 url:
[product_categories parent="93" columns="5" number="100" hide_empty="0"]
获取子项对象
$terms_objects = get_terms( array( 'taxonomy' => 'product_cat', 'child_of' => 93, 'hide_empty' => false, ) );
遍历缩略图网格内的条款对象,获取经销商 URL 字段和缩略图 ID,然后是缩略图 URL
// Loop over the childrens. foreach( $terms_objects as $term_object ) { $dealer_url = get_option( 'taxonomy_' . $term_object->term_id ); $term_thumbnail_id = get_term_meta( $term_object->term_id, 'thumbnail_id', true ); $term_thumbnail_url = ( ! empty( $term_thumbnail_id ) && is_numeric( $term_thumbnail_id ) ) ? wp_get_attachment_image_url( (int) $term_thumbnail_id, 'thumbnail' ) : ''; // check if the $term_thumnail_url and the $dealer_url are not empty then the rest is HTML if ( ! empty( $term_thumbnail_url ) && ! empty( $dealer_url ) ) { ?> <a href="<?php echo esc_url( $dealer_url ); ?>" target="_blank" ><img src="<?php echo esc_url( $term_thumbnail_url ); ?>" ></a> <?php } }
对于经销商 URL 字段,最好也使用 termmeta table 而不是选项 table。