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 exclude latest post from the WordPress post loop

なぜWordPressの投稿ループから最新投稿を除外するのか?

初期設定では、WordPressはブログの投稿を時系列順に表示します。しかし、カスタマイザーホームページや WordPressアーカイブページで最新の投稿がどのように表示されるかを変更したい場合もあるでしょう。

WordPressの投稿ループから最新投稿を除外することで、そのコンテンツを別の場所に表示したり、独自のカスタムスタイルを追加することもできます。

それでは、WordPressの投稿ループから除外することで、最新のブログ投稿を非表示にする方法を見ていきましょう。以下のクイックリンクから、使いたい方法にジャンプしてください:

投稿ループから最新の投稿を除外する最も簡単な方法は、WordPressサイトにコードを追加することです。

テーマファイルを手動で編集するように指示するガイドもありますが、WordPressの一般的なエラーを引き起こし、サイトを完全に壊してしまう可能性もあります。

そのため、WPCodeの使用をお勧めします。サイトを危険にさらすことなく、WordPressにカスタムコードを簡単に追加できる最高のコードスニペットプラグインです。

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

プラグインを有効化したら、Code Snippets ” Add Snippetに進みます。

Adding custom code snippets to WordPress

ここでは、サイトに追加できるすべてのスニペットが表示されます。これらには、コメントを完全に無効化するスニペット、WordPressが通常サポートしていないファイルタイプのアップロード、添付ファイルページの無効化などが含まれます。

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

Adding a custom code snippet to your WordPress website using WPCode

次の画面で、コード・スニペットのタイトルを入力する。これは参考用なので、好きなものを使ってください。

次に、’Code Type’ドロップダウンを開き、’PHP Snippet’を選択します。

Excluding latest blog from the WordPress post loop

そうしたら、コードエディターに以下を貼り付けるだけだ:

<?php
function wpsites_exclude_latest_post( $query ) {
	if ( $query->is_home() && $query->is_main_query() ) {
		$query->set( 'offset', '1' );
	}
}

add_action( 'pre_get_posts', 'wpsites_exclude_latest_post', 1 );

その後、ページを下にスクロールし、「インサーター」セクションまで進む。

すでに選択されていなければ、「自動インサート」と「どこでも実行」を選択する。

Automatically inserting code into a WordPress website

最後に画面を一番上までスクロールし、「Inactive」スライダーをクリックして「Active」と表示させる。

その後、「スニペットを保存」または「更新」ボタンをクリックするだけで、コード・スニペットが有効になります。

Excluding latest blog from the WordPress post loop using WPCode

これで、WordPressのサイトにアクセスすると、最新のブログ投稿が非表示になる。

方法2:WordPressテーマファイルを編集する(プラグイン必須なし)

注意事項 ブロックテーマはテーマファイルのセットが異なるため、このチュートリアルはクラシックWordPressテーマでのみ動作します。ブロックテーマを使用している場合は、最初の方法を使用することをお勧めします。

また、この方法でエラーを防ぐには、まずサイトをバックアップするか、ステージング環境を使用することをお勧めします。

WordPressの投稿ループから最新投稿を除外するもう一つの方法は、WordPressテーマファイルにコードを追加することです。

これは上記のコードと同様の結果を実現するが、表示させたいWordPressのループに直接追加する必要がある。

例えば、サイト全体の投稿ループを変更したい場合、index.phpファイルにコードを追加する必要があります。

ただ、WordPressファイルに直接コードを追加すると、WordPressブログのページ送りに問題が生じる可能性があることに注意してください。

まず、WordPressホスティングサービスのファイルマネージャーを開くか、FTPクライアントでサイトに接続する必要があります。

このチュートリアルでは、プロセスがFTPを使用するよりもはるかに簡単なので、Bluehostのファイルマネージャを使用します。とはいえ、FTPを使っても、別のホスティングサービスを使っても、手順は多かれ少なかれ同じです。

あなたがBluehostユーザーであれば、ホスティングアカウントのダッシュボードにログインし、’ウェブサイト’タブに移動します。次に、編集したいサイトの「設定」をクリックします。

Bluehost site settings

次に、クイックリンクのセクションに行く。

次に、 “ファイルマネージャ “ボタンをクリックします。必要であれば、Bluehostはまた、あなたのサイトのルートディレクトリを表示します。

Accessing a website's file manager in Bluehost

ファイルマネージャーに入ったら、編集したいファイルを探します。例えば、index.phpにコードを追加したい場合、現在のテーマのフォルダー(public_html/wp-contentの中)にあります。

index.phpファイルを右クリックし、「編集」をクリックします。

Opening the index.php file in Bluehost file manager

次のコードをコピーして、WordPressのループに追加してください:

query_posts('posts_per_page=6&offset=1');

このコードは、最新の投稿に続く5つの投稿だけを表示するようにループに指示します。

WordPressのループの直上にコードを追加する必要があります:

<?php
query_posts('posts_per_page=6&offset=1');
if ( have_posts() ) {

	// Load posts loop.
	while ( have_posts() ) {
		the_post();

		get_template_part( 'template-parts/content/content', get_theme_mod( 'display_excerpt_or_full_post', 'excerpt' ) );
	}

	// Previous/next page navigation.
	twenty_twenty_one_the_posts_navigation();

} else {

	// If no content, include the "No posts found" template.
	get_template_part( 'template-parts/content/content-none' );

}

Bluehostのファイルマネージャーで私たちのコードがどのように見えるかを示します:

Adding code to exclude the latest post in Bluehost file manager

コードを追加したら、「変更を保存」ボタンをクリックするだけです。

次に、WordPressサイトをプレビューします。

この投稿が、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

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

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

    Hey guys, thanks for this article! I want to implement this, but whenever I try I’ve got an strange problem: entries would look like exactly like the frontpage, showing a list of recent post rather than the post itself. My code is slightly different, since is a SMTheme. Any help will be much appreciated. PS: Sorry for posting twice, but the code has printed wrong. Here you go:

  3. space says

    Solution is work fine, Thanks. but pagination broke after applying this code, I have tried but not able to work with correct pagination. Any solution to pagination ? .

    • Rickie Anand says

      apply class to the li as per the number of page and a counter & give style to p1c1 as display:none;

  4. Sabi Maharjan says

    I have used two plugins. One is “Recent facebook post” to show recent posts in wprdpress and “Facebook publish page” to publish my wordpress posts in facebook . Now I want exclude the recent post of the facebook that was already posted from my wordpress posts .

    How can I do it ?

    • WPBeginner Support says

      For that your plugin posting content from your Facebook page should have a check to see if a post was already published from WordPress to Facebook. We do not recommend users to directly edit plugin files. However, if you are comfortable editing php files then you can fork the plugin as a new plugin and then add the code to accomplish this.

      管理者

  5. Jim says

    Hmm, when i tried this code on a category page loop, it couldn’t keep the posts for only that category, instead it acted as if it was the front page loop and displayed all posts.

  6. PabloCovarrubias says

    hello, i just have one question, when i do this, pagination doesen0t work anymore, how can i fix this?

    thanks, other than that, it works perfectly!!

    cheers!

    • space says

      Yes, pagination not working. you have found any working solution for pagination, I am still searching .. :(

        • de hams says

          pagination do not work on index page most so we can make out own ajax pagination call and mostly query_posts() works for pagination rather then WP_Query()

  7. Wordpwess says

    Is there a way to offset a post from one cat?

    E.g I want to show all posts apart from the LATEST post of category x

  8. Khurram says

    “query_posts(‘posts_per_page=6&offset=1’);” this is not worked for me , while i am using the WP-PageNavi plugin for page navigation. :(

  9. Ken says

    Thanks for the info. Worked well :) Though, I’m encountering another issue for another site I’m currently doing. Is there a way to exclude from the loop only the latest post of a particular category?

    Thing is, I’ve a blog that publishes podcasts. The front page highlights the most recent podcast – posts are published under category: Podcast – via its own styling. At the same time, the front page also shows the last 5 most recent posts, of which may include posts under the category “podcast”. I just don’t want to show in the loop the most recent post published under “Podcast” simply because it’s already featured with it’s own styling. Hope you can help me out. thanks!

  10. Joffrey says

    Thanks for the tip!
    But I was wondering if there is also a way to exclude the post that is currently showed on screen.

    If yes, can you tell me how to do this?

    Thanks in advance!

      • Joffrey says

        One of my pages displays first of all the latest or current post the user is viewing.

        The current post is called by the loop.

        Below this post I want to display the excerpts of other previous posts without the current post displayed in this list.

        The previous post-excerpts are called by query_posts:

        query(‘showposts=3&cat=5’);
        while($previousPosts->have_posts()) : $previousPosts->the_post();
        ?>

        As you can see I call 3 posts(excerpts) from category 5 which displays three posts from category News. This seems ok but within these 3 post-excerpts, the current post which is called by the loop is also in this list.
        Do you know a way to exclude the current post within this list?

        Thanks again!

        • Joffrey says

          Thanks again for the quick response!
          However, I did try the offset=1 in the query, only thing is that when you press an older post, you get to see the older post in the main loop, within the list excerpts the latest post is excluded, but the older post which is now the current post is still in the excerpts list.

          Any idea how to exclude not the latest but the current post?

          Thanks so far already :)

  11. Bilal Ahmed says

    Thank you so much for this piece as i was looking for a way to exclude 4 latest posts form my home page and i got answer by this articles and i have replaced (query_posts(‘posts_per_page=6&offset=1’);) by query_posts(‘posts_per_page=6&offset=3’); and its done :)

    • Editorial Staff says

      That is a multi-step process. First you would need to have a custom page template. Second you would need to create that custom page, your front page. Then you would run a query on that custom template showing only sticky posts.

      管理者

返信を残す

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