A web developer's blog. PHP, MySQL, CakePHP, Zend Framework, Wordpress, Code Igniter, Django, Python, CSS, Javascript, jQuery, Knockout.js, and other web development topics.

WordPress: Post Types and Page Templates

Aside from the normal “Add Post” link in the WP-Admin Panel, you can add a custom post type. See http://codex.wordpress.org/Post_Types#Custom_Types.

For example, if you want to add “Videos and Podcasts” as another post type, you put this in your functions.php file:

register_post_type('videos_and_podcasts', array(
	'label' => __('Videos and Podcasts'),
	'singular_label' => __('Video/Podcast'),
	'public' => true,
	'show_ui' => true,
	'capability_type' => 'post',
	'hierarchical' => false,
	'rewrite' => false,
	'query_var' => false,
	'supports' => array('title', 'editor', 'author')
));

More details on the options can be found here.

You should be able to see something like this in your WP-Admin:

Your new custom post type is just like a normal post type.

Combining a custom post type with a Page Template

You can take things further by combining the custom page type with a Page Template. You simply have to create a PHP file that looks like this:

<?php
/*
Template Name: Landing Page Test
*/
?>
    <?php get_header(); ?>
    <h1>This is a test landing page</h1>
    <?php
    $args = array( 'post_type' => 'videos_and_podcasts', 'posts_per_page' => 10 );
    $loop = new WP_Query( $args );
    while ( $loop->have_posts() ) : $loop->the_post();
    	the_title();
    	echo '<div class="entry-content">';
    	the_content();
    	echo '</div>';
    endwhile;
    ?>
 
    <?php get_footer(); ?>
<?php wp_footer(); ?>

In your WP-Admin, when you try to add a Page, you should be able to see something like this:
.

There you have it, a Page Template combined with a custom post type to display a different “frontpage” / “landing page”.

This entry was posted in General and tagged , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>