wordpress的数据循环取值问题

查看:102
本文介绍了wordpress的数据循环取值问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

文章列表页想获取文章所属分类

<div style="display:none"><?php print_r(get_the_category(the_ID())) ?></div>

查看源码输出的结果是

252Array
(
    [0] => stdClass Object
        (
            [term_id] => 12
            [name] => 产品动态
            [slug] => cp
            [term_group] => 0
            [term_order] => 4
            [term_taxonomy_id] => 12
            [taxonomy] => category
            [description] => 
            [parent] => 10
            [count] => 1
            [object_id] => 252
            [filter] => raw
            [cat_ID] => 12
            [category_count] => 1
            [category_description] => 
            [cat_name] => 产品动态
            [category_nicename] => cp
            [category_parent] => 10
        )

    [1] => stdClass Object
        (
            [term_id] => 24
            [name] => 校OA
            [slug] => xoa
            [term_group] => 0
            [term_order] => 1
            [term_taxonomy_id] => 24
            [taxonomy] => category
            [description] => 
            [parent] => 12
            [count] => 1
            [object_id] => 252
            [filter] => raw
            [cat_ID] => 24
            [category_count] => 1
            [category_description] => 
            [cat_name] => 校OA
            [category_nicename] => xoa
            [category_parent] => 12
        )

)

怎样循环才能取到里面cat_name的值

这个问题已被关闭,原因:问题已解决 - 问题已解决,且对他人无借鉴意义

解决方案

文章页的话可以直接使用
<?php echo get_the_category(',');?>即可输出

输出数组可以
<?php

$arr=array();//空数组接收数据
$cats = get_the_category(the_ID());//分类数组
foreach($cats as $cat)://遍历
$arr[]=$cat->name;//赋值
endforeach;
//输出

?>

这篇关于wordpress的数据循环取值问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆