Grow your CSS skills. Land your dream job.

WP:Understanding Custom Post Types and Taxonomies

  • # March 10, 2011 at 1:51 am

    Hi everyone .

    First of all I want to apologize because my grammar will be a litt… very confusing, Second, my knowledge of coding is minute.

    I would like to share my experience with this two great options that WordPress Brought to us since version 2.8 ¿¿?? .

    I think I failed implementing this or WordPress failed in some important points . I don’t know, you’ll decide…

    Let’s start writing about Custom Post Types….

    it seems complex to add this kind of options in functions.php to people that doesn’t know anything of php… or etc… (cof cof , designers, like me)

    But it’s easier than you thought…Let’s put a example here , We’re going to build a Custom Post Type that we’re going to call: “2010″

    Personally I like to add actions at the top of functions.php :

    add_action('init', 'season_2010_register');

    Then we create the function , adding the parameters to make post types work correctly.:



    function season_2010_register() {

    $labels = array(
    'name' => _x('2010', 'post type general name'),
    'singular_name' => _x('2010 Item', 'post type singular name'),
    'add_new' => _x('Add New', 'portfolio item'),
    'add_new_item' => __('Add New 2010 Item'),
    'edit_item' => __('Edit 2010 Item'),
    'new_item' => __('New 2010 Item'),
    'view_item' => __('View 2010 Item'),
    'search_items' => __('Search 2010'),
    'not_found' => __('Nothing found'),
    'not_found_in_trash' => __('Nothing found in Trash'),
    'parent_item_colon' => ''
    );

    $args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true,
    'query_var' => true,
    'menu_icon' => get_stylesheet_directory_uri() . '/img/icons/article16.png',
    'rewrite' => true,
    'capability_type' => 'post',
    'hierarchical' => true,
    'menu_position' => null,
    'supports' => array('title','editor','thumbnail'),

    In the same function we add something that came with WP 3.1, the characteristic for adding archives in the post-types ( ‘has_archive =>)



    'rewrite' => array(
    'slug' => '2010',
    'with_front' => false
    ),

    'has_archive' => 'true'
    );

    register_post_type( '2010' , $args );
    flush_rewrite_rules();

    }

    it’s very easy to add the post-type archives to the template, just add a file in your template , that we must called :

    archive-my-post-type.php (obviously you have to change my-post-type to 2010 (that is the post-type name that we are using in this example)

    Now we can continue with the Taxonomies: We’re going to create 2 taxonomies

    first we add the action:

    add_action( 'init', 'create_taxonomies', 0 );

    function create_taxonomies()
    {

    $labels = array(
    'name' => _x( 'Movies', 'taxonomy general name' ),
    'singular_name' => _x( 'Movies', 'taxonomy singular name' ),
    'search_items' => __( 'Search in Movies' ),
    'all_items' => __( 'All Movies' ),
    'parent_item' => __( 'Parent Movies' ),
    'parent_item_colon' => __( 'Parent Movies' ),
    'edit_item' => __( 'Edit Movies' ),
    'update_item' => __( 'Update Movies' ),
    'add_new_item' => __( 'Add New Movie' ),
    'new_item_name' => __( 'New Movie' ),
    'menu_name' => __( 'Movies' ),
    );

    register_taxonomy('movie',array('2010'), array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'movie','hierarchical' => true ),
    ));


    $labels = array(
    'name' => _x( 'Other Worlds', 'taxonomy general name' ),
    'singular_name' => _x( 'Other Worlds', 'taxonomy singular name' ),
    'search_items' => __( 'Search in Other Worlds' ),
    'popular_items' => __( 'Popular in Other Worlds ),
    'all_items' => __( 'All in Other Worlds' ),
    'parent_item' => null,
    'parent_item_colon' => null,
    'edit_item' => __( 'Edit in Other Worlds' ),
    'update_item' => __( 'Update in Other Worlds' ),
    'add_new_item' => __( 'Add New Events in Other Worlds' ),
    'new_item_name' => __( 'New Event in Other Worlds' ),
    'menu_name' => __( 'Other Worlds' ),
    );

    register_taxonomy('otros','2010',array(
    'hierarchical' => true,
    'labels' => $labels,
    'show_ui' => true,
    'query_var' => true,
    'rewrite' => array( 'slug' => 'other-worlds','hierarchical' => true ),
    ));

    Let’s add a custom title text only because:

    we add the action:


    add_action('gettext', 'change_2010_title_text');


    function change_2010_title_text ($translation ) {
    global $post;
    if( isset( $post ) ) {
    switch( $post->post_type ){
    case '2010' :
    if( $translation == 'Enter title here' )
    return 'Enter Movie Title Here';
    break;
    }
    }
    return $translation;
    }

    Now the problem came here... We can have a Custom Post Type Archive and get all post of all taxonomies. that's perfect , because with a good permalink you'll get:

    http://mysite.com/2010/

    but what if we want to to have taxonomies archive and have permalinks like_:

    http://mysite.com/2010/movies

    Well , this is technically impossible with my knowlage ;)

    so I solve this with this:
    you can either create a custom page in your template with :


    < ?php
    /*
    Template Name: Movies
    */
    ?>

    or my favorite and clean way to organize your template:



    < ?php
    $post = $wp_query->post;

    if ( is_page('2') ) {
    include(TEMPLATEPATH.'/layouts/about.php');
    } elseif ( is_page('268') ) {
    include(TEMPLATEPATH.'/layouts/contact.php');
    } elseif ( is_page('443') ) {
    include(TEMPLATEPATH.'/layouts/taxonomy-movies.php');
    } else {
    include(TEMPLATEPATH.'/layouts/404.php');
    }
    ?>

    you can notice that the page 443 is the one will use to create the custom taxonomy "MOVIES"

    Let's put a parenthesis here.

    if we create in the root template the file "taxonomy-movies.php" this will create the template structure for the Taxonomy Terms , not for the taxonomy.

    let's go back in what we were.

    In the file tha we created : /layouts/taxonomy-movies.php

    we are going to query the post inside the terms of the taxonomy (do I explain myself clearly , I hope so)

    how I done this? :


    < ?php
    $args = array(
    'tax_query' => array(
    array(
    'taxonomy' => 'movie', // taxonomy
    'field' => 'slug', // taxonomy term by('id' or 'slug')
    'terms' => 'open-air' // texonomy term
    ),

    array(
    'taxonomy' => 'movie', // taxonomy
    'field' => 'slug', // taxonomy term by('id' or 'slug')
    'terms' => 'meridian' // texonomy term
    ),
    array(
    'taxonomy' => 'movie', // taxonomy
    'field' => 'slug', // taxonomy term by('id' or 'slug')
    'terms' => 'topographic' // texonomy term
    )
    )
    );
    query_posts( $args );
    ?>

    The code above is getting the post in the different terms that we added previously in the taxonomy. what we just do is like :

    (this is just an example :)


    < ?php query_post('category_name=X'); ?>

    but with WP3.1 we can get the same achieve , but with taxonomies.

    then we continue with the loop

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

    < ?php the_title(); ?>


    < ?php the_post_thumbnail('large') ?>

    < ?php the_content(); ?>




    < ?php endwhile; else: ?>

    Sorry, no posts matched your criteria.


    < ?php endif; ?>

    < ?php wp_reset_query(); ?>

    But the sad thing is that the permalink will show like:

    http://mysite.com/movies/ (or whatever name you use in the page) instead of http://mysite.com/2010/movies

    So I think that I miss something to achieve the taxonomy arhive or Does wordpress miss this??

    everybody , please feel free to give feedback , laugh , and share a beer ......

    see'a

    # March 10, 2011 at 1:51 am

    source:
    http://thinkvitamin.com/code/create-your-first-wordpress-custom-post-type/
    http://codex.wordpress.org/Function_Reference/WP_Query
    http://codex.wordpress.org/Pages
    http://ottopress.com/2010/wordpress-3-1-advanced-taxonomy-queries/
    http://nielsencreativemedia.com/blog/how-to-develop-a-custom-post-plugin-with-unique-taxonomy-and-tags
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".