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

カテゴリー:WordPressで最新の先頭固定投稿を表示する方法

WordPressには、先頭固定表示というとてもクールな機能がある。先頭固定表示は、ブログのおすすめ投稿のようなものです。投稿を先頭固定表示にすると、新着投稿の上に表示されますが、テーマが許可している場合に限ります。このチュートリアルでは、WordPressで先頭固定表示をする方法を紹介します。

Latest Sticky Posts Demo

注:これは中級レベルのチュートリアルであり、基本的なHTML / CSSの知識とWordPressテーマの知識が必要です。

動画チュートリアル

Subscribe to WPBeginner

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

まず最初に、テーマのfunctions.phpファイルまたはサイト固有のプラグインに、このコード・スニペットをコピー&ペーストしてください。

[cbk1]

上記のコードは、WordPressのデータベースにクエリーして、最新の5つの先頭固定表示投稿を取得します。そして、各固定投稿のタイトルとリンクを投稿フォーマットで表示します。すべてを関数でラップし、ショートコードを作成しました。

このショートコード[latest_stickies]をWordPressの投稿やページ、あるいはテキストウィジェットに使用することで、最新の先頭固定表示機能を実現できる。

テキストウィジェットの中でショートコードを使用したい場合は、テーマのfunctions.phpまたはサイト固有のプラグインにこのコードを追加する必要があります。

[cbk2]

このスニペットと関数は、特集スライダー、またはサイトに表示したいその他の高度な機能で使用することができます。このスニペットは、カスタマイザーホームページやマガジンスタイルの外観を持つWordPressサイト向けのものです。

以上、この投稿があなたのWordPressブログに最新の先頭固定表示を表示するのに役立てば幸いです。また、WordPressで先頭固定表示に有効期限を追加する方法についてのチュートリアルもご覧いただきたい。

この投稿が気に入ったなら、WordPressの動画チュートリアルをYouTubeチャンネルに登録してください。Twitterや Google+でもご覧いただけます。

Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us. Here's our editorial process.

アバター

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.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit - a collection of WordPress related products and resources that every professional should have!

Reader Interactions

8件のコメントLeave a Reply

  1. umair

    very informative efforts made by you, this code works perfectly fine,
    i have a question that i have multiple categories & sub-categories, so i just want to make a sticky post to each category separately, and displayed on category list page where all posts of that specific category listed, like (Sports category->one sticky post – cooking recipes category -> one sticky post) etc

  2. Dom

    Thanks for getting me on the right track!!
    The code works great in getting the Title to show up where the short code is pasted.

    However, I was trying to find a method to display the entire post (all content, in stead of just a title or even excerpt) on a specific page.

    I decided to play with your code above in order to try and achieve this and finally managed…
    Although the title displays the same size as paragraph text, I’m sure I can fix that.

    Here’s what I changed…please let me know if you think there’s something I should reconsider lest the whole world explodes ;)

    I simply changed your get_the_excerpt() text to get_the_content() and this successfully pulled in the entire blog post into that page.
    In order to remove the bulleted indentation I then removed the ” ” ” ” tags, but had to leave the apostrophes in place, otherwise the page wouldn’t load.
    So in essence, where ” or ” was before, now there is only ” and so on.

    That’s all I changed and it mostly works like a charm for me…
    There are 2 issues I noticed:
    1: Sometimes it loads most of the post, but not always right down to the end, especially if the post is edited afterwards…anyone with suggestions to ensure this doesn’t happen?
    2: I’d love if the featured image would also show up, this would make it the ultimate flexible solution! Any thoughts here?

    Thanks again for the advice!

    Hope what I explained makes sense to anyone who wants to achieve the same as I was after.

  3. Achi Baten

    How can i show homepage post like you?

  4. richard

    Hello

    Thank you for your code. However i try to achieve something and i have 2 issues.

    I want to display only one sticky post on my home page and only the youtube video present on the content.

    ACtually i display the title and all the content of a post (include texts, pictures and videos).
    Second problem is that it display youtube as a link and not embed directly the video into my homepage.

    Thank you for your help

  5. Raviraj

    Hi,

    I have tried out with the code, It works fine when at least 1 post has been assigned as sticky post else when none of the posts are assigned a sticky, then it will display all latest 5 posts.

    So would be the condition to display only sticky posts, if there are no sticky posts assigned then It should not display anything.

    Thanks in advance

  6. AnastisSourgoutsidis

    ‘caller_get_posts’ has been replaced by ‘ignore_sticky_posts’ since v3.1 so I think its important to change your code to reflect that.
    Also, a) $post->ID is not needed in this instance of get_permalink() as you’ve setup the post data by calling $the_query->the_post(), and if for whatever reason you still want to include $post->ID, you should be global-ing it, i.e. global $post;

Leave A Reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.