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でタグを簡単にスタイル設定する方法(例付き)

編集メモ: WPBeginner のパートナーリンクから手数料を得ています。手数料は編集者の意見や評価に影響を与えません。編集プロセスについて詳しく知る。

長年にわたり、WPBeginnerにはタグに関する多くの質問が寄せられており、そのうちのひとつが、WordPressでタグを目立たせるためにスタイルを整えるべきかどうかというものでした。

タグは、コンテンツをトピックごとに整理するのに役立ちます。WordPressブログ投稿のハッシュタグのようなもので、ユーザーがより多くのコンテンツを発見するのに役立ちます。しかし、私たちの経験では、タグが視覚的に目立たなければ、見落とされてしまうかもしれません。

この投稿では、WordPressでタグを簡単にスタイル設定し、ユーザーエンゲージメントを高め、サイトのページ表示を増やす方法をご紹介します。

Styling tags in WordPress

WordPressでタグを表示する方法

WordPressには、カテゴリーとタグと呼ばれる2つの主要なタクソノミーがあります。カテゴリーがコンテンツの主要なエリアに使用されるのに対し、タグはコンテンツをより具体的なトピックに分類することができます。

WordPressの人気テーマの多くは、初期設定でタグを投稿の上部または下部に表示します。

Tags below post in WordPress

しかし、アーカイブページ、フッター、サイドバーなど、WordPressの好きな場所にタグを表示することもできます。

投稿、ページ、サイドバーウィジェットにタグクラウドを挿入するには、「タグクラウド」ブロックを追加するだけです。

Tag cloud block

タグクラウドでは、投稿数に応じて各タグのフォントサイズが異なります。また、各タグの次の投稿数を表示することもできます。

Tag cloud preview

これらはWordPressで利用できる初期設定のオプションに過ぎませんが、タグをさらにカスタマイズしたいとしたらどうしますか?その方法をご紹介しよう。

WordPressでタグを簡単にスタイル設定する方法を見てみよう。

WordPressの初期設定タグクラウドをスタイリングする

タグクラウドブロックを投稿やページに追加したら、カスタムCSSを追加してカスタマイズできます。

タグクラウドブロックには、WordPressで初期設定されたCSSクラスが自動的に含まれ、それを使ってスタイルを設定することができます。

WordPressサイトにカスタムCSSを追加するには、外観 ” カスタマイザーページに移動し、追加CSSタブに切り替えるだけです。

Styling tag cloud

手始めに、このカスタムCSSコードを追加してください。

.tag-link-position-1 { font-size:40px!important;}
.tag-link-position-2 {  font-size:35px!important;}
.tag-link-position-3 { font-size:30px!important; }
.tag-link-position-4 { font-size:35px!important; }
.tag-link-position-5 {  font-size:30px!important; }
 
 
.tag-cloud-link {
    text-decoration:none;
    background-color:#fff;
}
.tag-link-count {
    background-color: #d6d6d6;
}

ご覧のように、.tag-link-positionクラスを使って、リンクの位置に応じてスタイルを調整することができます。投稿数の多いタグは位置が高く、投稿数の少ないタグは位置が低くなります。

タグクラウドブロック内のすべてのタグを同じサイズにしたい場合は、代わりに以下のCSSを使用することができます:

.tag-cloud-link { 
font-size:16px !important;
border:1px solid #d6d6d6;
}
.tag-cloud-link {
    text-decoration:none;
    background-color:#fff;
}
.tag-link-count {
    background-color: #d6d6d6;
 
}

私たちのテストサイトではこのように見えた:

Alternate tag cloud style

カテゴリー:WordPressの投稿タグのスタイリング

タグクラウドのスタイリングに加えて、個々のブログ投稿日に表示される投稿タグのスタイリングも行いたい場合があります。通常、WordPressテーマは投稿タグを投稿タイトルまたは投稿コンテンツの上部または下部に表示します。

タグにマウスオーバーして右クリックすると、WordPressテーマで使用されているCSSクラスを表示するInspectツールを使用できます。

Using inspect tool to find tags class

その後、カスタムCSSでこれらのCSSクラスを使用することができます。以下は、テスト・テーマのCSSクラスに基づいたサンプル・コードです:

.entry-tag { 
padding: 5px 8px;
border-radius: 12px;
text-transform: lowercase;
background-color: #e91e63;
color:#fff;
}
.entry-tags a { 
color:#fff;
font-size:small;
font-weight:bold;
}

私たちのテストサイトではこのように見えた。

Post tags styled

WordPressでカスタムタグクラウドを作成する

初期設定のタグクラウド・ブロックは使いやすいが、上級ユーザーの中には、より柔軟にカスタマイズしたいと思う人もいるだろう。

この方法では、タグクラウドを囲む独自のHTMLとCSSクラスを追加することができます。タグクラウドの外観を自分の必要に合わせてカスタマイズすることができます。

まず最初に、テーマのfunctions.phpファイルまたはコードスニペットプラグインにこのコードをコピー&ペーストします:

function wpb_tags() { 
$wpbtags =  get_tags();
foreach ($wpbtags as $tag) { 
$string .= '<span class="tagbox"><a class="taglink" href="'. get_tag_link($tag->term_id) .'">'. $tag->name . '</a><span class="tagcount">'. $tag->count .'</span></span>' . "\n"   ;
} 
return $string;
} 
add_shortcode('wpbtags' , 'wpb_tags' );

WordPressに最適なコードスニペットプラグインであるWPCodeを使って、このコードを追加することをお勧めします。テーマのfunctions.phpファイルを編集することなく、WordPressに簡単にカスタマイザーのコードを追加することができます。

開始するには、無料のWPCodeプラグインをインストールして有効化します。ヘルプが必要な場合は、WordPressプラグインのインストール方法のチュートリアルをご覧ください。

プラグインを有効化したら、WordPressダッシュボードからCode Snippets ” Add Snippetに向かいます。

次に、「カスタムコードを追加(新規スニペット)」オプションにマウスオーバーし、「スニペットを使用」ボタンをクリックします。

Add a new custom code snippet in WPCode

ここから、ページ上部にスニペットのタイトルを追加することができます。これは、何のためのコードなのかを覚えておくのに役立つものであれば何でも構いません。

次に、上記のコードを「コードプレビュー」ボックスに貼り付け、ドロップダウンメニューからコードタイプとして「PHPスニペット」を選択します。

Paste snippet into WPCode

その後、スイッチを’Inactive’から’Active’に切り替え、’Save Snippet’ボタンをクリックするだけです。

Activate and save your custom code snippet

このコードは、すべてのタグとその次の投稿数を表示するショートコードを追加します。

アーカイブページやウィジェットに表示するには、このショートコードを使用する必要があります:

[wpbtags]

このコードだけでは、タグリンクとその次の投稿数が表示されるだけです。見栄えを良くするためにCSSを追加してみましょう。このカスタムCSSをコピー&ペーストしてサイトに貼り付けるだけです。

.tagbox { 
background-color:#eee;
border: 1px solid #ccc;
margin:0px 10px 10px 0px;
line-height: 200%;
padding:2px 0 2px 2px;
  
}
.taglink  { 
padding:2px;
}
  
.tagbox a, .tagbox a:visited, .tagbox a:active { 
text-decoration:none;
}
  
.tagcount { 
background-color:green;
color:white;
position: relative;
padding:2px;
}

CSSはご自由に変更してください。デモサイトではこのように表示されました:

Custom tag cloud

この投稿が、WordPressでタグを簡単にスタイル設定する方法のお役に立てば幸いです。また、WordPressでサブカテゴリーを非表示にする方法や、 WordPressの便利なTips、トリック、ハックのリストもご覧ください。

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

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

Avatar

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

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

  1. Syed Balkhi says

    Hey WPBeginner readers,
    Did you know you can win exciting prizes by commenting on WPBeginner?
    Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
    You can get more details about the contest from here.
    Start sharing your thoughts below to stand a chance to win!

  2. Dennis Muthomi says

    the step-by-step guide and code snippets are really helpful

    QUICK QUESTION: If I apply these code snippets to style my tags and later decide to change my WordPress theme, will the tag styling change according to the new theme’s default styles, or will it stick with the customizations I made using these code snippets?

    • WPBeginner Comments says

      The CSS you added should remain, but you may still have to edit the CSS depending on how the new theme’s CSS effects the styling, and if there are any conflicts.

  3. Nikola says

    Great guide, thanks!

    Would it be possible to order the tags by most used and limit the number of tags shown?

    • WPBeginner Support says

      At the moment you would need to use a plugin for that or custom code, we do not have a recommended method for that at the moment.

      管理者

  4. Joe Smith says

    This is great! Exactly what I was looking for and very classy. Just one question: If I wanted to add more space between the tags, how would i modify the css? I’ve tried changing several of the settings in the css above but none of them simply add whitespace between the tags.

    • WPBeginner Support says

      For that, you would want to reach out to your specific theme’s support for assistance as each theme has its own location for the featured image.

      管理者

  5. Trishah says

    I’m trying to add a CSS class “current” to tags for a single post. The few solutions I’ve found no longer seem to work. How would I do this?

    • WPBeginner Support says

      That would vary based on your specific theme, if you reach out to your theme’s support they should be able to let you know where you can add that class for your tags.

      管理者

  6. uche Peter says

    My tags are listed serially.. No Border nothing nothing,,, just a text …please how do I give it a border with background color

  7. Hugo says

    My tag cloud doesn’t come as a cloud but as a list, since after each span a line break is inserted. How can I remove these line breaks?

  8. bob says

    I get that you showed another page on how to limit tags to a certain number, but how can I do it with this example included? It doesn’t seem to work using the method linked in combination with this.

  9. Amy Croson says

    I’m struggling with adding this, and I believe it’s with how my ‘Chosen’ Theme is limiting me. I’ve copied & pasted all into my php, & it is giving me errors left and right. I’m currently being told that there is an unknown “.” in this detail. Is there any way to format this without using the “.”s before .taglink, .tagbox, & .tagcount?

  10. Val Archer says

    Hi – I’m looking for a way to list tags on a category-slug.php page. Do you perhaps know where I can find that tutorial?

    So far I’ve created a category [sitename]/category/recipes and a category-recipes.php.

    I’m tagging the recipes, e.g. Breakfast, Dinner, etc

    I’d like the Tags to display in category-recipes.php like this. I don’t know what I’m doing. I’m copying from my category.php file.

    Archives for the Category:

    Breakfast
    now it lists all the posts tagged with Breakfast something like this:

    <a href="”>
    by / / posted in cat_name . ‘ ‘;} ?>

    Dinner
    now it lists all the posts tagged with Dinner something like this:

    <a href="”>
    by / / posted in cat_name . ‘ ‘;} ?>

    [Then at the end of all the Tags:]
         </div

    Please do you know where I can get help on this?

    thank you! – Val

  11. Magistar says

    Code works fine for a tag cloud. However I would like to apply this to the post tags (meaning the tags associated with a post and their count, instead of the big list).

    I tried something with wp_get_post_tags but I did not really got it to work. Any suggestions?

  12. DER KUNSTBLOG says

    That’s great, I’ve added your code to a site-specific plugin – but is there a possibility, to change it in a way, that the font-size vary on how often the tag is used? Any idea please? Thanks!

    • WPBeginner Staff says

      Yes it is possible, in the first piece of code you would need to replace get_tags with wp_tag_cloud function. Like this:


      $wpbtags = wp_tag_cloud('smallest=15&largest=40&number=50&orderby=count&format=array');

返信を残す

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