【问题标题】:How to display custom taxonomy on a WooCommerce product using Divi theme如何使用 Divi 主题在 WooCommerce 产品上显示自定义分类
【发布时间】:2020-05-06 04:16:52
【问题描述】:

我在 Wordpress 和 WooCommerce 上使用 Divi 主题,并且我使用插件创建了一个名为 Brand 的自定义分类法。 现在我需要在产品页面上显示品牌字段。我不知道 php,但如果我知道将代码放在哪里,我可以将代码添加到主题文件中。 我已经看到了很多类似场景的代码,但他们中的大多数都在谈论注册分类法,我相信我已经完成了(使用 CPTUI),所以我从这个页面上的 cmets 获取了代码:@ 987654321@

我将此代码块添加到functions.php:

/**
 * Get taxonomies terms links.
 *
 * @see get_object_taxonomies()
 */
function wpdocs_custom_taxonomies_terms_links() {
    // Get post by post ID.
    if ( ! $post = get_post() ) {
        return '';
    }

    // Get post type by post.
    $post_type = $post->post_type;

    // Get post type taxonomies.
    $taxonomies = get_object_taxonomies( $post_type, 'objects' );

    $out = array();

    foreach ( $taxonomies as $taxonomy_slug => $taxonomy ){

        // Get the terms related to post.
        $terms = get_the_terms( $post->ID, $taxonomy_slug );

        if ( ! empty( $terms ) ) {
            $out[] = "<h2>" . $taxonomy->label . "</h2>\n<ul>";
            foreach ( $terms as $term ) {
                $out[] = sprintf( '<li><a href="%1$s">%2$s</a></li>',
                    esc_url( get_term_link( $term->slug, $taxonomy_slug ) ),
                    esc_html( $term->name )
                );
            }
            $out[] = "\n</ul>\n";
        }
    }
    return implode( '', $out );
}
?>

然后在 Divi 代码块中输入:

<?php echo wpdocs_custom_taxonomies_terms_links(); ?>

但是什么也没发生。控制台没有错误,什么都没有。已为此产品填充分类。请帮忙!

【问题讨论】:

    标签: php wordpress woocommerce custom-taxonomy


    【解决方案1】:

    代码在 Divi 中运行良好...将截取的代码转换为短代码以将其放在 divi 页面上。

    您可以使用Insert PHP Code Snippet 创建短代码。

    【讨论】:

      猜你喜欢
      • 2019-06-15
      • 2022-01-27
      • 2021-12-13
      • 1970-01-01
      • 1970-01-01
      • 2021-07-30
      • 2016-06-12
      • 1970-01-01
      • 2018-11-24
      相关资源
      最近更新 更多