Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPBカップ
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

WordPressテーマで投稿後にタグを限定表示する方法

最近、WordPressテーマで各投稿の後にタグを限定して表示する方法を尋ねられました。通常、投稿が属するタグへのリンクを表示するにはthe_tags()関数を使用します。しかし、この関数には表示するタグの数を制限するパラメータがありません。そのため、投稿に12個のタグがあるにもかかわらず、テーマには5個分のスペースしかない場合、デザイン的にあまりよく見えないかもしれません。多くの人々はタグの使用を制限するか、テンプレートにタグを含めないようにしています。しかしこの投稿では、WordPressテーマで投稿後のタグの数を制限する方法を紹介します。

編集:どうやらこの投稿を書いた後、最も素晴らしいOtto(@otto42)が私のGoogle+アカウントに返信し、これを実現するもっと簡単な方法があることを教えてくれたようだ。

まず、テーマのfunctions.phpファイルを開き、この関数を追加する必要がある:

[cbk1]

5]の数字を最大数に変更することができます。

次に、loop.php、個別投稿.php、index.php、またはこれらの投稿タグを追加したい場所(投稿ループ内でなければなりません)を開き、次のコードを貼り付けます:

[cbk2]

これは間違いなく、私が思いついたものよりずっと簡単です。

古い複雑な方法

次のコードをテーマファイル(投稿ループ内)に貼り付けるだけです:

[cbk3]

上記のコードでは、テーマに6つのタグが表示されます。タグの表示数を減らしたい、あるいは増やしたい場合は、$count > 5の行を好きな数で調整するだけです。カウント数が5より大きくても、タグは6個表示されることを覚えておいてください。つまり、タグを4つだけ表示したいのであれば、カウント数を3にする必要があります。

区切りを変更したい場合は、9行目を変更する必要があります。現在のコードではカンマで区切られています。divやリスト要素など、好きなものを追加してスタイリングをカスタマイズすることもできます。

情報開示 私たちのコンテンツは読者支援型です。これは、あなたが私たちのリンクの一部をクリックした場合、私たちはコミッションを得ることができることを意味します。 WPBeginnerの資金源 をご覧ください。3$編集プロセスをご覧ください。

アバター

Editorial Staff at WPBeginner is a team of WordPress experts led by Syed Balkhi with over 16 years of experience in WordPress, Web Hosting, eCommerce, SEO, and Marketing. Started in 2009, WPBeginner is now the largest free WordPress resource site in the industry and is often referred to as the Wikipedia for WordPress.

究極のWordPressツールキット

ツールキットへの無料アクセス - すべてのプロフェッショナルが持つべきWordPress関連製品とリソースのコレクション!

Reader Interactions

19件のコメント返信を残す

  1. Jilaan

    Hi Dear Sir,

    sir i want to show randomly tags in widget how can i do that can you give a code which i can added on my function.php file so its showing randomly tags with limit of tags number.

    Thanks

  2. C. Dee

    This code snippet works great.

    But I need one modification: I need to only show the first three tags that I enter for each blog post. It’s now showing three tags, which is a good step.

    However, I specifically need the first three in the order I specify. This is because of the purpose my website serves and how research data is presented. For example:

    Jennifer Weaver, Case #4455883, New Mexico

    That’s how I’m entering tags: Name, case number, location — and then I have some additional tags. But I only want the first three to display in the entry meta.

    Any suggestions how to do this? The code you shared chooses any order it wants. If my tags are entered like this — one, two, three, four, five — it chooses its own arrangement, such as:

    five, four, one

    Thanks for any help you can offer with this!!

  3. mahmood dabestani

    Hi.
    this post was very useful.
    But I’m going to create a single page like contact page that it has descriptions about one of my products.
    how can I add my favorite tags about that descriptions in my page?
    in other word how can I add my favorite tags in a single page?
    thank you so much.

  4. joni

    How for showing random tags?
    example: in my posting blog have 20 tags,
    And i want showing tags number 1, tag number 6, tag number 5,
    and another post have 20 some tags,
    and i want showing tags number 7, 17, 3.
    ETC

  5. Hector P

    Hello!

    How can I do this “limit the usage of tags”. I want only 3 tags per post/product. And if I select a 4, the number is erased.

    Thank you!

  6. Gabrielle

    Hi, how can i add if is statement to this function?

    I want to show 5 tags on my main Blog Page(Static page with id=99),
    and then show all tags in single-post page
    Is it possible to combine this function, to work with post category?
    Like Above, i want 5 categories and 5 tags on main page, and full list on single-post…
    Sorry for my English ;/

    • WPBeginner Support

      For that you will have to edit your theme or child theme’s template file. For tags you can try this code:

      <?php
      $posttags = get_the_tags();
      $count=0;
      if ($posttags) {
      	$output = '';
      	foreach($posttags as $tag) {
      		$count++;
      		$output .= $tag->name . ' ';
      		if( $count >5 ) break;
      	}
      }
      echo $output;
      ?>
      

      管理者

  7. Andrew

    Great info. I came to this site though google search looking for an answer to another problem I need the tag pages to display their tag names as a heading for their individual tag pages. Is there any theme that does this? I am currently using the Divi theme and that feature is not available.

  8. Mehmet Çağatay

    How to print the labels at random?
    For example, 5 out of 10 have a label that is shown on a random

  9. Jill

    Just FYI…when I pasted those 4 lines in my functions file…my entire site went blank. I about had a panic attack. My hosting company was able to get me back up.

  10. ata

    What about duplicate content (after Panda update) using tags as same article will be on the post and tag pages.

    Also, i read on wp forum regarding the negative impact of using excessive tags (more than 10 tags) for each post.

    • wpbeginner

      @ata People should not use Tags for SEO. Tags are built-in to WordPress to improve the usability of your site. You can have 500 tags for one post for all that matters. What is crucial is that you follow, noindex the tags pages. So search engines go to those pages and follow all the links, but don’t index those pages. That will solve the SEO problem. There are too many self-proclaimed SEO experts who don’t know what the heck they are doing. Putting generic statements like the ones you mentioned above are totally wrong as long as you follow what we are saying.

      • ata

        Actually tags does increase traffic for some sites. You can find proof on Google. Especially long tail keywords attached to a post.

        Not sure how the search engine can find your tag links if it is set as ‘noindex’. Maybe the post url but not the tag links as it doesn’t make sense.

        The ‘generic statement’ was not a statement.at all. If you read my comment, it is more of a query/question so there is no need to bite my head off.

        • wpbeginner

          @ata First of all let me be clear. I am not biting your head off. The frustration was expressed toward those experts that go along making generic claims like that. WordPress allows for many ways of indexation. Most folks use category as their primary method of archives. In order to prevent duplicate content, you can only use ONE primary method of archive that is indexable. All secondary methods need to be no-index in order to prevent duplicate content.

          We use Categories as our main method of archiving. If you want you can use tags as a primary source of archiving. We find that categories tend to perform better for our blogs. If you want to use tags as a primary source of archiving, then no-index categories, and date based archives. If you are the only author on the site, then you might as well no-index author archives because that will just produce duplicate content as well.Will not argue SEO theories any further in this article because this is a tutorial for design not SEO.

  11. astronyu

    I was looking for this. Thanks for sharing. I agree with @mrahmadawais the first method works just fine and it is simple too.

  12. Livefyre

    the first method is just great

返信を残す

コメントありがとうございます。すべてのコメントは私たちのコメントポリシーに従ってモデレートされ、あなたのメールアドレスが公開されることはありませんのでご留意ください。名前欄にキーワードを使用しないでください。個人的で有意義な会話をしましょう。