首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >子类别缩略图和自定义URL的输出网格

子类别缩略图和自定义URL的输出网格
EN

Stack Overflow用户
提问于 2021-01-14 16:46:53
回答 1查看 129关注 0票数 0

我在WooCommerce中有一个叫做经销商(ID 93)的产品类别。该产品类别目前约有50个儿童,每一个都有一个标志作为其缩略图。我已经在每个类别中添加了一个自定义字段来将一个URL粘贴到经销商网站。我想要创建一个页面,该页面将自动填充这些子类别并显示缩略图的网格。我希望每个缩略图链接到dealer_website网址,并打开一个新窗口。

这是我用来向产品类别添加自定义元字段的代码:

代码语言:javascript
复制
// 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 );

这个部分工作得很好,但我不知道如何将其输出到自定义页面模板上的缩略图网格中,该模板全部链接到dealer_url并在新窗口中打开。

我现在使用的是短代码,但我不知道如何提取dealer_website url,而不是类别url:

代码语言:javascript
复制
[product_categories parent="93" columns="5" number="100" hide_empty="0"]
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-15 11:14:43

  1. 获取子术语对象

$terms_objects = get_terms(数组(‘分类学’=>‘product’,'child_of‘=> 93,'hide_empty’=> false,‘hide_empty’=>false,] );

  1. 循环遍历缩略图网格中的术语对象,获取交易商URL字段和缩略图ID,然后是缩略图URL

//在孩子们身上翻滚。( $terms_objects as $term_object ){ $dealer_url = get_option( 'taxonomy_‘.)$term_object->term_id;$term_thumbnail_id = get_term_meta( $term_object->term_id,‘缩略图_id’,true );$term_thumbnail_url =(!空( $term_thumbnail_id )& is_numeric( $term_thumbnail_id )?wp_get_attachment_image_url( (int) $term_thumbnail_id,‘缩略图’):'';//检查$term_thumnail_url和$dealer_url是否为空,其余都是HTML (!!空( $term_thumbnail_url )&!空( $dealer_url )){ ?>

对于交易商URL字段,最好使用termmeta表而不是options表。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65723196

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档