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 のパートナーリンクから手数料を得ています。手数料は編集者の意見や評価に影響を与えません。編集プロセスについて詳しく知る。

WordPressでアーカイブの月数を表示しますか?

何年もブログを書いていると、アーカイブリストが長くなりすぎることに気づくだろう。

この投稿では、WordPressでアーカイブの表示月数を制限する方法を紹介します。

How to limit number of archive months in WordPress

動画チュートリアル

Subscribe to WPBeginner

動画が気に入らなかったり、もっと説明が必要な場合は、このまま読み進めてください。

方法1.プラグインでアーカイブ月数を制限する。

この方法は簡単で、すべてのユーザーにお勧めできる。

まず最初に、Collapsing Archivesプラグインをインストールして有効化します。詳しくは、WordPressプラグインのインストール方法のステップバイステップガイドをご覧ください。

有効化したら、外観 ” ウィジェットページにアクセスし、サイドバーに折りたたみアーカイブウィジェットを追加する必要があります。

Collapse archives

ウィジェットメニューが展開され、設定が表示されます。

折りたたみアーカイブウィジェットは、JavaScriptを使用してアーカイブリンクを折りたたみ可能な年別リンクに折りたたみます。ユーザーは年月をクリックすることで、月別アーカイブを表示することができます。月別アーカイブを折りたたみ可能にして、その下に投稿タイトルを表示させることもできます。

あなたのニーズに合うようにウィジェットの設定を見直して、「保存」ボタンをクリックして設定を保存してください。

これで、あなたのサイトにアクセスして、ウィジェットの動作を確認することができます。

Collapsing archives

方法2.初期設定のアーカイブをコンパクトアーカイブに置き換える

この方法は、初期設定のアーカイブウィジェットに代わり、アーカイブをコンパクトに美しく表示します。

まず、Compact Archivesプラグインをインストールして有効化する必要があります。詳しくは、WordPressプラグインのインストール方法のステップバイステップガイドをご覧ください。

有効化したら、外観 ” ウィジェットページにアクセスし、「コンパクトアーカイブ」ウィジェットをサイドバーに追加する必要があります。

Compact Archives settings

Compact Archivesプラグインには3つのスタイルがあります。ブロック、イニシャル、数字から選択できます。

保存」ボタンをクリックして、ウィジェット設定を保存することを忘れないでください。

コンパクト・アーカイブが活躍する様子を、あなたのサイトでご覧いただけます。

Compact Archives preview

また、Compact Archivesプラグインを使用すると、サイトにカスタムアーカイブページを作成することができます。

詳しくはWordPressでコンパクトなアーカイブを作成する方法をご覧ください。

方法3.WordPressでアーカイブの月数を手動で制限する

この方法では、WordPressテーマファイルにコードを追加する必要があります。WordPressでコードをコピー&ペーストする方法をご覧ください。

テーマのfunctions.phpファイルまたはコードスニペットプラグインに以下のコードを追加する必要があります。

// Function to get archives list with limited months
function wpb_limit_archives() { 
 
$my_archives = wp_get_archives(array(
    'type'=>'monthly', 
    'limit'=>6,
    'echo'=>0
));
     
return $my_archives; 
 
} 
 
// Create a shortcode
add_shortcode('wpb_custom_archives', 'wpb_limit_archives'); 
 
// Enable shortcode execution in text widget
add_filter('widget_text', 'do_shortcode'); 

WordPressコードスニペットプラグインのWPCodeを使用してコードを追加することをお勧めします。これにより、functions.phpファイルを編集することなく、WordPressにカスタマイザーコードを安全に追加することができます。

最初に行う必要があるのは、無料のWPCodeプラグインをインストールして有効化することです。WordPressプラグインのインストール方法については、こちらをご覧ください。

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

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

Add a new custom code snippet in WPCode

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

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

Paste the code snippet into WPCode

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

Activate and save your custom code snippet

このコードはアーカイブリストを取得し、過去6ヶ月のみに制限します。そして、ショートコードを作成し、テキストウィジェットでショートコードを有効化します。

外観 ” ウィジェットページに行き、サイドバーに’テキスト’ウィジェットを追加することができます。テキストモードに切り替えて、以下のようにショートコードを追加します:

<ul>
[wpb_custom_archives]
</ul>

ウィジェットの設定を保存することを忘れないでください。

カスタマイザーで作成したアーカイブリストは、あなたのサイトでご覧いただけます。

以上、WordPressでアーカイブの表示月数を制限する方法について、この投稿がお役に立てれば幸いです。WordPressの関数ファイルに関する最も便利なトリックのリストや、WordPressでアーカイブページを削除する方法に関するガイドもご覧ください。

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

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

  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. Jenny says

    I used this code and it worked perfectly.
    But the problem is that whenever I select a month, it takes me to the homepage.

    • WPBeginner Support says

      It would depend on the method you are using. If you are using a plugin then you would want to reach out using the plugin’s support forum

      管理者

  3. Gaz says

    Hi,

    I would like to ask how can I show the amount of posts next to the month in the archive list in the footer?

    Example:

    May 2018 (5)

    I put the above code on the site however I would like to show how many posts have been published each month as well.

    Thanks

    Gaz

  4. Gaelyn says

    Seems rather stupid to have to add plugins to stop something I didn’t ask for in the first place. How about just on/off. No reason to load all the archives.

  5. Stegemüller says

    Hi there,

    I simply love your site! I have been using WP for 10 years, but finally I am beginning to learn how to go backend and manipulate. Great fun and very useful.

    To day I followed your advise about limiting the number of archives, and I used “Method 3. Manually Limit Number of Archive Months in WordPress” – It works great.

    I just have one question: Is it possible to make the first letter a capital letter? I.e.: January not january, February not february and so on.

    • WPBeginner Support says

      Hi Stegemüller,

      By default, it should display month names starting with capital letter. If you are using WordPress in another language, then it is possible that localization files has month names in small letters. If you can confirm and report it, then translators would be able to fix that.

      As a temporary fix you can use CSS. Wrap your shortcode around a div

      <div class="capsfix">[wpb_custom_archives]</div>

      Then add this custom CSS:

      .capsfix li {
      text-transform:capitalize;
      }

      管理者

返信を残す

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