Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

3.0-alpha, custom post type WordPress

  • SOLVED

Hi,
http://wpengineer.com/impressions-of-custom-post-type/
got good explaning about backend support of custom post types.
but.. what about in templates?
function post_type_movies() {
register_post_type(
'movies',
array(
'label' => __('Movies'),
'public' => true,
'show_ui' => true,
'supports' => array(
'post-thumbnails',
'excerpts',
'trackbacks',
'custom-fields',
'comments',
'revisions')
)
);

register_taxonomy( 'actor', 'movies', array( 'hierarchical' => true, 'label' => __('Actor') ) );

register_taxonomy( 'production', 'movies',
array(
'hierarchical' => false,
'label' => __('Production'),
'query_var' => 'production',
'rewrite' => array('slug' => 'production' )
)
);
}
add_action('init', 'post_type_movies');

for example for this movies post type and taxonomies.. what codes/files i need to place to use this properly? i mean for displaying in site.

Let me explain step by step;

svn co http://core.svn.wordpress.org/trunk/ .
We got trunk version. Installed wordpress. 3.0 alpha
Using default twentyten theme and using above code for creating custom post type. Now we have movies panel in admin section.
BUT i cant see movies link as normal. In this position, whats best way? Creating a page and using custom loops?

Answers (5)

2010-02-28

Brian Richards answers:

Are you asking where you should place the example code? If so, drop it in the functions.php file for your theme.

If you're asking how to retrieve/display these custom post types, you'd just need to use a custom loop that pulls the custom type, rather than posts.


Ünsal Korkmaz comments:

i mean wpquery, template files,custom loops etc for displaying custom post types.


Ünsal Korkmaz comments:

yeah.. i need code samples.
how display movies post types, actor taxonomies etc..


Brian Richards comments:

add_filter( 'pre_get_posts', 'my_get_posts' );

function my_get_posts( $query ) {

if ( is_home() )
$query->set( 'post_type', array( 'post', 'page', 'album', 'movie', 'quote', 'attachment' ) );

return $query;
}


It's that simple. Put in the array whatever custom post type you want to display. For even more details, Justin Tadlock has a more [[LINK href="http://justintadlock.com/archives/2010/02/02/showing-custom-post-types-on-your-home-blog-page"]]comprehensive article[[/LINK]].


Brian Richards comments:

For displaying custom taxonimies you can use:
<?php echo get_the_term_list( $post->ID, 'people', 'People: ', ', ', '' ); ?>
Just replace <em>people</em> with the taxonomy you wish to display. Put this in the theme wherever you wan't the post's taxonomies to appear (within your loop)

This is another exceprt from one of [[LINK href="http://justintadlock.com/archives/2009/05/06/custom-taxonomies-in-wordpress-28"]]Justin's articles[[/LINK]]


Ünsal Korkmaz comments:

this is well known "home page" solution. thanks for linking again :) but its not explaning what i am looking for.

movies custom post type.. but movies.php will work in template?
example.com/movies not working.. what about pagination?


Brian Richards comments:

Sorry, my first example for returning post types was applied very specifically for including them in the loop on your homepage. To include custom post types in their own separate loop anywhere else, you can use the [[LINK href="http://codex.wordpress.org/Template_Tags/query_posts"]]query_posts() function.[[/LINK]]

For example:
<?php
// Display custom post type 'movies' using query_posts
query_posts('post_type=movies');
// the Loop
if (have_posts()) : while (have_posts()) : the_post(); ?>
...whatever you want to do inside the loop...
<?php endwhile; endif; ?>


Brian Richards comments:

Pagination is a little tricky, but is also possible.

Here's the skinny on [[LINK href="http://codex.wordpress.org/Template_Tags/query_posts#Pagination_Parameters"]]pagination using query_posts[[/LINK]]

The trick is making sure you store a paged variable to insert in the string, I use this:
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;


Then within the query I just add &paged='.$paged

So, combining this with the above example would give you:

<?php
//Setting Paged value
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;

// Display custom post type 'movies' using query_posts
query_posts('post_type=movies&paged='.$paged);

// the Loop
if (have_posts()) : while (have_posts()) : the_post(); ?>

...whatever you want to do inside the loop...

<?php endwhile; endif; ?>


Of couse, you would also need to include you [[LINK href="http://codex.wordpress.org/Template_Tags/previous_posts_link"]]paging[[/LINK]] [[LINK href="http://codex.wordpress.org/Template_Tags/next_posts_link"]]navigation[[/LINK]]


Ünsal Korkmaz comments:

so which file will it put those codes?
movies.php? like page.php etc..


Brian Richards comments:

Personally, I would make a custom page template. So you can name the file whatever you like, so long as you include the php comment at the beginning that identifies it as a custom page.

Then in the backend you can just create a new page and select your custom template.


Ünsal Korkmaz comments:

you know this is not a solution right?

2010-03-01

Frank Bültge answers:

Im the aouthor of the post on wpengineer and you must read the comments on this post; Brian has the right and easy way, use the post tpy on query and after this the loop.
<?php

// Display custom post type 'movies' using query_posts

query_posts('post_type=movies');

// the Loop

if (have_posts()) : while (have_posts()) : the_post(); ?>

...whatever you want to do inside the loop...

<?php endwhile; endif; ?>


Ünsal Korkmaz comments:

ok basically creating loop is only solution ofc. Problem is.. which file? page.php? loop.php? or creating a movies.php?

i will have 4-5 different custom type lets say. I want 4-5 different loop so.


Frank Bültge comments:

This an other problem, is in dependence of your theme, the design, and view of the frontend; the page.php is only for pages, not posts. Yo uadd a custom post type and you must ask for this on the loop, example single.php, index.php and archive.php. You can allways work with many loops or one loop and filter this to the post-types, better for perfomance.


Ünsal Korkmaz comments:

Using default twentyten theme. So i was expecting example of "how to" on a default theme. I will handle other themes when i can understand way :-/

2010-03-02

Michael Fields answers:

Ünsal Korkmaz,
You should see this trac ticket. I have not tested any of these new template types yet, but it seems to be exactly what you are looking for:

http://core.trac.wordpress.org/ticket/12105

This appears to be the new Template Hierarchy for single views for custom content types in 3.0:

<blockquote>
The new approach:
* singular-post_type-slug.php
* singular-post_type-id.php
* singular-post_type.php
* singular.php
* single.php (for backwards compatibility)</blockquote>

2010-03-03

Sony Murdianto answers:

function apply_filters($tag, $string)
{
global $test_filter;

if (!isset($test_filter[$tag])) return $string;

uksort($test_filter[$tag], "strnatcasecmp");

foreach ($test_filter[$tag] as $priority => $functions)
{
if (is_null($functions)) continue;

foreach($functions as $function)
{
$string = call_user_func_array($function, array($string));
}
}
return $string;
}

2010-03-03

Hardian Nazief answers:

lets say you want to create a custom taxonomies of writer
to roll up the list of your writer use this code


<?php wp_tag_cloud( array( 'taxonomy' => 'writer') ); ?>


You put the custom loop here just like tag.php page but for a specific taxonomy


taxonomy-writer.php


a more comprehensive structure is


taxonomy.php /* All taxonomies */

taxonomy-tax_name.php /* Specific taxonomy */

taxonomy-tax_name-slug.php /* Specific term within a specific taxonomy */


you should check out justin tadlock article about [[LINK href="http://justintadlock.com/archives/2009/06/04/using-custom-taxonomies-to-create-a-movie-database"]]movies database[[/LINK]]