Get category url in loop

时间:2018-09-28 作者:user3309614

我需要获取类别url,以便将其放入元代码段中。现在,span内的输出是带有url的标记。我只需要获取不带标记的url,并将其放入$cat\\u display中。我尝试使用2个选项,但会看到一个错误:注意:尝试获取非对象的属性

// Get post category info
$category = get_the_category();

if(!empty($category)) {
        // Get last category post is in
        $last_category = $category[count($category) - 1];

        // Get parent any categories and create array
        $get_cat_parents = rtrim(get_category_parents($last_category->term_id, true, \',\'),\',\');
        $cat_parents = explode(\',\',$get_cat_parents);

        //$category_link = get_category_link( $category_id );
        $cat_parents_url = get_category_link($cat_parents->term_id);
        // Loop through parent categories and store in variable $cat_display
        $cat_display = \'\';
        foreach($cat_parents as $parents) {
                $cat_display .= \'<li class="item-cat" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><span itemprop="item"><span itemprop="name">\'.$parents .\'</span></span><meta itemprop="position" content="\'. $counter++ .\'" /></li>\';
                $cat_display .= \'<li class="separator"> \' . $separator . \' </li>\';
        }

}

1 个回复
最合适的回答,由SO网友:Sally CJ 整理而成

注意:尝试获取非对象的属性

那是因为$cat_parents 不是object; 这是一个array:

$cat_parents_url = get_category_link($cat_parents->term_id);
其次,要仅获取类别URL,请调用get_category_parents() 将第二个参数设置为false, 像这样:

$get_cat_parents = rtrim(get_category_parents($last_category->term_id, false, \',\'),\',\');
$get_cat_parents 然后是一个列表/string ,而不是HTML格式的类别链接,例如<a href="http://example.com/category/slug">Name of the category here</a>.

然后,在foreach 循环,您可以按如下方式获取类别URL:

foreach($cat_parents as $slug) {
    $term = get_category_by_slug( $slug );
    if ( ! $term ) {
        continue;
    }

    $cat_url = esc_url( get_category_link( $term->term_id ) );
    ...
}
即获取类别/术语object/数据使用get_category_by_slug(), 然后使用get_category_link().

以下是我使用的完整代码:(为了清晰起见,重新缩进)

// Get post category info
$category = get_the_category();

if(!empty($category)) {
    // Get last category post is in
    $last_category = $category[count($category) - 1];

    // Get parent any categories and create array
    $get_cat_parents = rtrim(get_category_parents($last_category->term_id, false, \',\'),\',\');
    $cat_parents = explode(\',\',$get_cat_parents);

    $cat_display = \'\';
    $separator = \', \'; // not defined; so I defined it here.
    $counter = 0;      // not defined; so I defined it here.
    foreach($cat_parents as $slug) {
        $term = get_category_by_slug( $slug );
        if ( ! $term ) {
            continue;
        }

        $cat_url = esc_url( get_category_link( $term->term_id ) );

        $cat_display .= \'<li class="item-cat" itemprop="itemListElement" itemscope itemtype="http://schema.org/ListItem"><span itemprop="item"><span itemprop="name">\'.$cat_url .\'</span></span><meta itemprop="position" content="\'. $counter++ .\'" /></li>\';
        $cat_display .= \'<li class="separator"> \' . $separator . \' </li>\';
    }

    echo $cat_display; // test

}

结束

相关推荐

Get assigned post categories

我创建了一个名为“Crew”的自定义帖子类型,如下所示:function crew_custom_post_type() { $labels = array( \'name\' => _x( \'Pages\', \'Post Type General Name\', \'project\' ), \'singular_name\' => _x( \'Page\', \'Post Type