无法在 WooCommerce 的某些模板中获取类别对象

2024-03-03

我在用着get_category()通过 ID 获取类别对象。这里category 39是的孩子category 37。例如,

Courses (37) 
    - Programming (39)

我的问题是,如果我使用get_category(37 or 39) in functions.php两者都会回来null。如果我使用get_category(37 or 39) in single-product.php37(根类别)将返回 null。如果我使用相同的调用add-to-cart/simple.php两者都会返回一个对象。

首先调用 WooCommerce 函数,然后single-product.php进而add-to-cart/simple.php模板。

怎么了?
为什么它的工作取决于文件?


@edit

get_term( 37, 'category' ); 

似乎也失败了


@编辑 13/7 -正确的工作解决方案

在阅读答案之前我设法解决了这个问题:

$category = get_term_by('id', $category_id, 'product_cat');

参考:

  • 函数 get_term_by() https://developer.wordpress.org/reference/functions/get_term_by/
  • 显示带有标题的类别列表 https://wordpress.org/support/topic/show-category-list-with-titles
  • 如何从 woocommerce 中的 id 产品获取类别名称 https://stackoverflow.com/questions/22838939/how-to-get-the-category-name-from-an-id-product-in-woocommerce/22844327#22844327

你不能使用get_category() https://developer.wordpress.org/reference/functions/get_categories/ or get_term() https://developer.wordpress.org/reference/functions/get_terms/直接凭身份证到处走就可以了。您需要使用列出的更多参数in here https://developer.wordpress.org/reference/functions/get_terms/ (见下面的例子)。在模板上,我认为这也取决于所显示的产品(如果它们有此类别或子类别)。

来检索想要的类别对象你需要这样做类别蛞蝓你会使用get_category_by_slug('the_slug') https://codex.wordpress.org/Function_Reference/get_category_by_slug反而。然后您可以使用以下命令检索 ID:

$idObj = get_category_by_slug('my_category_slug'); 
$id = $idObj->term_id;

其他有用的 WordPress 功能:
要根据类别 ID 检索类别名称,您需要使用get_the_category_by_ID() https://developer.wordpress.org/reference/functions/get_the_category_by_id/.
您还可以通过以下方式检索 ID分类名称你会使用get_cat_ID( 'cat_name' ) https://codex.wordpress.org/Function_Reference/get_cat_ID.


列出产品类别和子类别get_category() https://developer.wordpress.org/reference/functions/get_categories/ (例子):

这是一个基于函数的示例这个线程 https://stackoverflow.com/questions/21009516/get-categories-from-wordpress-woocommerce/21012252#21012252,这将列出所有产品类别和子类别(到处):

function products_cats_subcats(){
    $taxonomy     = 'product_cat';
    $orderby      = 'name';
    $show_count   = 0;      // 1 for yes, 0 for no
    $pad_counts   = 0;      // 1 for yes, 0 for no
    $hierarchical = 1;      // 1 for yes, 0 for no
    $title        = '';
    $empty        = 0;

    $args = array(
        'taxonomy'     => $taxonomy,
        'orderby'      => $orderby,
        'show_count'   => $show_count,
        'pad_counts'   => $pad_counts,
        'hierarchical' => $hierarchical,
        'title_li'     => $title,
        'hide_empty'   => $empty
    );
    $all_categories = get_categories( $args );
    echo '<ul>';
    foreach ($all_categories as $cat) {
        if($cat->category_parent == 0) {
            $category_id = $cat->term_id;
            echo '<li><a href="'. get_term_link($cat->slug, 'product_cat') .'">'. $cat->name .'</a></li>';

            $args2 = array(
                'taxonomy'     => $taxonomy,
                'child_of'     => 0,
                'parent'       => $category_id,
                'orderby'      => $orderby,
                'show_count'   => $show_count,
                'pad_counts'   => $pad_counts,
                'hierarchical' => $hierarchical,
                'title_li'     => $title,
                'hide_empty'   => $empty
            );
            $sub_cats = get_categories( $args2 );
            echo '<ol>';
            if($sub_cats) {
                foreach($sub_cats as $sub_category) {
                    echo  '<li><a href="'. get_term_link($sub_category->slug, 'product_cat') .'">' . $sub_category->name .'</a></li>';
                }
            }
            echo '</ol>';
        }
    }
    echo '</ul>';
}

要使用它,只需将其放在您想要的位置即可:<?php products_cats_subcats() ;?>

这将显示按层次排序的所有类别和子类别Name,以及每个类别或子类别的相应链接。


然后你还可以使用get_term_by() https://codex.wordpress.org/Function_Reference/get_term_by获取类别名称或别名:

$term = get_term_by('id', $term_id, 'product_cat', 'ARRAY_A');

$term['name']; //get the WC category name
$term['slug']; //get the WC category slug

然后现在您将能够构建自己的功能,以满足您的需求......


参考:

  • 从 Wordpress Woocommerce 获取类别 https://stackoverflow.com/questions/21009516/get-categories-from-wordpress-woocommerce/21012252#21012252
  • 代码参考 > 函数 > get_categories()* https://developer.wordpress.org/reference/functions/get_categories/
  • 代码参考 > 函数 > get_terms()(和所有可选参数)* https://developer.wordpress.org/reference/functions/get_terms/
  • 代码参考 > 函数 get_term_by() https://codex.wordpress.org/Function_Reference/get_term_by
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

无法在 WooCommerce 的某些模板中获取类别对象 的相关文章

随机推荐