【问题标题】:Adding Additional Text To a Product Category Page in Woocommerce在 Woocommerce 中向产品类别页面添加附加文本
【发布时间】:2014-11-17 11:11:58
【问题描述】:

Woocommerce 有产品类别页面。它们就像 wordpress 常规类别页面一样,但用于产品而不是帖子。

在产品类别编辑器中有一个描述框,您可以在其中添加文本。查看产品类别页面时,文本将显示在类别页面标题下方。

我也在尝试找到一种方法来在产品下方添加内容。基本上,我希望在该产品类别页面上的产品下方有一篇 1,000 字的文章,用于 SEO。

但是,我不知道该怎么做。

【问题讨论】:

    标签: php woocommerce


    【解决方案1】:

    要在产品下方的分类页面上显示文字,需要将此代码添加到文件 wp-includes/functions.php 的开头,在代码 "mysql2date($format,$date,$translate = true ) {"

        add_action( 'init', 'wpm_product_cat_register_meta' );
    /**
     * Register details product_cat meta.
     *
     * Register the details metabox for WooCommerce product categories.
     *
     */
    function wpm_product_cat_register_meta() {
        register_meta( 'term', 'details', 'wpm_sanitize_details' );
    }
    /**
     * Sanitize the details custom meta field.
     *
     * @param  string $details The existing details field.
     * @return string          The sanitized details field
     */
    function wpm_sanitize_details( $details ) {
        return wp_kses_post( $details );
    }
    
    
    add_action( 'product_cat_add_form_fields', 'wpm_product_cat_add_details_meta' );
    /**
     * Add a details metabox to the Add New Product Category page.
     *
     * For adding a details metabox to the WordPress admin when
     * creating new product categories in WooCommerce.
     *
     */
    function wpm_product_cat_add_details_meta() {
        wp_nonce_field( basename( __FILE__ ), 'wpm_product_cat_details_nonce' );
        ?>
        <div class="form-field">
            <label for="wpm-product-cat-details"><?php esc_html_e( 'Details', 'wpm' ); ?></label>
            <textarea name="wpm-product-cat-details" id="wpm-product-cat-details" rows="5" cols="40"></textarea>
            <p class="description"><?php esc_html_e( 'Detailed category info to appear below the product list', 'wpm' ); ?></p>
        </div>
        <?php
    }
    
    
    add_action( 'product_cat_edit_form_fields', 'wpm_product_cat_edit_details_meta' );
    /**
     * Add a details metabox to the Edit Product Category page.
     *
     * For adding a details metabox to the WordPress admin when
     * editing an existing product category in WooCommerce.
     *
     * @param  object $term The existing term object.
     */
    function wpm_product_cat_edit_details_meta( $term ) {
        $product_cat_details = get_term_meta( $term->term_id, 'details', true );
        if ( ! $product_cat_details ) {
            $product_cat_details = '';
        }
        $settings = array( 'textarea_name' => 'wpm-product-cat-details' );
        ?>
        <tr class="form-field">
            <th scope="row" valign="top"><label for="wpm-product-cat-details"><?php esc_html_e( 'Details', 'wpm' ); ?></label></th>
            <td>
                <?php wp_nonce_field( basename( __FILE__ ), 'wpm_product_cat_details_nonce' ); ?>
                <?php wp_editor( wpm_sanitize_details( $product_cat_details ), 'product_cat_details', $settings ); ?>
                <p class="description"><?php esc_html_e( 'Detailed category info to appear below the product list','wpm' ); ?></p>
            </td>
        </tr>
        <?php
    }
    
    add_action( 'create_product_cat', 'wpm_product_cat_details_meta_save' );
    add_action( 'edit_product_cat', 'wpm_product_cat_details_meta_save' );
    /**
     * Save Product Category details meta.
     *
     * Save the product_cat details meta POSTed from the
     * edit product_cat page or the add product_cat page.
     *
     * @param  int $term_id The term ID of the term to update.
     */
    function wpm_product_cat_details_meta_save( $term_id ) {
        if ( ! isset( $_POST['wpm_product_cat_details_nonce'] ) || ! wp_verify_nonce( $_POST['wpm_product_cat_details_nonce'], basename( __FILE__ ) ) ) {
            return;
        }
        $old_details = get_term_meta( $term_id, 'details', true );
        $new_details = isset( $_POST['wpm-product-cat-details'] ) ? $_POST['wpm-product-cat-details'] : '';
        if ( $old_details && '' === $new_details ) {
            delete_term_meta( $term_id, 'details' );
        } else if ( $old_details !== $new_details ) {
            update_term_meta(
                $term_id,
                'details',
                wpm_sanitize_details( $new_details )
            );
        }
    }
    
    add_action( 'woocommerce_after_shop_loop', 'wpm_product_cat_display_details_meta' );
    /**
     * Display details meta on Product Category archives.
     *
     */
    function wpm_product_cat_display_details_meta() {
        if ( ! is_tax( 'product_cat' ) ) {
            return;
        }
        $t_id = get_queried_object()->term_id;
        $details = get_term_meta( $t_id, 'details', true );
        if ( '' !== $details ) {
            ?>
            <div class="product-cat-details">
                <?php echo apply_filters( 'the_content', wp_kses_post( $details ) ); ?>
            </div>
            <?php
        }
    }
    

    您将在名为“详细信息”的类别属性中获得一个附加字段。在此处添加您的文章,您将在类别页面的产品列表下方获得文本。这将适用于每个类别。

    【讨论】:

      【解决方案2】:

      有无数种方法可以实现这一目标。一种方法是override the archive-product.php template,并在其中包含内容。

      一般步骤是:

      1. 在您的主题根目录中创建一个/woocommerce/ 目录
      2. archive-product.php 模板从 /plugins/woocommerce/templates/ 复制到新创建的文件夹中。
      3. 编辑并保存复制的文件。

      the WooCommerce docs 中阅读有关模板覆盖的更多信息。

      【讨论】:

      • 这不只是安全地开始以任何需要的方式编辑代码的过程......编码吗?您发送给我的页面也有此消息“请注意:在您的主题文件夹中创建 woocommerce.php 时,您将无法将 woocommerce/archive-product.php 自定义模板(在您的主题中)覆盖为 woocommerce。 php 的优先级高于所有其他模板文件。这是为了防止显示问题。"
      • 我的印象是你需要在 category.php 或类似的东西中添加另一个文本框/客户字段,然后从 archive-product.php 调用它.....你可以我完全一无所知....必须有一个简单的方法来做到这一点。
      • 我提到了简单的方法。 archive-product.phpcategory.php 在管理员中没有等效项。它们是完全自动生成的。你需要按照我上面的步骤,在产品下面硬编码你想要的文本。唯一可行的替代方法是编写自己的插件以向管理员添加新的选项页面,然后修改存档模板以编程方式从该选项页面中提取数据。
      猜你喜欢
      • 1970-01-01
      • 2019-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-16
      • 2016-07-16
      相关资源
      最近更新 更多