WordPressでカスタム投稿タイプを簡単に作成する方法を学びたいですか?
カスタム投稿タイプを使用すると、投稿やページにとどまらず、サイトにさまざまなコンテンツタイプを作成できます。WordPress サイトをブログプラットフォームから強力なコンテンツ管理システム(CMS) に変身させます。
この投稿では、WordPressでカスタム投稿タイプを簡単に作成する方法を紹介する。
WordPressのカスタム投稿タイプとは?
WordPressサイトでは、投稿タイプはWordPressの異なるコンテンツタイプを区別するために使用されます。投稿とページはどちらも投稿タイプですが、異なる目的で作られています。
WordPressには初期設定でいくつかの投稿タイプが用意されています:
- 投稿
- ページ
- 添付ファイル
- リビジョン
- ナビメニュー
また、カスタム投稿タイプと呼ばれる独自の投稿タイプを作成することもできます。これらは標準の投稿やページとは異なるフォーマットのコンテンツを作成する際に便利です。
例えば、映画レビューのサイトを運営しているのであれば、映画レビューの投稿タイプを作成したいと思うでしょう。また、ポートフォリオ、お客様の声、製品などのカスタム投稿タイプを作成することもできます。
WPBeginnerでは、お得な情報や用語集のセクションにカスタム投稿タイプを使用し、日々のブログ記事とは切り離しています。サイトのコンテンツをよりよく整理するのに役立っています。
カスタム投稿タイプは異なるカスタムフィールドと独自のカスタムカテゴリー構造を持つことができます。
WordPressサイトのデータを保存するために、多くの人気のあるWordPressプラグインがカスタム投稿タイプを使用しています。以下はカスタム投稿タイプを使用するトッププラグインです:
- WooCommerceはオンラインストアに’product’投稿タイプを追加します。
- WPFormsは、すべてのフォームを保存するための’wpforms’投稿タイプを作成します。
- MemberPressは‘memberpressproduct’カスタム投稿タイプを追加します。
動画チュートリアル
文章での説明がお望みなら、このまま読み進めてください。
カスタム投稿タイプを作成する必要がありますか?
WordPressサイトでカスタム投稿タイプの作成を始める前に、ニーズを評価することが重要です。多くの場合、通常の投稿やページで同じ結果を得ることができます。
あなたのサイトにカスタム投稿タイプが必要かどうかわからない場合は、WordPressでカスタム投稿タイプやタクソノミーが必要な場合についてのガイドを参照してください。
それでは、WordPressでカスタム投稿タイプを簡単に作成する方法を見ていきましょう。
ここでは2つの方法を紹介し、WordPressサイトでカスタム投稿タイプを表示する方法もいくつか取り上げます。
WPCodeを使ってカスタム投稿タイプを手動で作成する
カスタム投稿タイプを作成するには、テーマのfunctions.phpファイルにコードを追加する必要があります。ちょっとしたミスでもサイトを壊してしまう可能性があるため、通常、上級ユーザー以外にはお勧めしません。また、テーマを更新するとコードが消えてしまいます。
WPCodeは、WordPressサイトにカスタマイザーコードを追加する最も簡単で安全な方法です。
WPCodeを使えば、カスタム・スニペットを追加したり、ビルトインで設定済みのコードライブラリから多くの機能を有効化したりすることができ、インストール済みの専用プラグインや個別プラグインを置き換えることができます。
まず、無料のWPCodeプラグインをインストールし、有効化する必要があります。詳しい手順については、WordPressプラグインのインストール方法のステップバイステップガイドをご覧ください。
有効化したら、WordPressダッシュボードのCode Snippets “ Add Snippetに移動します。カスタムコードを追加(新規スニペット)」にマウスを合わせ、「スニペットを使用」をクリックします。
次に、「カスタマイザーの作成」画面が表示されます。
これで、コード・スニペットにタイトルをつけ、スイッチを「有効化」に切り替えることができる。
その後、以下のコードを「コードプレビュー」エリアに貼り付けるだけです。このコードは、管理サイドバーに表示される「ムービー」という基本的なカスタム投稿タイプを作成し、どのテーマでも動作します。
// Our custom post type function
function create_posttype() {
register_post_type( 'movies',
// CPT Options
array(
'labels' => array(
'name' => __( 'Movies' ),
'singular_name' => __( 'Movie' )
),
'public' => true,
'has_archive' => true,
'rewrite' => array('slug' => 'movies'),
'show_in_rest' => true,
)
);
}
// Hooking up our function to theme setup
add_action( 'init', 'create_posttype' );
基本的なカスタム投稿タイプが欲しいだけなら、moviesと
Moviesを
自分のCPTスラッグと名前で置き換えて、「更新」ボタンをクリックするだけです。
しかし、カスタム投稿タイプのオプションをさらに増やしたい場合は、上記のコードの代わりに以下のコードを使用してください。
以下のコードでは、リビジョン、アイキャッチ画像、カスタムフィールドのサポート、カスタム投稿タイプと「ジャンル」というカスタムタクソノミーの関連付けなど、「映画」カスタム投稿タイプにさらに多くのオプションを追加しています。
注: この2つのスニペットを組み合わせないでください。WordPressがエラーを出します。どちらのスニペットも同じカスタム投稿タイプを登録するからです。登録したい投稿タイプを追加するごとに、WPCodeを使って新規スニペットを作成することをお勧めします。
/*
* Creating a function to create our CPT
*/
function custom_post_type() {
// Set UI labels for Custom Post Type
$labels = array(
'name' => _x( 'Movies', 'Post Type General Name', 'twentytwentyone' ),
'singular_name' => _x( 'Movie', 'Post Type Singular Name', 'twentytwentyone' ),
'menu_name' => __( 'Movies', 'twentytwentyone' ),
'parent_item_colon' => __( 'Parent Movie', 'twentytwentyone' ),
'all_items' => __( 'All Movies', 'twentytwentyone' ),
'view_item' => __( 'View Movie', 'twentytwentyone' ),
'add_new_item' => __( 'Add New Movie', 'twentytwentyone' ),
'add_new' => __( 'Add New', 'twentytwentyone' ),
'edit_item' => __( 'Edit Movie', 'twentytwentyone' ),
'update_item' => __( 'Update Movie', 'twentytwentyone' ),
'search_items' => __( 'Search Movie', 'twentytwentyone' ),
'not_found' => __( 'Not Found', 'twentytwentyone' ),
'not_found_in_trash' => __( 'Not found in Trash', 'twentytwentyone' ),
);
// Set other options for Custom Post Type
$args = array(
'label' => __( 'movies', 'twentytwentyone' ),
'description' => __( 'Movie news and reviews', 'twentytwentyone' ),
'labels' => $labels,
// Features this CPT supports in Post Editor
'supports' => array( 'title', 'editor', 'excerpt', 'author', 'thumbnail', 'comments', 'revisions', 'custom-fields', ),
// You can associate this CPT with a taxonomy or custom taxonomy.
'taxonomies' => array( 'genres' ),
/* A hierarchical CPT is like Pages and can have
* Parent and child items. A non-hierarchical CPT
* is like Posts.
*/
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'capability_type' => 'post',
'show_in_rest' => true,
);
// Registering your Custom Post Type
register_post_type( 'movies', $args );
}
/* Hook into the 'init' action so that the function
* Containing our post type registration is not
* unnecessarily executed.
*/
add_action( 'init', 'custom_post_type', 0 );
また、階層化の値をfalseに
設定している部分にもお気づきでしょう。カスタム投稿タイプを投稿ではなくページのように動作させたい場合は、この値をtrueに
設定します。
もう一つ注目すべき点は、twentytwentyone
文字列が繰り返し使われていることで、これはテキストドメインと呼ばれています。もしあなたのテーマが翻訳に対応していて、カスタム投稿タイプを翻訳させたいのであれば、あなたのテーマが使用しているテキストドメインについて言及する必要があります。
テーマのテキストドメインは、テーマディレクトリのstyle.css
ファイル内、または管理画面の外観 “ テーマファイルエディターで見つけることができます。テキストドメインはファイルのヘッダーに記載されています。
twentytwentyoneを
あなたのテーマのテキストドメインに置き換えるだけです。
変更に満足したら、「更新」ボタンをクリックするだけで、あとはWPCodeが処理します。
プラグインでカスタム投稿タイプを作成する
WordPressでカスタム投稿タイプを作成するもうひとつの簡単な方法は、プラグインを使うことだ。この方法は安全で超簡単なので、初心者におすすめです。
最初に行う必要があるのは、カスタム投稿タイプUIプラグインをインストールして有効化することです。詳しくは、WordPressプラグインのインストール方法のステップバイステップガイドをご覧ください。
有効化したら、CPT UI ” Add / Edit Post Typesで新規カスタム投稿タイプを作成する必要があります。新規投稿タイプの追加」タブに移動します。
まず、’movies’のようなカスタム投稿タイプのスラッグを指定する必要があります。このスラッグはURLとWordPressのクエリーで使用されるので、アルファベットと数字しか使用できません。その下に、カスタム投稿タイプの複数形と単数形の名前を指定する必要があります。
その後、「選択したラベルに基づいて追加ラベルを入力する」というリンクをクリックすることができます。これにより、下の追加ラベルフィールドが自動的に入力され、通常は時間を節約することができます。
追加ラベル」セクションまでスクロールしてください。リンクをクリックしなかった場合は、投稿タイプの説明とラベルの変更が必要になります。
これらのラベルは、特定の投稿タイプのコンテンツを管理する際にWordPressユーザーインターフェース全体で使用されます。
次の投稿タイプの設定です。ここから投稿タイプに様々な属性を設定することができます。各オプションには簡単な説明がついています。
例えば、投稿タイプをページのように階層化しないようにしたり、時系列投稿を逆順に並べ替えたりすることができます。
一般設定の下に、この投稿タイプがサポートする編集機能を選択するオプションがあります。設定したいオプションにチェックを入れてください。
最後に「投稿タイプを追加」ボタンをクリックして保存し、カスタム投稿タイプを作成します。
これですべてです。これでカスタム投稿タイプの作成が完了し、コンテンツの追加を開始することができます。
カスタム投稿タイプをサイトに表示する
WordPressには、カスタム投稿タイプを表示するためのサポートがビルトインされています。新しいカスタム投稿タイプにいくつかの項目を追加したら、それらをサイトに表示しましょう。
使える方法はいくつかあり、それぞれに利点がある。
初期設定アーカイブテンプレートを使用したカスタム投稿タイプの表示
まず、外観 ” メニューからカスタムリンクをメニューに追加します。このカスタムリンクはあなたのカスタム投稿タイプへのリンクです。
SEOフレンドリーなパーマリンクを使用している場合、カスタム投稿タイプのURLはこのようになる可能性が高いです:
http://example.com/movies
SEOフレンドリーなパーマリンクを使用していない場合、カスタム投稿タイプのURLは次のようになります:
http://example.com/?post_type=movies
‘example.com’を自分のドメイン名に、’movies’をカスタム投稿タイプ名に置き換えることをお忘れなく。
メニューを保存し、サイトのフロントエンドにアクセスしてください。あなたが追加した新しいメニュー項目が表示され、それをクリックすると、テーマのarchive.phpテンプレートファイルを使用してカスタマイ投稿タイプのアーカイブページが表示されます。
カスタム投稿タイプテンプレートの作成
カスタム投稿タイプのアーカイブページの外観が気に入らない場合は、カスタム投稿タイプのアーカイブ専用のテンプレートを使うことができます。
必要なことは、テーマディレクトリに新しいファイルを作成し、archive-movies.phpという
名前にすることだけです。本当に〜してもよいですか?’movies’をあなたのカスタム投稿タイプの名前に置き換えてください。
はじめに、テーマのarchive.php
ファイルのコンテンツをarchive-movies.php
テンプレートにコピーし、必要に応じて変更してください。
これで、カスタマイザー投稿タイプのアーカイブページにアクセスするたびに、このテンプレートが使われるようになります。
同様に、投稿タイプの個別エントリー表示用のカスタムテンプレートを作成することもできます。そのためには、テーマディレクトリにsingle-movies.phpを
作成する必要があります。movies’をカスタム投稿タイプの名前に置き換えることを忘れないでください。
テーマのsingle.php
テンプレートのコンテンツをsingle-movies.php
テンプレートにコピーし、あなたのニーズに合うように修正することから始めることができます。
さらに詳しく知りたい方は、WordPressでカスタム個別投稿テンプレートを作成する方法をご覧ください。
カスタム投稿タイプをフロントページに表示する
カスタム投稿タイプを使用する利点の一つは、カスタムコンテンツタイプを通常の投稿から区切ったままにできることです。しかし、お望みであれば、カスタム投稿タイプをサイトのフロントページに表示することもできます。
無料のWPCodeプラグインを使って、このコードを新しいスニペットとして追加するだけです。詳しい手順については、この投稿のコードを手動で追加するセクションをご覧ください。
add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
function add_my_post_types_to_query( $query ) {
if ( is_home() && $query->is_main_query() )
$query->set( 'post_type', array( 'post', 'movies' ) );
return $query;
}
movies」をカスタム投稿タイプに置き換えることをお忘れなく。
カスタム投稿タイプのクエリー
もしあなたがコーディングに慣れていて、テンプレートの中でループクエリーを実行したいのであれば、その方法を紹介しましょう。データベースにクエリーすることで、カスタム投稿タイプから項目を取得することができます。
カスタム投稿タイプを表示したいテンプレートに以下のコードスニペットをコピーする必要があります。
<?php
$args = array( 'post_type' => 'movies', 'posts_per_page' => 10 );
$the_query = new WP_Query( $args );
?>
<?php if ( $the_query->have_posts() ) : ?>
<?php while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
<h2><?php the_title(); ?></h2>
<div class="entry-content">
<?php the_content(); ?>
</div>
<?php endwhile;
wp_reset_postdata(); ?>
<?php else: ?>
<p><?php _e( 'Sorry, no posts matched your criteria.' ); ?></p>
<?php endif; ?>
このコードでは、新しいWP_Queryクラスの引数に投稿タイプとページあたりの投稿数を定義しています。そしてクエリーを実行し、投稿を取得し、ループの中に表示します。
カスタム投稿タイプをウィジェットに表示する
WordPressには初期設定で最近の投稿を表示するウィジェットがありますが、カスタマイ投稿タイプを選択する権限がありません。
新しく作成した投稿タイプの最新エントリーをウィジェットに表示したいとしたらどうしますか?これを行う簡単な方法があります。
最初に行う必要があるのは、カスタム投稿タイプウィジェットプラグインをインストールして有効化することです。詳しくは、WordPressプラグインのインストール方法のステップバイステップガイドをご覧ください。
有効化したら、外観 ” ウィジェットにアクセスし、「最近の投稿(カスタム投稿タイプ)」ウィジェットをサイドバーにドラッグ&ドロップするだけです。
このウィジェットは、任意の投稿タイプからの最近の投稿を表示することができます。カスタム投稿タイプを’投稿タイプ’ドロップダウンから選択し、必要なオプションを選択する必要があります。
その後、画面上部の「更新」ボタンをクリックし、あなたのサイトにアクセスしてウィジェットの動作を確認してください。
プラグインはまた、アーカイブ、カレンダー、カテゴリー、最近のコメント、検索、タグクラウドを表示するカスタム投稿タイプウィジェットを提供します。
このチュートリアルで、WordPressでカスタム投稿タイプを作成する方法を学んでいただけたなら幸いです。また、ブログのトラフィックを増やす方法や、WordPressの一般的なエラーとその修正方法のリストもご覧ください。
If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.
Syed Balkhi says
Hey WPBeginner readers,
Did you know you can win exciting prizes by commenting on WPBeginner?
Every month, our top blog commenters will win HUGE rewards, including premium WordPress plugin licenses and cash prizes.
You can get more details about the contest from here.
Start sharing your thoughts below to stand a chance to win!
R Davies says
You have a syntax error in your second (more detailed) example, code does not work in latest WordPress 7.4.3
) Warning: call_user_func_array() expects parameter 1 to be a valid callback, function ‘custom_post_type’ not found or invalid function name
Any chance of an update / correction?
WPBeginner Support says
Hi R Davies,
We checked and it worked perfectly.
管理者
Archit says
Is the comma at the end if the supports array (in the options for the custom post type) deliberate?
Robert Stuart says
On line 31? Yes, that’s normal PHP code.
“The comma after the last array element is optional and can be omitted. This is usually done for single-line arrays, i.e. array(1, 2) is preferred over array(1, 2, ). For multi-line arrays on the other hand the trailing comma is commonly used, as it allows easier addition of new elements at the end.”
saurabh says
How to enable ‘Post Settings’ in Custom_Post_type (using Custom Post Type UI Plugin)?
Arias says
Hello, I have been having problems with this plugin.
It has disabled the option to create categories and tags,
I have been looking for an example to place them manually but I still have not found anything.
I am trying to undo if with this method I can fix the problem but I would greatly appreciate your help.
stormonster says
In your $args array, on the ‘taxonomies’ index, add ‘category’ and ‘post_tag’.
That should do the trick.
John D says
Way too much coding. WordPress need to keep things more simple.
Ilija says
This is why I use my own CMS where I can create new post types in a fraction of a second directly through cms itself. Without any coding, unfortunately big agencies want WordPress developers and have to learn it, seems so complicated..
Sarah A says
Hi, i’ve succeded to display group of CPT with a specific design in a pop-up when you click on a image like the first one But it opens a new page and when you click out of the pop-up to quit you don’t return to the homepage, and i don’t want that. I want all on the homepage.
I’ve put the code of the CPT to display as the pop-up on “single-chg_projet.php” and open and close the pop-up with javascript. I’ve already tried to put all the code of single-chg_projet.php on the index, but it display nothing. Or i may be failed somewhere. Please help me. Thanks
Ghulam Mustafa says
Hi,
Thanks for the great code. Just a minor correction to the code. The endwhile; statement is missing before else: statement in the Querying Custom Post Types section =)
Tony Peterson says
THIS! Please update your code to reflect this syntax error as it caused me a bit of heartache until I found Ghulam’s comment. It’s working now.
Arkanum says
Yes! True. It’s miss befire wp_reset_postdate();
The cycle while does not end
Azamat says
Typo: “When doi I need a custom post type?”
WPBeginner Support says
Thanks for notifying us
We have updated the article.
管理者
Jhon says
hey, can you guide me in the process of making a custom glossary like you have on your site?
WPBeginner Support says
We use a custom fields and a custom template to display Glossary terms.
管理者
Anil Reddy says
I want to create list type for posts in the category page for my website
david ben oren says
how do i clone a post type which has a speicifc table in it, i need to create a seperate post type for other tables.
betty says
How do I add a custom field to a Post Type?
WPBeginner Support says
Please see our guide WordPress Custom Fields 101.
管理者
Megan says
I’ve downloaded the plugin and want to add two custom post types. 1. Fanfiction for all of my writings and 2. Fanart for all of my art.
For Fanfiction – I want the ability to link chapters together into a story and be able to upload chapters to a story as I write them.
For Fanart – I’d like to have the focus be on an image (obviously) with a description underneath it
Is this article what I need or this something completely different?
Thanks,
Megan
Zubair Abbas says
Hi,
I simply copied the code to my site’s functions.php. The new post type is visible in the dashboard but when I try to see a post after publishing it, a blank page appears. Later I realised that even the default posts are not opening.
When I remove the code from functions.php everything works fine again.
Please help
Thanks,
Zubair Abbas
Jouke Nienhuis says
If you see a blank page, it often means that you forgot a character. The fact that you see the posts if you delete your custom code, confirms that you have a typo. Check for semi-colons ” ; ” and the opening and closing brackets.
To see exactly where you made a mistake, you could edit the wp-config file. Look for ERROR REPORTING and set this value to true. After that test again and there you get an error and a line with the omission.
Alex says
I have created the CPT and is working beautifully, but Google cannot find it even after updating sitemaps, using SEO plugins or fetching on Google Webmaster Tools. Any thoughts on why is that happening?
WPBeginner Support says
It takes Google sometime to start showing new content in search results. Just to be on safe side, check your SEO plugin settings to make sure you are not blocking indexing of your CPTs or CPT archive pages.
管理者
Amunet says
Creating Custom Post Type can be easy especially with a plugin. The real trick is to show them on the page. Usually you need quite advanced custom development or theme specific plugins like that for Avada.
Unfortunately there is no universal way to display CPT in WordPress.
WPBeginner Support says
Actually, there are straight forward and standard ways to display CPTs in WordPress. We have mentioned one in the article above.
管理者
Jouke Nienhuis says
Like the author said, but I will repeat the answer.
In a nutshell create a link in your navigation menu
Advanced answer in a nutshell: create an archive page and a single page
Chuck says
Great article. How can you modify single post CPT post info based on the custom taxonomy? For instance:
Date | Author | Series | Book | Topic
This is easy to write but I want to figure out how to display a modified post info if one the missing taxonomy of Series, like:
Date | Author | Book | Topic
Otherwise the default post info displays as:
Date | Author | | Book | Topic
borahan says
I want to diplay specific category posts for current user in page. How can i do it?
Moazam Ali says
Dear,
Thanks for the post. I want to make a library of ebooks and want to use custom post type and portfolio to show the thumbnails of books in front end. Can you please guide how i can do that?
Sharon Wallace says
Hello All,
This is a great plugin. I’m trying to get the taxonomy to show on the page. I created one called Presenters. You can see it here.
How can I make that happen?
Thank you
WPBeginner Support says
Please take a look at how to create custom taxonomies in WordPress. Hope this answers your question.
管理者
Sharon Wallace says
Hello,
Thank you for your response. It still isn’t working but you’ve pointed me in the right direction. I will continue to play with it.
Have a wonderful day.
Dave S. says
Hi,
I have created a form (using ArForms plugin) which I need it to be turned into a Post-Type. Do you have any suggestions as to how to accomplish this please?
Thank you.
Mark Greenway says
Thanks for this, exactly what I needed to know to help me get to grips with custom post types.
Mark.
shashik says
Thanks dear..it’s very useful..
Graham says
Thank you! Just what I was looking for. It’s amazing how many times I find wordpress work-arounds and then forget how I do them and keep having to come back.
Much appreciated!
Ram says
Very useful! Thank you….
MELAS says
Dear,
I don’t have a lot of knowledge about coding. How can I see on the specific page theses Custom post types and taxonomies?
Thanks in advance!
MELAS
Mike Ritter says
Is there an error? Shouldn’t the function be `create_post_type`?
WPBeginner Support says
No mike, it is not a core function. You can define this function anyway you want.
管理者
Vera says
Hello,
Thank yo for this beautiful tutorial.
I have gone and done everything as you said.
Two things I do not understand:
1.
You have specified taxonomy “genre”. Where is that set up? What if I want to make the actual genres underneath that? How do I make them? Where do I see them?
2.
I would like to make the menu links to the “Movies” and underneath – sublinks to “Genres”. I can see the “Movies” in the Menu section, each post, like pages. Don’t really need that, but I won’t mind. How to see the “Genres” in there?
Thank you,
Vera
WPBeginner Support says
Seems like you have successfully created your custom post type, i.e. Movies. The next step is to create custom taxonomy, Genres. A taxonomy is a way to sort content in WordPress. Categories and tags are two default taxonomies that come built in with WordPress and are by default associated with ‘Posts’. Please see our tutorial on how to create custom taxonomies in WordPress. Follow the instructions in that tutorial to create your custom taxonomy Genres and associate it with the post type movies. Your custom taxonomy will appear below the Movies menu in WordPress admin area.
管理者
Hamed 3daJoo says
I do All Of This but when i want to public a post by this post type my post types redirects to main page (i Just coppied your code without any changes)
for example i write this post with Movies post type
please help me ilove post type but i can’t use it correctly
Ciprian says
Have you tried re-saving the permalinks structure?
Cheers!
Aaron says
Excellent post! Thanks for the content.. helped a lot.
antonio says
hi i’m trying to add the snippet of code fort the post type movie… i copied it into functions.php but when i refresh the page nothing is shown. i’m using the twenty fourteen theme… what can be wrong?
ashish shestha says
How to display Csutom post type in front End??
Fahd says
Hi, Custom post types on my wordpress website were working fine from last 2 years. But what happend is when I change the title of post and click update it save other changes too. But if I keep the post title same and make changes in the post, it doesn’t save it. Any recommendations please?
WPBeginner Staff says
Please check out this tutorial on how to show/hide text in WordPress posts with toggle effect.
Arup Ghosh says
Thanks for the info.
Arup Ghosh says
I want to create a custom post type coupons with reveal coupon option and the code will link to the store url, can you tell me how can I do that. I don’t have much knowledge about coding.
Isaías Subero says
Great articule. How can I add icons to custom post types on the wordpress back-end just like it is shown on the picture?
Jouke Nienhuis says
it is one of the arguments ($args) when defining the custom post type.
Add ‘menu-icon’ => ‘dashicons-cart’ to the $args list.
WordPress uses built-in dashicons, but you can also use your own icons.
More information on this link:
Johan says
Hi, the excerpt and the custom fields data is not displaying in the front end… any idea of why this is happening?
Bill Querry says
I forgot to mention, prreferably via code for my functions.php file since that’s where the curernet CPT are defined.
Bill Querry says
I am looking at a way to add categories to some existing custom post types. Anyone able to point me in the right direction?
Jouke Nienhuis says
You can add new taxonomies to an existing Post Type (custom or not) just by filling in the right post-type when you write the function to create it. Category is just a taxonomy name which includes all posts with a category.
If you want to make a new category, just click on category on the menu and create a new one.
Examples of categories are Boats if your post is about boats or planes if your post is about planes. More categories is also possible, just select or add them in the right sidecolumn when you are writing your new post or editing one.
A post type is not attached or linked to a specific category, a post is.
Robey Lawrence says
I just tried to use the snippet under
Querying Custom Post Types,
and figured out it needs a before the reset.
YassinZ says
Thanks for the clean handy article
I just want to use the text editor in the custom post
so that I can use html tags,
Yassin says
thanks for such an awesome clear tutorial
but I’m faceing a problem in displaying the CPT I’m using SEO friendly permalinks when I direct to may website/movies the CPT are duplicated
Aris Giavris says
Very useful! Thank you.
I would like to add to every tag of my posts one of the following signs: +, -, +/-. May I?
If so, then I would like to have the choice to represent the signed tags as follow: all the +tags, all the -tags, all the +/-tags.
I think I am questioning a lot of things.
Placid says
Hi,
I am having a hard time implementing a custom post type correctly. I have searched for a solution for a long time but couldn’t find any. Here’s what I did:
1. Freshly installed WordPress in my local wamp server (enabled apache rewrite_module first).
2. Using default theme (twenty fourteen). No plugins installed.
3. Changed permalinks to “Post name”
4. In the plugins folder, created a folder named pr_custom_posts and inside that, created a file named pr_custom_posts.php. In the file I created a custom post type. The code is as follows:
register_post_type();
//flush_rewrite_rules();
}
public function register_post_type () {
$args = array(
‘labels’ => array (
‘name’ => ‘Movies’,
‘singular_name’ => ‘Movie’,
‘add_new’ => ‘Add New Movie’,
‘add_new_item’ => ‘Add New Movie’,
‘edit_item’ => ‘Edit Movie’,
‘new_item’ => ‘Add New Movie’,
‘view_item’ => ‘View Movie’,
‘search_items’ => ‘Search Movies’,
‘not_found’ => ‘No Movies Found’,
‘not_found_in_trash’ => ‘No Movies Found in Trash’
),
‘query_var’ => ‘movies’,
‘rewrite’ => array (
‘slug’ => ‘movies/’,
‘with_front’=> false
),
‘public’ => true,
‘publicly_queryable’ => true,
‘has_archive’ => true,
‘menu_position’ => 10,
‘menu_icon’ => admin_url().’/images/media-button-video.gif’,
‘supports’ => array (
‘title’,
‘thumbnail’,
‘editor’
)
);
register_post_type(‘jw_movie’, $args);
//flush_rewrite_rules();
}
}
add_action(‘init’, function() {
new PR_Movies_Custom_Post();
//flush_rewrite_rules();
});
?>
The Good Thing: The CPT is showing in my admin panel and I can add and view movies there.
THE PROBLEM: I cannot preview the movies in the front end (By clicking the “view” in the CPT in admin panel). It shows in the front end only when I set permalink to default (http://localhost/wp02/?p=123).
What I have tried:
1. Go to permalink, keep permalink settings to “Post name” and Save changes.
2. Use flush_rewrite_rules() in several places (one by one) in my code. Please see the commented out parts in the code above.
3. Created a menu item as:
URL: http://localhost/wp02/movies
Navigation Label: Movies
This creates a menu item in the front end but shows “Not Found” when “Movies” link is clicked.
This is driving me crazy. Can anyone please help on this? I would really appreciate.
kikilin says
I was going crazy too with the same “Not Found” issue, until I tried this: go to Settings > Permalinks and then re-save your settings. I had switched my setting to Default, and then changed it to Post Name (for my project’s needs). After that, links were working as expected.
WPBeginner Staff says
Yes sure we will try to make a video tutorial soon. Please subscribe to our YouTube Channel to stay updated.
rehan says
Helpfull tutorials and posts
thanks
ceslava says
Another easy way is just duplicate the archive.php and rename it to archive-movies.php and the same for single.php -> single-movies.php
Then you can make tweaks to the php files for your theme.
Best regards
Mladen Gradev says
lol the plugin looks super useful, thanks.
Achilles Khalil says
Very hard to display. please can you make a video tutorial ?
Mik says
Hi, I’ve been reading and following your posts for so long now, you are amazing, and targeting those missing stuff of beginners… Thank you.
Davide De Maestri says
This plugin should be okay, but after every upgrade they’ve got some bug. Due to exporting field, or while migrating from local to remote etc… So It’s better to hand-write the code and put into functions.php