0

在主页上我正在打印帖子,我想在 foreach 循环中打印类别名称?我怎样才能做到这一点?

示例代码:

       if (have_posts()) : 

        $args = array(
            'showposts' => '5',
            'paged' => $paged
        );


        $thePosts = query_posts($args);

        foreach($thePosts as $post) : setup_postdata($post);

    ...
4

2 回答 2

1

解决了:

<?php

            GLOBAL $wpdb;

            $query = "
            SELECT 
                terms.slug, r.term_taxonomy_id, terms.name AS cat_name 
            FROM 

                {$wpdb->prefix}term_taxonomy t, {$wpdb->prefix}terms terms, $wpdb->posts p, $wpdb->term_relationships r 

            WHERE 

                t.term_id=terms.term_id AND 
                p.ID = r.object_id AND
                r.term_taxonomy_id = t.term_taxonomy_id AND 
                p.ID = '".$post->ID."'

            ORDER BY terms.term_order ASC, terms.term_id DESC";

            $categories = $wpdb->get_results($query, ARRAY_A);


            if(is_array($categories)){

                $i=1;
                foreach($categories as $category) { 

                    echo '<span>'.$category['cat_name'].'</span>';

                    ++$i;
                }#end foreach($categories 

            }#end if


            ?>
于 2012-06-19T17:27:56.273 回答
0

将此代码放在您的循环中。

<?php
/* translators: used between list items, there is a space after the comma */
$categories_list = get_the_category_list( __( ', ', 'twentyeleven' ) );

if ( '' != $categories_list ) {
$utility_text = __( 'This entry was posted in %1$s', 'twentyeleven' );
} 
printf( $utility_text );
?>

这将打印当前帖子的类别。希望您使用的是 211 的儿童主题。

于 2012-06-19T12:38:12.550 回答