当サイト、Codex 日本語版は今後積極的な更新は行わない予定です。後継となる新ユーザーマニュアルは、https://ja.wordpress.org/support/ にあります。
万が一、当サイトで重大な問題を発見した際などは、フォーラムWordSlack #docs チャンネルでお知らせください。</p>

「投稿タイプ」の版間の差分

提供: WordPress Codex 日本語版
移動先: 案内検索
(ページ Custom Post Typesカスタム投稿タイプ へ移動: 和訳確定による改名。)
({{原文|Custom Post Types|93324}} <!-- 12:41, 29 September 2010 MichaelH 版 --> に更新)
1行目: 1行目:
{{NeedTrans|一部}}
 
 
WordPress はたくさんのタイプのコンテンツを保存して表示できます。内部ではこれらはすべて同じ場所、wp_posts テーブルに保存されてます。これらの異なるタイプのコンテンツは "post_type" というカラムによって区別されています。
 
WordPress はたくさんのタイプのコンテンツを保存して表示できます。内部ではこれらはすべて同じ場所、wp_posts テーブルに保存されてます。これらの異なるタイプのコンテンツは "post_type" というカラムによって区別されています。
  
50行目: 49行目:
 
  add_action( 'init', 'create_post_type' );
 
  add_action( 'init', 'create_post_type' );
 
  function create_post_type() {
 
  function create_post_type() {
   register_post_type( 'super',
+
   register_post_type( 'acme_product',
 
     array(
 
     array(
 
       'labels' => array(
 
       'labels' => array(
         'name' => __( 'Supers' ),
+
         'name' => __( 'Products' ),
         'singular_name' => __( 'Super' )
+
         'singular_name' => __( 'Product' )
 
       ),
 
       ),
       'public' => true,
+
       'public' => true
 
     )
 
     )
 
   );
 
   );
 
  }
 
  }
  
This creates a post type named "super". It has two major arguments with it. The first one is the "labels", which define the name of the type in both plural and singular forms. The second one is "public", which is a predefined flag to show the post type in the admin section and to make it show up on the main site itself, if it's queried for.
+
This creates a post type named "Product". It has two major arguments with it. The first one is the "labels", which define the name of the type in both plural and singular forms. The second one is "public", which is a predefined flag to show the post type in the admin section and to make it show up on the main site itself, if it's queried for.
  
 
There are many more parameters you can add to the [[関数リファレンス/register_post_type|register_post_type]] function, to do things like set up hierarchy, show the new post type in searches, change the URLs of the new posts, and to hide or show meta boxes in the post editing screen. These parameters are optional, and you can use them to configure your post type on a detailed level.
 
There are many more parameters you can add to the [[関数リファレンス/register_post_type|register_post_type]] function, to do things like set up hierarchy, show the new post type in searches, change the URLs of the new posts, and to hide or show meta boxes in the post editing screen. These parameters are optional, and you can use them to configure your post type on a detailed level.
 +
 +
=== Naming Best Practices ===
 +
While it's convenient to name your custom post type a simple name like "product" which is consistent with the core post types "post", "page", "revision", "attachment" and "nav_menu_item", it is better if you prefix your name with a short "''namespace''" that identifies your plugin, theme or website that implements the custom post type.
 +
 +
For example:
 +
 +
* "acme_product" or "aw_product" - Products post type used by hypothetical ACMEWidgets.com.
 +
* "eightfold_product" or "eft_product" - Products post type provided by the hypothetical "EightFold" Theme.
 +
* "ai1m_product" - Products post type provided by the hypothetical "All-in-One Merchant" Plugin.
 +
 +
Without namespacing of your custom post types your website's post types will more likely conflict with custom post types defined in a theme you fall in love with later or a plugin you realize that you absolutely need to use. Or if you are developing custom post types or themes there's a much greater chance your plugin or theme will conflict with custom post types defined in other plugins or themes and/or custom post types defined for your prospective user's website. Namespacing your custom post type won't guarantee against conflicts but will certainly minimize their likelihood.
 +
 +
Do pay close attention to not having your namespace exceed 20 characters though, as the post_type column in the DB is currently a varchar field of the latter length.
 +
 +
=== Reserved Post Type Names ===
 +
Although the core development team has yet to make a final decision on this, it has been proposed on the wp-hackers list that future core post type names will be namespaced with "'''wp_'''", i.e. if the core team decides to add an "Event" post type then according to this suggestion they would use the internal name "wp_event."  Even though this hasn't been finalized, it'll be a good idea to avoid any custom post types whose name begins with "wp_."
 +
 +
=== Function Reference ===
 +
{{Post Type Tags}}
  
 
<div id="Admin_UI">
 
<div id="Admin_UI">
75行目: 93行目:
 
</div>
 
</div>
  
The new post type will also get its own special section of the site layout. In the above example, posts of this new "super" type can be displayed at http://example.com/super/post-name. You can see this link appear in the edit pages for your new type, just like other posts. If
+
The new post type will also get its own special section of the site layout. In the above example, posts of this new "product" type can be displayed at http://example.com/product/%product_name% (where %product_name% is the URL-ized name of your product, i.e. http://example.com/product/foobrozinator.) You can see this link appear in the edit pages for your new type, just like other posts.  
 +
 
 +
==== URLs with Namespaced Custom Post Types ====
 +
 
 +
When you namespace a URL and still want to use a "clean" URL structure, you need to add the "rewrite" element to the array. For example, assuming the "ACME Widgets" example from above:
 +
 
 +
add_action( 'init', 'create_post_type' );
 +
function create_post_type() {
 +
  register_post_type( 'acme_product',
 +
    array(
 +
      'labels' => array(
 +
        'name' => __( 'Products' ),
 +
        'singular_name' => __( 'Product' )
 +
      ),
 +
      'public' => true,
 +
      'rewrite' => array('slug' => 'products')
 +
    )
 +
  );
 +
}
 +
The above will result in a URL like http://example.com/products/%product_name% (see description of %product_name% above.) Note that we used a plural form here which is a format that some people prefer because it implies a more logical URL for a page that embeds a list of products, i.e. http://example.com/products/. 
 +
 
 +
Also note using a generic name like "products" here can potentially conflict with other plugins or themes that use the same name but most people would dislike longer and more obscure URLs like http://example.com/acme_products/foobrozinator and resolving the URL conflict between two plugins is easier simply because the URL structure is not stored persistently in each post record in the database in the same way custom post type names are stored.
  
 
<div id="Single_template">
 
<div id="Single_template">
81行目: 120行目:
 
</div>
 
</div>
  
The theme system supports post types too, in the form of the single-type template. In the same way that posts are shown on their own page with single.php, custom post types will use single-type.php if it's available. So for the above example, you could create a single-super.php file and the super posts would be shown using that template.  
+
The theme system supports post types too, in the form of the single-type template. In the same way that posts are shown on their own page with single.php, custom post types will use single-type.php if it's available. So for the above example, you could create a single-acme_product.php file and the product posts would be shown using that template.
  
 
<div id="Querying_by_post_type">
 
<div id="Querying_by_post_type">
90行目: 129行目:
  
 
Example:
 
Example:
  $loop = new WP_Query( array( 'post_type' => 'super', 'posts_per_page' => 10 ) );
+
  $loop = new WP_Query( array( 'post_type' => 'product', 'posts_per_page' => 10 ) );
 
  while ( $loop->have_posts() ) : $loop->the_post();
 
  while ( $loop->have_posts() ) : $loop->the_post();
 
   the_title();
 
   the_title();
98行目: 137行目:
 
  endwhile;
 
  endwhile;
  
This simply loops through the latest 10 super posts and displays the title and content of them.
+
This simply loops through the latest 10 product posts and displays the title and content of them.
  
 
<div id="More_Information">
 
<div id="More_Information">
108行目: 147行目:
 
* [http://www.h-fj.com/blog/archives/2010/06/15-172024.php WordPress 3.0のカスタム投稿タイプ機能(その2)]
 
* [http://www.h-fj.com/blog/archives/2010/06/15-172024.php WordPress 3.0のカスタム投稿タイプ機能(その2)]
 
* [http://www.odysseygate.com/archives/1624 カスタム投稿タイプ(Custom Post Type)の導入と使い方 (WordPress 3.0)]
 
* [http://www.odysseygate.com/archives/1624 カスタム投稿タイプ(Custom Post Type)の導入と使い方 (WordPress 3.0)]
 +
* [http://ounziw.com/2010/10/09/custom-post-dashboard/ カスタム投稿タイプの投稿数をダッシュボードに表示する ]
  
 
=== 英語情報 ===
 
=== 英語情報 ===
115行目: 155行目:
 
* [http://kovshenin.com/archives/extending-custom-post-types-in-wordpress-3-0/ Extending Custom Post Types in WordPress 3.0]
 
* [http://kovshenin.com/archives/extending-custom-post-types-in-wordpress-3-0/ Extending Custom Post Types in WordPress 3.0]
  
[[Category:wp3.0]]
+
{{原文|Custom Post Types|93324}} <!-- 12:41, 29 September 2010 MichaelH 版 -->
 
+
{{原文|Custom Post Types|90220}} <!-- 2010-06-17T13:10:39 Otto42 版 -->
+
  
 
[[en:Custom_Post_Types]]
 
[[en:Custom_Post_Types]]
 +
[[Category:wp3.0]]
 +
[[Category:上級トピック]]

2010年10月13日 (水) 13:38時点における版

WordPress はたくさんのタイプのコンテンツを保存して表示できます。内部ではこれらはすべて同じ場所、wp_posts テーブルに保存されてます。これらの異なるタイプのコンテンツは "post_type" というカラムによって区別されています。

WordPress 3.0 以降では、任意の投稿コンテンツを追加したり使用できる機能があります。

WordPress には5つの主要なデフォルト投稿タイプがあります。

投稿 (post)

WordPress での投稿 (post) は、ブログで使われるメインの投稿タイプです。投稿は通常、一番新しいものが最初にくる反時系列順で表示されます。投稿はフィードの作成にも使われます。

ページ (page)

WordPress でのページ (page) は投稿に似ていますが、投稿の時系列構造の外にあります。ページはメインのサイトドメインのすぐ下にくる独自 URL を持っており、特別なページテンプレートを使って表示することができます。また、他のページを親に持つ階層化した構造にすることもできます。

添付ファイル (attachment)

添付ファイル (attachment) は特別な投稿で、メディアアップロードシステムを使ってアップロードされたファイルについて、ファイル名・説明などすべての情報が含まれます。例えばファイルが画像の場合、画像のサイズや自動生成されたサムネイル、ファイルの位置、埋込み EXIF 情報などです。

リビジョン (revision)

リビジョン (revision) は下書きや既存の投稿・ページの過去の変更履歴を保存するための投稿タイプです。投稿・ページタイプと基本的には同じような内容ですが、元となる投稿・ページが親となります。

ナビゲーションメニュー (nav_menu) はナビゲーションメニューシステムの各メニュー項目を保存するための投稿タイプです。posts テーブルの項目がサイト内に表示出来るコンテンツ以外にも使えるという最初の例と言えるでしょう。

カスタム投稿タイプ

WordPress にカスタム投稿タイプを追加するには、register_post_type 関数を使って独自の投稿タイプとその動作を定義します。

以下はカスタム投稿タイプ追加の基本的な一例です。

add_action( 'init', 'create_post_type' );
function create_post_type() {
  register_post_type( 'acme_product',
    array(
      'labels' => array(
        'name' => __( 'Products' ),
        'singular_name' => __( 'Product' )
      ),
      'public' => true
    )
  );
}

This creates a post type named "Product". It has two major arguments with it. The first one is the "labels", which define the name of the type in both plural and singular forms. The second one is "public", which is a predefined flag to show the post type in the admin section and to make it show up on the main site itself, if it's queried for.

There are many more parameters you can add to the register_post_type function, to do things like set up hierarchy, show the new post type in searches, change the URLs of the new posts, and to hide or show meta boxes in the post editing screen. These parameters are optional, and you can use them to configure your post type on a detailed level.

Naming Best Practices

While it's convenient to name your custom post type a simple name like "product" which is consistent with the core post types "post", "page", "revision", "attachment" and "nav_menu_item", it is better if you prefix your name with a short "namespace" that identifies your plugin, theme or website that implements the custom post type.

For example:

  • "acme_product" or "aw_product" - Products post type used by hypothetical ACMEWidgets.com.
  • "eightfold_product" or "eft_product" - Products post type provided by the hypothetical "EightFold" Theme.
  • "ai1m_product" - Products post type provided by the hypothetical "All-in-One Merchant" Plugin.

Without namespacing of your custom post types your website's post types will more likely conflict with custom post types defined in a theme you fall in love with later or a plugin you realize that you absolutely need to use. Or if you are developing custom post types or themes there's a much greater chance your plugin or theme will conflict with custom post types defined in other plugins or themes and/or custom post types defined for your prospective user's website. Namespacing your custom post type won't guarantee against conflicts but will certainly minimize their likelihood.

Do pay close attention to not having your namespace exceed 20 characters though, as the post_type column in the DB is currently a varchar field of the latter length.

Reserved Post Type Names

Although the core development team has yet to make a final decision on this, it has been proposed on the wp-hackers list that future core post type names will be namespaced with "wp_", i.e. if the core team decides to add an "Event" post type then according to this suggestion they would use the internal name "wp_event." Even though this hasn't been finalized, it'll be a good idea to avoid any custom post types whose name begins with "wp_."

Function Reference

投稿タイプ: register_post_type(), add_post_type_support(), remove_post_type_support(), post_type_supports(), post_type_exists(), set_post_type(), get_post_type(), get_post_types(), get_post_type_object(), get_post_type_capabilities(), get_post_type_labels(), is_post_type_hierarchical(), is_post_type_archive(), post_type_archive_title()


管理画面

When a post type is created like this, it gets a new top level entry in the Admin section to create posts of that new type. From there, you'll have a full post editor and everything that comes along with it by default.

URL

The new post type will also get its own special section of the site layout. In the above example, posts of this new "product" type can be displayed at http://example.com/product/%product_name% (where %product_name% is the URL-ized name of your product, i.e. http://example.com/product/foobrozinator.) You can see this link appear in the edit pages for your new type, just like other posts.

URLs with Namespaced Custom Post Types

When you namespace a URL and still want to use a "clean" URL structure, you need to add the "rewrite" element to the array. For example, assuming the "ACME Widgets" example from above:

add_action( 'init', 'create_post_type' );
function create_post_type() {
  register_post_type( 'acme_product',
    array(
      'labels' => array(
        'name' => __( 'Products' ),
        'singular_name' => __( 'Product' )
      ),
      'public' => true,
      'rewrite' => array('slug' => 'products')
    )
  );
}

The above will result in a URL like http://example.com/products/%product_name% (see description of %product_name% above.) Note that we used a plural form here which is a format that some people prefer because it implies a more logical URL for a page that embeds a list of products, i.e. http://example.com/products/.

Also note using a generic name like "products" here can potentially conflict with other plugins or themes that use the same name but most people would dislike longer and more obscure URLs like http://example.com/acme_products/foobrozinator and resolving the URL conflict between two plugins is easier simply because the URL structure is not stored persistently in each post record in the database in the same way custom post type names are stored.

シングルテンプレート

The theme system supports post types too, in the form of the single-type template. In the same way that posts are shown on their own page with single.php, custom post types will use single-type.php if it's available. So for the above example, you could create a single-acme_product.php file and the product posts would be shown using that template.

投稿タイプによるクエリ

In the rest of the theme system, you can also create new queries to display posts from a specific post type. This is done via the "post_type" parameter to a WP_Query.

Example:

$loop = new WP_Query( array( 'post_type' => 'product', 'posts_per_page' => 10 ) );
while ( $loop->have_posts() ) : $loop->the_post();
  the_title();
  echo '<div class="entry-content">';
  the_content();
  echo '</div>';
endwhile;

This simply loops through the latest 10 product posts and displays the title and content of them.

追加情報

日本語情報

英語情報

最新英語版: WordPress Codex » Custom Post Types最新版との差分