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 3.0のカスタム投稿タイプの使い方

バージョン2.9以降、WordPressはカスタム投稿タイプの機能を導入しました。バージョン3.0では、カスタム投稿タイプのパネルを作成するオプションが追加され、さらに進化しました。このチュートリアルでは、WordPressサイトにカスタム投稿タイプを実装する方法を紹介します。

カスタム投稿タイプの作成 – プラグインを使う

バージョン3.0の時点で、WordPressにはカスタム投稿タイプを作成するためのビルトインUI(ユーザーインターフェース)がありません。カスタム投稿タイプを作成するには、プラグインを使うか、テーマのfunctions.phpファイルにハードコードするかの2つのオプションしかありません。まず、プラグインを使ってカスタム投稿タイプを作成する方法を見てみましょう。

カスタム投稿タイプのUI

Custom Post Types UI

Custom Post Type UIWebDevStudiosのBrad Williamsによって開発されたプラグインで、カスタム投稿タイプとタクソノミーを簡単に作成することができる。このプラグインの最もクールな機能の一つは、カスタム投稿タイプを作成するコードをジェネレータで生成し、それをテーマのfunctions.phpファイルに貼り付けることができることです。このプラグインの癖の一つは、すべてのpost_type間でタクソノミーを共有できないことです。

Generate code for Custom Posts Types

カスタム投稿タイプのUIパネルから「新規追加」をクリックします。

Add New Button for Custom Post Type UI

次に、いくつかのオプションを入力します。投稿タイプ名」は、WordPressがその投稿タイプの投稿をすべてクエリーするために使用するものです。ラベル」は、通常の「投稿」メニューと同じように、ダッシュボードのサイドバーに表示されるものです。詳細オプションの表示」を展開すると、設定できるオプションがいくつか表示されます。ほとんどのオプションは “Public “や “Show UI “といった説明不要のものです。最初のものをtrueに設定するとカスタム投稿タイプメニューをサイドバーに表示することができ、もう一つのもの(show ui)をtrueに設定するとメニューパネルが生成されます。

“Rewrite “はカスタム投稿タイプがSEO Friendly WordPress URL(パーマリンク)を使用できるようにするものです。カスタマイザースラッグ “は好きなように設定することができます。WordPressはこのスラッグを使ってパーマリンクを生成します。例えば、example.comに“movies “というカスタムリライトスラッグを設定した場合、カスタム投稿タイプのパーマリンクはexample.com/moviesのようになります。

WordPressの「クエリーVar」機能を使えば、カスタム投稿タイプの投稿をクエリーすることができる。先ほどの例で、Moviespost_typeの下にあるMy First Movie Postというタイトルの投稿にアクセスするには、example.com/?movies=my-first-movie-postと入力します。クエリー変数はこのようになります:posttypename

最後に、サムネイル/おすすめ画像や抜粋など、カスタム投稿タイプでサポートされているさまざまな機能を選択することができます。

Custom Post Types UI Options

カスタム投稿タイプの作成 – Functions.phpファイルの使用

Hard Code Custom Post Types

プラグインなしでカスタム投稿タイプを使用したい場合は、テーマのfunctions.phpファイルに以下のコードを追加するだけです:

[cbk1]

コードを分解してみましょう。

register_post_type( $post_type, $args ): この関数は2つのパラメータ、$post_typeあるいは投稿タイプの名前と、引数の配列$argsを受け取ります。

ラベル:管理画面のサイドバーに表示される投稿タイプにつけられる複数形の名前。

public:true/false。この投稿タイプの投稿が管理UIに表示されることを許可する。

show_ui: true/false。この投稿タイプを管理する初期設定のUIを表示するか非表示にするか。

capability_type:初期設定: post 読み込み、編集、削除機能のチェックに使う投稿タイプ。

hierarchical: 投稿タイプが階層型かどうか。

rewrite: true/false。初期設定: true スラッグ引数が入力された場合、投稿の前にスラッグ名が付加される。

query_var: true/false 投稿タイプ名をクエリー変数として設定する。

supports: 初期設定: title と author 投稿タイプが許可する様々なサポート機能を設定する。

register_post_type() の詳細についてはWordPress Codex を参照。

カスタム投稿タイプの投稿を表示する

カスタム投稿タイプの投稿を表示するには、ループ内に以下のコードを追加します。name」は投稿タイプの名前に置き換えてください。注:カスタム投稿タイプをindex.phpファイルに追加する必要はありません。カスタムWordPressページを作成し、ループ内で以下のクエリーを実行することができます。

[cbk2]

複数の投稿タイプからの投稿を表示するには、上記のコードを次のように変更してください。ムービーはカスタム投稿タイプ名に変更してください。

[cbk3]

上記のコードでは、通常の投稿タイプ(post)とカスタム投稿タイプ(movies)の投稿がすべて表示されます。

以上です。このチュートリアルがお役に立てば幸いです。ご質問があればコメントするのをお忘れなく。

情報開示 私たちのコンテンツは読者支援型です。これは、あなたが私たちのリンクの一部をクリックした場合、私たちはコミッションを得ることができることを意味します。 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

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

  1. dave

    Ive created two custom post types but only the first one is showing on my page. ive added this
    $query = new WP_Query( array( ‘post_type’ => array( ‘post’, ‘featured’, ‘latest’ )
    ) );

    inside my loop file but nothing. how can i resolve this?

  2. Jeroen

    Do you know how i can add custom post types to the blog archive page, and rss feed ?

  3. wizdom networks

    Firstly, great article! I’m working through implementing custom post types via code. Noteworthy that the current WordPress documentation (http://codex.wordpress.org/Function_Reference/query_posts) specifies that the query_posts function should generally not be used and that the pre_get_posts hook is used to modify queries.

    “It is strongly recommended that you use the pre_get_posts filter instead, and alter the main query by checking is_main_query”

    Again, great article, thanks for sharing.

    Cheers.

  4. Thom Abbott

    WOW…for a novice who uses WP to run their website, this is not Beginner stuff by any means! I’ll probably have to reach out to some WP developer to set up my Custom Page that I want.

    • Editorial Staff

      Custom Post Types were created for developers to extend WordPress functionality. It was never meant for beginners.

      管理者

  5. mark shirley

    query_posts( ‘post_type=movies’);
    ?>

    I cant get this to work in my twentyeleven child theme page template where would i put it to pull a page of movies. Actually its the excerpts i really want. Thanks Mark

  6. muntzdesigns

    When using this plugin and adding the above code to loop.php you will lose control over categories…my custom post type is added to all categories…any help?

    • wpbeginner

      @muntzdesigns This is why you have multiple loops… your main blog loop, archive.php, category.php etc… On a custom designed site, you will have to utilize that in order for this to work appropriately.

      • muntzdesigns

        @wpbeginner@muntzdesigns So just add the above code to all files? (loop.php, archive.php and category.php). Thanks.

        • wpbeginner

          @muntzdesigns No. You only add the code on the front pages. The issue you had was that custom post types were being added to all categories… You need to add the above code only to the main loop. Then create a new loop for category archive, taxonomy archive, author archive …

  7. PriyaVerma

    Nice article thanks you so much

  8. dazuaga

    No, I was wrong.

  9. dazuaga

    Never mind: %category%/%type%/%postname% I think will do the trick

  10. dazuaga

    What exactly does Content Type Identifier in CMS Press plugin? I’m looking for a way to prepend a the category slug to the custom post type (example.com/category/post_type/postname) but when using %category%/%identifier%/%postname% in Content Type Identifier automaticaly transforms this way: %categoryidentifierpostname

  11. murphymakes

    excellent info as per usual. thanks

  12. simplywendz

    This is such a great tutorial for creating a custom post in wordpress. The steps are given on details one could easily follow. This is a big, big help!

  13. hoss9009

    I’ve been wondering this too!!

  14. Cupbearer

    Wow, I had the whole CMS Press thing working, but couldn’t figure out how to get it to show up in the posts. I guess it’s been around for so long that everyone just assumes that it should already be known. Perfect Answer to getting my Custom Post Type to show up in the Loop.

    Jerry Craig
    Cupbearer

  15. Noel Boaz

    Thsnks this helped me a lot to get my Custom post type on the front page. Definetly bookmarked, thanks.

  16. Ed

    I have tried the plugin, very impressive. However, I am trying to get a page attribute to appear from my themes ‘Pages’ attributes i.e the ability to use a full width page option. Am I to assume that your plugin only pulls attributes from Post type layouts rather than Page type layouts? All I want is to have the option of setting a post to a full, pre-determined template.

    Great job though – I shall be using this a lot.

    Cheers
    Ed

  17. Simon

    I have tried the plugin way but couldn’t get the post to show up. Maybe that’s because I coulndn’t find the loop.php file.

    Anyways, thanks a lot, I’m pretty sure it will work sometime soon.

    I want to say that one super awesome thing that would be great to do with wordpress is to have the possibility to create custom views of content just like in drupal with the views module.

    I know there is a plugin called pods and pods cms that is suppose to make that possible. Perhaphs you could ask the developper to help out in a post on wpbeginner if you do not understand it at all like me. I’m sure lots of people would love this. Presenting views is what realy makes a cms a realy dynamic cms after all.

    • Editorial Staff

      The loop.php might not exist in your theme. Loop can also be found in your index.php, archive.php, category.php etc. You may also create a custom loop if you like.

      管理者

  18. Peter

    Good tutorial, thank you for sharing.

    I have a question on this.
    I made a custom post type through functions.php (a calendar) which has a custom meta box in the admin UI. This meta box uses jQuery on one field.(http://jqueryui.com/demos/datepicker/).
    For this datepicker to work, I need to embed the required scripts.
    add_action(‘admin_enqueue_scripts’,’enqueue_my_scripts’);
    works but my scripts get loaded on the entire admin UI. (which interferes with the default WP admin jquery)
    So what I need is the hook for my custom post type.
    this:
    add_action(‘register_post_type’,’enqueue_my_scripts’);
    doesn’t work because register_post_type isn’t a hook.
    So, what is the hook for a custom post_type?

    • Amanda

      You could try adding the following to your function definition before registering the script:

      if(is_admin()) return;

      So something like:

      function some-function(){
      // we don't need this on admin pages, so...
      if(is_admin()) return;
      //register the custom script
      wp_enqueue_script( 'some-script' );
      }

  19. Evan

    How do I get each content type to show up in the loop? I mean, it’s almost like Tumblr. If I post a picture… I need to customize the code in the loop – same thing I post a link.

    • Editorial Staff

      You can see in the post, there is an option to show both a custom post types, and posts inside one loop…

      管理者

  20. Michael

    I cannot seem to get the loop to work to query posts of my custom type. Your example seems easy enough so I tried the following $var = query_posts( ‘post_type=sponsor’);

    Then I started a loop
    while ($var ->have_posts()) : $var ->the_post();
    Do stuff
    endwhile;

    this returns a php error:
    Fatal error: Call to a member function have_posts() on a non-object in single-sponsor.php on line 22

    line 22 is the line with the while loop.

    Ideas? What am I doing wrong?

  21. Romero

    I have tried to create a tag for custom field, and when I use get_the_tags within the loop of custom type, it doesn’t print anything.
    Any suggestions?

  22. Jan

    Im very excited about this new feature ^-^ You made a nice video. Altough in your example, why use custom post types and not just make up a category books and movies for the posts? That way you save all the hassle of a custom post type.

    • Editorial Staff

      First, it lets you organize things differently. Second, you can have a completely different write panel with different options. It allows for much more customizations…

      管理者

  23. Sandra

    Thanks for this great article! I’m a real wp beginner, and maybe you can help me with this question:
    Is it possible to display archives of one custom post type in the sidebar like one can do by using the “display archives by cat”-plugin by kwebble?

    • Editorial Staff

      Yes it is possible to display custom post types as archives. Just create a custom loop in the sidebar to display posts or categories from a specific post type.

      管理者

  24. lukeMV

    Any easy to understand guides on how to add options to the custom posts? For example, if a movie is drama, comedy, horror…. to have those options as check boxes WITHIN the custom post edit panel? I can’t seem to find a guide or plug-in for that.

  25. Paul

    Hi, That was a great article. I was hoping that you may know the answer to my question.
    In the last couple of lines you have:

    query_posts(array('post_type' => array('post', 'movies')));

    this returns all posts and post_types named movies.

    Do you know how to return both post_types named ‘movies’ and posts in the category ‘movies’ but exclude all other posts that do not have this category??

  26. Walter

    Can you go one step back: Why do I need custom post types in the first place? What’s the use-case?

    • Editorial Staff

      Custom Post Types are good for users who are using WordPress for more than a Blog, for example CMS. Lets say if you want to have a site that has your portfolio and your blog. Obviously you don’t want your blogs to look the same way as your portfolio does. That is when custom post types comes in handy. This is a very versatile feature for taking WordPress to the next level. For average blog user, they probably don’t need custom post types.

      管理者

      • Tracy B.

        Okay, but I’m still trying to understand the difference between this and just categorizing things. I’ve made plenty of sites for people where separate pages do things like list only the “current events” category and another the “projects” category or whatever. How is this better?

        • Editorial Staff

          This is better in terms of organization. You can have custom taxonomy, write panel etc for a specific post type.

  27. Chris

    I can’t help but think that this would have been more useful if you showed something out of the ordinary with post types, rather than use the old “movies and books” bit.

    How about a sideblog using post types, or something a bit more practical? All this does is re-state what the WP Codex will eventually have, if it isn’t already on there.

    • Editorial Staff

      We are sorry that you feel this way Chris. WP Codex will not have a video that will show you how to do this. It does not matter what names we use for the custom post types, the idea is how to add them. We will not create an extra-ordinary site just to write a post about custom post type. If you seek that knowledge, you are probably better off doing it yourself. This blog still has to fulfill the needs of the beginner level users hence why we shared the plugin method.

      管理者

  28. Kevin Elliott

    Will this work on WordPress 2.9 or is it only for 3.0?

    I tried using 3.0, but some quirk happened where none of my plugins would activate, even the ones that said they activated. Was very strange!

    -Kevin

    • Editorial Staff

      This would work with 3.0, so use it on 3.0 only. Try reinstalling WordPress 3.0, we are running a few sites on it.

      管理者

  29. Marc

    Just watched the video…

    Is it true that we need to edit php files (= hard code) to get the whole thing working?

    • Editorial Staff

      No, you have to do one or the other. In our video, we showed both ways. Either you can do the hard codes way, or the plugin way.

      管理者

      • Marc

        Allright, I see, thnx! ;)

        Then I suppose you started the hard code way? At the time seeing all the coding stuff only, I just quit. Therefore didn’t see the plugin way I guess…

  30. Rilwis

    Very nice article. The Custom Post Type UI plugin has the options very similar to raw PHP code of registering post types, that is great for developers.

    Thanks for introducing many useful plugin to work with custom post types.

  31. Bryan

    I’m happy that Wordpress has integrated custom post types right into the wp framework. However I’m not seeing how it is any better than using plugins already available such as Magic Fields or Flutter. With those plugins you get the same effect plus easy to setup custom fields with lots of different types and flexibility in how to enter and display your data. Does wp 3.0 support anything like that right out of the gate?

    • Editorial Staff

      You can create a UI for custom fields and just about anything in the backend with Custom Post Types. You are right that those plugins make it very easy for users, but if any of those plugins fall on development, then you are left with no choice.

      管理者

      • Bryan

        Fair enough. I’ve played around with the beta a bit, but not as much as I should I suppose. It would be exciting if it did go more that direction. Since I’ve become experienced using those plugins I almost don’t make a site now with one

        So that would be great if all those tools were available and handled within the original application. I can see it leading to better backend management and encourage more people to contribute to it rather than a smaller group supporting a particular plugin.

        • Ian

          I think they have intentionally left some of the custom post type code as code. To allow the average user to have to deal with it.

    • ravalde

      I cant get any taxonomies to display in twentyeleven neither can I find a tutorial that shows me how from start to finish all seem to focus on twentyten and the loop

返信を残す

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