wordpress的stdClass处理方式
ken 2009-11-11 Hit: 317
下面是wordpress 的栏目返回值,从里面发现了stdClass 想输出里面第一个栏目ID
代码是:
$cat_id= get_the_category( $post->id );
echo $cat_id[0]->term_id;
跟JSON的对象输出一样
function get_the_category_list( $separator = '', $parents='', $post_id = false ) {
global $wp_rewrite;
// 找到当前文章对应的分类 (数组, 因为文章可以属于多个分类)
$categories = get_the_category( $post_id );
// 如果分类数组为空, 在 the_category 方法中当作 '未分类' 进行处理
if ( empty( $categories ) )
return apply_filters( 'the_category', __( 'Uncategorized' ), $separator, $parents );
$rel = ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) ? 'rel="category tag"' : 'rel="category"';
// 准备返回的 HTML 字符串
$thelist = '';
// 如果分隔符为空, 返回分类的 HTML 列表
if ( '' == $separator ) {
// 列表的开头, 是一个 unordered list
$thelist .= '<ul class="post-categories">';
// 循环处理所有分类
foreach ( $categories as $category ) {
// 每个分类项的开头
$thelist .= "\n\t<li>";
switch ( strtolower( $parents ) ) {
// 如果以 'multiple' 模式显示父分类, 每层的分类会独立成一个链接
case 'multiple':
// 父分类存在的话, 获取父分类的 HTML 代码并拼接到准备输出的 HTML 字符串中
if ( $category->parent )
$thelist .= get_category_parents( $category->parent, true, $separator );
// 最后把当前分类的 HTML 代码和分类也拼接到准备输出的 HTML 字符串中
$thelist .= '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . $rel . '>' . $category->name.'</a></li>';
break;
// 如果以 'single' 模式显示父分类, 所有分类作为一个链接
case 'single':
// 链接的开头
$thelist .= '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . $rel . '>';
// 父分类存在的话, 获取父分类的 HTML 代码并拼接到准备输出的 HTML 字符串中
if ( $category->parent )
$thelist .= get_category_parents( $category->parent, false, $separator );
// 当然分类的名字和链接的结尾
$thelist .= $category->name.'</a></li>';
break;
// 如果以默认模式显示父分类, 即不显示父分类
case '':
default:
$thelist .= '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . $rel . '>' . $category->cat_name.'</a></li>';
}
}
// 列表的结尾
$thelist .= '</ul>';
// 如果分隔符不为空, 返回一段格式化的 HTML
} else {
// 计数器
$i = 0;
// 循环处理所有分类
foreach ( $categories as $category ) {
// 第一个分类的前面不显示分隔符
if ( 0 < $i )
$thelist .= $separator . ' ';
switch ( strtolower( $parents ) ) {
// 如果以 'multiple' 模式显示父分类, 每层的分类会独立成一个链接
case 'multiple':
if ( $category->parent )
$thelist .= get_category_parents( $category->parent, true, $separator );
$thelist .= '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . $rel . '>' . $category->cat_name.'</a>';
break;
// 如果以 'single' 模式显示父分类, 所有分类作为一个链接
case 'single':
$thelist .= '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . $rel . '>';
if ( $category->parent )
$thelist .= get_category_parents( $category->parent, false, $separator );
$thelist .= "$category->cat_name</a>";
break;
case '':
// 如果以默认模式显示父分类, 即不显示父分类
default:
$thelist .= '<a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . $rel . '>' . $category->name.'</a>';
}
// 计数器自增
++$i;
}
}
return apply_filters( 'the_category', $thelist, $separator, $parents );
}

欢迎订阅: 