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 );
?>

これにより、現在の投稿のカテゴリが印刷されます。21の子テーマを使用していることを願っています。

于 2012-06-19T12:38:12.550 に答える