3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

archive.phpでカテゴリ名を表示する(リンクあり・なし)#WordPress

Posted at

WordPressにて、archive.phpにカテゴリ名(タクソノミー名)を表示させたかったときにハマったのでメモ。
以下ではタクソノミー名を「tax_news」としてます :)

##リンクあり

php
<?php echo get_the_term_list( $post->ID, 'tax_news' ); ?>
archive.php

<div class="sample-block">

    <?php if ( have_posts() ) : ?>

    <ul class="sample-list">

      <?php
      while ( have_posts() ) : the_post();
      ?>

      <li>
        <a href="<?php the_permalink(); ?>">
          <span class="date"><?php the_time('Y.m.d'); ?></span>
          <div class="category"><?php echo get_the_term_list( $post->ID, 'tax_news' ); ?></div>
          <h3 class="ttl"><?php the_title(); ?></h3>
        </a>
      </li>

      <?php endwhile; ?>

    </ul><!-- /sample-list -->

    <?php else : ?>
    何も投稿がありません。
    <?php endif; ?>


</div><!--/sample-block-->

※aタグ(ブロック要素)になるのでspanなどでは囲えない

##リンクなし

php
<?php $sample_terms = wp_get_object_terms($post->ID, 'tax_news'); ?>
<?php foreach($sample_terms as $term): ?>

~~~

<?php echo $term->name ?>

archive.php

<div class="sample-block">

    <?php if ( have_posts() ) : ?>

    <ul class="sample-list">

      <?php
      while ( have_posts() ) : the_post();
      ?>

      <?php $sample_terms = wp_get_object_terms($post->ID, 'tax_news'); ?>
      <?php foreach($sample_terms as $term): ?>

      <li>
        <a href="<?php the_permalink(); ?>">
          <span class="date"><?php the_time('Y.m.d'); ?></span>
          <div class="category"><?php echo $term->name ?></div>
          <h3 class="ttl"><?php the_title(); ?></h3>
        </a>
      </li>

      <?php endforeach; ?>
      <?php endwhile; ?>

    </ul><!-- /m-news-list -->

    <?php else : ?>
    何も投稿がありません。
    <?php endif; ?>

</div><!--/sample-block-->

##参考
カスタム投稿タイプのターム名出力_WordPress (©luuuing_web)
http://luuuing-web.com/web/wordpress/terms_name_wordpress/

3
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?