【问题标题】:Can't get category object in some templates of WooCommerce在 WooCommerce 的某些模板中无法获取类别对象
【发布时间】:2016-11-12 07:12:08
【问题描述】:

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

Courses (37) 
    - Programming (39)

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

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

发生了什么?
为什么它会根据文件起作用?


@编辑

get_term( 37, 'category' ); 

似乎也失败了


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

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

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

参考资料:

【问题讨论】:

  • 不,我会更新它。我的意思是一个或另一个。如果我使用 37 或 39,则该术语只能在某些文件中访问,而在其他文件中则无法访问。我不知道为什么
  • 哦。所以你用一个值调用它,然后用另一个值调用它在不同的时间
  • 这就是我正在做的事情

标签: php mysql wordpress woocommerce categories


【解决方案1】:

您不能在任何地方直接使用带有 ID 的 get_category()get_term()。您需要使用列出的更多参数in here(参见下面的示例)。在模板上,我认为这也取决于显示的产品(如果他们有这个类别或子类别)。

要检索所需的category object,您需要通过category slug 来完成,您将使用get_category_by_slug('the_slug') 代替。然后您可以通过以下方式检索 ID:

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

其他有用的 WordPress 功能:
要根据类别 ID 检索类别名称,您需要使用 get_the_category_by_ID()
您还可以通过 类别名称 检索 ID,您将使用 get_cat_ID( 'cat_name' )


使用get_category() 列出产品类别和子类别(示例)

这是一个基于this thread 的函数示例,它将列出所有产品类别和子类别(无处不在)

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>';
}

要使用它,只需将其放在您想要的位置:&lt;?php products_cats_subcats() ;?&gt;

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


那么你也可以使用get_term_by()来获取类别名称或slug:

$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

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


参考:

【讨论】:

  • get_the_category_by_ID 的问题在于它只返回类别的名称。我们不能在同一个 Wordpress 实例的某些模板中使用这个函数get_category,但我们可以在其他一些模板中使用这一事实对我来说似乎很奇怪。我的意思是,我认为它应该在数据库中找到类别,这将独立于模板或其他任何东西..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-11
  • 1970-01-01
  • 2013-06-10
  • 2015-04-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多