Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 6 of 6
  1. #1
    Junior Member STHLMWebDesign's Avatar
    Join Date
    Apr 2010
    Location
    Stockholm
    Posts
    10
    Member #
    21731
    Anyone here had a go at custom post types in WordPress 3.0?

    I've about to start a project and going to build a website for a photographer completely in WordPress. I am completely against using a plug-in to handle the image galleries and instead want to use custom post types to do so.

    I've already enabled several custom post types (for example "portraits") via the functions.php file. I know need to figure out how to display these custom post types.

    I would like to make pages in the Dashboard (for example "portraits") and have those pages automatically display the appropriate custom posts. For example the "Portraits" page should display custom posts that are named "portraits."

    Anyone know how I go about doing this? Do I just modify my page.php and attempt to create some PHP condidtion that seems is the page name matches, a custom post type, and then returns those posts? Or do you need to create an entirely new file in your theme to handle these custom post types?

  2.  

  3. #2
    Member WPExplorer's Avatar
    Join Date
    Apr 2010
    Location
    USA
    Posts
    89
    Member #
    21716
    This is very easy...

    1. Add the post type to your functions.php (you did this)
    PHP Code:
    // Define Post Types
    add_action'init''create_post_type' );
    function 
    create_post_type() {
      
    register_post_type'portraits',
        array(
          
    'labels' => array(
            
    'name' => __'Portraits' ),
            
    'singular_name' => __'Portraits' )
          ),
          
    'menu_position' => 5,
          
    'public' => true,
          
    'supports' => array('title','editor','thumbnail')
        ));

    2. Create a New Page Template

    PHP Code:
    <?php
    /*
    Template Name: Portraits
    */
    ?>
    3. Add the query to get your custom posts and show them


    PHP Code:
    <?php query_posts(array(
                
    'post_type'=>'portraits'
                
    )); 
    ?>

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

    (whatever you want in your loop goes here)

    <?php endwhile; ?>

  4. #3
    Junior Member STHLMWebDesign's Avatar
    Join Date
    Apr 2010
    Location
    Stockholm
    Posts
    10
    Member #
    21731
    WPExplorer you are the man!

    I'd like to take it a step forward and was wondering if you had any advice. As it stands now I'd have to create files in my theme for each gallery page, for example "portraits", "wedding", and so on.

    I would like to modify the query_posts and try to make it dynamic, so I can have one file that I can apply to all the various gallery sections.

    You have any idea how I could set that up? I was thinking that a condition where we check the permalink would be about right, as the permalink would match the appropriate gallery in my functions file.

  5. #4
    Member WPExplorer's Avatar
    Join Date
    Apr 2010
    Location
    USA
    Posts
    89
    Member #
    21716
    You should consider creating only 1 custom post type (gallery). And then create several categories for this custom post type (portraits, wedding..etc).

    To allow taxonomies in custom posts you need to declare your custom post type as follows:

    PHP Code:
    // Define Post Types
    add_action'init''create_post_type' );
    function 
    create_post_type() {
      
    register_post_type'portraits',
        array(
          
    'labels' => array(
            
    'name' => __'Portraits' ),
            
    'singular_name' => __'Portraits' )
          ),
          
    'menu_position' => 5,
          
    'public' => true,
          
    'supports' => array('title','editor','thumbnail'),
          
    'taxonomies' => array('category')
        ));

    By adding taxonomies you can now create categories for your custom post type. Then you can call specific categories if you wish or show them all...

    Or if you are really talented you can incorporate the Quick Sand Plugin - jQuery Quicksand plugin

  6. #5
    Junior Member STHLMWebDesign's Avatar
    Join Date
    Apr 2010
    Location
    Stockholm
    Posts
    10
    Member #
    21731
    Thanks for your fast reply. I like the idea of using categories in my custom post types, but this still doesn't really fix my issue.

    How would I go about making my pages display the correct category of custom post without having to create files for each page in my theme?

  7. #6
    Member WPExplorer's Avatar
    Join Date
    Apr 2010
    Location
    USA
    Posts
    89
    Member #
    21716
    How would I go about making my pages display the correct category of custom post without having to create files for each page in my theme?
    It works just like regular post categories - Wordpress creates them for you. Checkout the Wordpress CODEX at WordPress.org


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 06:01 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com