Custom Post - yagisawatakuya/Wiki GitHub Wiki

カスタム投稿の追加

add_action('init', 'register_posttype_custom');
function register_posttype_article() {
  register_post_type('custom',
    array( 'label' => 'custom',
      'description' => '',
      'public' => true,
      'show_ui' => true,
      'show_in_menu' => true,
      'capability_type' => 'post',
      'hierarchical' => false,
      'has_archive' => true,
      'rewrite' => true,
      'query_var' => true,
      'exclude_from_search' => false,
      'supports' => array('title'),
      'menu_icon' => 'dashicons-images-alt',
      'labels' => array (
        'name' => 'Article',
        'singular_name' => 'Article',
        'menu_name' => 'Article',
        'add_new' => 'Articleを追加する',
        'add_new_item' => 'Articleを追加する',
        'edit' => '編集',
        'edit_item' => 'Articleを編集する',
        'new_item' => '新しいArticle',
        'view' => 'Articleを見る',
        'view_item' => 'Articleを見る',
        'search_items' => 'Articleを検索する',
        'not_found' => 'Articleがありません',
        'not_found_in_trash' => 'ゴミ箱の中は空です',
        'parent' => 'Parent',  
      )
    )
  );
}

カスタム投稿の記事がページ送りで反映されない場合の対応 原因:ホームでは通常のポスト数でページ数を判断しているため

add_action( 'pre_get_posts', 'home_posts_type' );
function home_posts_type( $wp_query ) {
  if ( ! is_admin() && $wp_query->is_main_query() && $wp_query->is_home() ) {
    $wp_query->set( 'post_type', array( 'カスタム投稿' ) );
  }
}