サポート » 使い方全般 » query_postsで指定しているのと違うカテゴリ名が出る

  • お世話になります。WP初心者です。
    <?php query_posts('category_name=album&posts_per_page=4'); ?>
    で、スラッグ[album]というカテゴリを指定しているのですが
    その後に入れている
    <?php the_category(''); ?>では、[album]カテゴリ名ではなく
    一番最後に書いた記事のカテゴリが出てしまいます。
    つまり、最後に書いたのが、ニュースの場合は「ニュース」
    最後が「日記」であれば、「日記」と毎回、カテゴリ名が入れ替わるのです。

    そのタグの前にも
    <?php query_posts...で、指定している他のカテゴリが2つあるのですが
    <?php wp_reset_query();?> で、リセットされているのでは、と思うのですが、他の原因がわかりません。
    どなたか、解決法をご教授いただけませんでしょうか?
    どうぞ、よろしくお願いいたします。

    —–入れているタグ 問題は、3つめの”query_posts”—————

    <!-- box01 massage -->
    <div class="box01 clearfix">
    <div class="box01con01">
    <?php query_posts('pagename=message'); ?>
    <?php if(have_posts()): while(have_posts()):the_post(); ?>
    <p class="img01"><?php the_post_thumbnail('ssize'); ?></p>
    <div class="clearfix">
    <p class="newsTitle"><a name="46" id="46"></a><?php echo get_post_meta($post->ID,'メッセージのタイトル',true); ?></p>
    <?php the_content(); ?>
    </div>
    <?php endwhile; endif; ?>
    <?php wp_reset_query();?>
    </div>
    
    <!-- news -->
    <div class="box01con02">
    <p class="ban02"></p>
    <div class="news clearfix">
    <?php query_posts('category_name=news&posts_per_page=8'); ?>
    <?php if(have_posts()): while(have_posts()):the_post(); ?>
    <dl class="clearfix">
    <dt><?php echo get_the_date('y.n.j'); ?></dt>
    <dd><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></dd>
    </dl>
    <?php endwhile; endif; ?>
    <?php wp_reset_query();?>
    <p class="clear"></p>
    </div>
    </div>
    <p class="clear"></p>
    </div>
    
    <!-- Album -->
    <div class="box02 clearfix">
    <?php query_posts('category_name=album&posts_per_page=4'); ?>
    <p class="txt02"><?php the_category(''); ?></p>
    <?php if(have_posts()): while(have_posts()):the_post(); ?><dl>
    <dt><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('ssize'); ?></a></dt>
    <dd><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></dd>
    <dd><?php echo mb_substr(get_the_excerpt(), 0, 30); ?>…</dd>
    </dl>
    <?php endwhile; endif; ?>
    <?php wp_reset_query();?>
    <p class="clear"></p>
    </div>

3件の返信を表示中 - 1 - 3件目 (全3件中)
  • ん?the_categoryはループ中じゃなきゃだめじゃないっすか?

    the_なんとか() はループの中で the_post() した記事が対象です。

    カテゴリーアーカイブの情報なら以下のような書き方で OK です◎

    $category = get_category( intval( get_query_var( 'cat' ) ) );
    echo '<a href="' . get_category_link( $category->term_id )
      . '" title="' . esc_attr( sprintf( __( "View all posts in %s" ), $category->name ) )
      . '">' . $category->name.'</a>';

    トピック投稿者 fujifuji

    (@fujifuji)

    ありがとうございます!
    できました。
    リンクをしないのであれば、

    <?php $category = get_category( intval( get_query_var( 'cat' ) ) );
    echo $category->name; ?>

    と書けばいいのですね!?

    ありがとうございました!!

3件の返信を表示中 - 1 - 3件目 (全3件中)
  • トピック「query_postsで指定しているのと違うカテゴリ名が出る」には新たに返信することはできません。