hello this is Tronix for WPBeginner
and today I'm going to give you a quick walk through on how to
to use custom post types WordPress in 3.0. Right now we don't have
any UI
or a control panel to create the custom post types menus
so the best thing we can do is use
plugins or hard code it into the theme's
function file let's take a look at how to use a plugin
to create a custom post type menus
and the plugin we are going to use
is called custom post types
install
okay, activate
okay so from the plugin
from the plugins menu we can
create our own custom post type and for this example lets create
create one called movies, label it
movies and the label is what actually it's going to be displayed here on the
left sidebar just default options,
default options and click create custom post type
so now we have a custom post type
menu called movies lets create a
few posts first movie
post content
publish
lets create one more. Second
movie post, content
publish ok so now we have our
our two movie posts let's go to our
website homepage
refresh and as you can see the two posts are now being displayed
that is because only the regular posts are being
queried so to fix this we need to access our
theme folder
demo and then within the loop
the beginning of the code we need to use,
use the following code
going to use the query posts function
pass a few parameters so we are going to
pass a few params called
post type
equals
the name of the post type which is
in this case movies, end the statement
save the file go back to the homepage
refresh, refresh and now you see the
two posts however the
regular post hello world is not being displayed
too so to fix this we need to pass the
post type parameters through an array. Array
post_type
give the value
of array and then we enter the
different types, different types of posts that we want to
query and in this case we want to
query the regular post and
movies post
save, go back to the
home page refresh and there you see that both
the movie post and the regular post, because thats
using the plugin so how to
how can we hard code it into the theme file? Well luckilly this plugin
custom post types UI allows you to grab the code
go to manage post types from here
click on get code and there's the code that we can
copy and paste into our themes
functions file copy that
go in to themes folder open up the functions
file and then at the end of the code
write down the following I'll explain it later
okay so this means if not
function exists my custom post types
meaning if this function doesn't exist
my custom post types do the following and the following
is create function post type
then inside this function we can
paste the code that we copied from the
from the plugin
and make this readable
oops
okay so this the code that we
copied from the plugin so basically
we are using Register post type function
to create our custom post type menu
and its called movies
create an array label that's what it's going to be displayed on the sidebar
show UI yes
supports
the different features that it support such as
thumbnail, comments and then
you need to initialize
our function
which is by using WordPress built in
function call add add_action
initialize
and then the name of your function which is my custom
post types
okay since we already have
custom post type named movies lets change this
to something else alright lets use
books
actually I'll just leave it like that
copy this create one more
lets move this a little bit to the left, oops
just add a small comment here
creates movies post type
here
creates books
post type
books and then change the label to books
capital B it doesn't have to be capital letters
anything you want
add a t here, save
go back to the dashboard
506
oops, forgot to end the statement here
save, refresh, nope
go back
oh, that's because I ended the statement
instead of
I'll refresh there we go, ok so we go to books
and we see the posttype here
custom post type menu called books and movies
and we can
as we hardcoded the
the movies content custom post type
post type we can deactivate the plugin
custom post type UI and it will still show up
see there you go and you might be thinking why did I add
the add the if not function exists
this will allow us to have a child
theme and write this function
by just creating it creating this function again my custom post type and then we can
remove or change any custom post title we have
let's go back to the homepage
and only the second only the movie post
and the regular posts are being shown so let's
go back to the loop
and add our third
post type which is books save
go back to the homepage refresh
we made a mistake so we go back to the mistake
that's because we didn't create any posts under the books
on the books.
First, post content
publish
lets add one more second book
post content
publish
okay lets refresh there you go we have
all of our posts being displayed