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

投稿タイプ

提供: WordPress Codex 日本語版
2010年6月18日 (金) 04:01時点におけるNao (トーク | 投稿記録)による版 (Otto42の90220版より新規作成。)

(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
移動先: 案内検索

このページ「投稿タイプ」は未翻訳です。和訳や日本語情報を加筆してくださる協力者を求めています

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

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

Default Types

There are five major types that WordPress uses by default.

Post

A "post" in WordPress is the main type used by the blog. Posts are normally displayed in the blog in reverse sequential order by time (newest posts first). Posts are also used for creating the feeds.

Page

A "page" in WordPress is like a post, but it lives outside the normal time based structure of posts. They have their own URLs directly off the main site URL. They can also use special Page Templates to display them. Pages can also be organized in a hierarchical structure, with Pages being parents to other Pages.

Attachment

An "attachment" is a special post that holds information about files uploaded through the Media upload system. They hold all the description and name and other information about uploaded files. For images, this is also linked to metadata information about the size of the images and thumbnails generated from the images, the location of the files, and even information obtained from EXIF data embedded in the images.

Revisions

A "revision" is used to hold draft posts as well as any past revisions of existing posts or pages. These are basically identical to the main post/page that they are for, but have that post/page as their parent.

Nav Menus

The "nav_menu" type holds information about a single item in the new Navigation Menu system. These are the first examples of entries in the posts table to be used for something other than an otherwise displayable content on the blog.

Custom Types

Adding a custom type to WordPress is done via the register_post_type function. This function allows you to define the post type and how it operates within WordPress.

Here's a basic example of adding a custom post type:

add_action( 'init', 'create_post_type' );
function create_post_type() {
  register_post_type( 'super',
    array(
      'labels' => array(
        'name' => __( 'Supers' ),
        'singular_name' => __( 'Super' )
      ),
      '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.

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.

Admin UI

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.

URLs

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

Single 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-super.php file and the super posts would be shown using that template.

Querying by post type

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' => 'super', '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 super posts and displays the title and content of them.

More Information

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