WordPress.org

Ready to get started?Download WordPress

Forums

Canvas Theme Custom Post Types (7 posts)

  1. d0cpaul
    Member
    Posted 7 months ago #

    So I've been searching and reading through the support forums for hours to resolve this so I thought I would finally ask. How do I customize a CTP template in the WooThemes Canvas theme? I created my own custom post type called, "disneyland". I duplicated content.php and archive.php and renamed it to content-disneyland.php and archive-disneyland.php and tried customizing content-disneyland but none of my changes reflect the CTP at all. What am I doing wrong?

    Here is the code I used to register the CTP in functions.php:

    // Register Custom Post Type

    function custom_post_type() {
    
    $labels = array(
    'name' => _x( 'Disneyland', 'Post Type General Name', 'text_domain' ),
    'singular_name' => _x( 'Disneyland', 'Post Type Singular Name', 'text_domain' ),
    'menu_name' => __( 'Disneyland', 'text_domain' ),
    'parent_item_colon' => __( 'Parent Information:', 'text_domain' ),
    'all_items' => __( 'All Disneyland Info', 'text_domain' ),
    'view_item' => __( 'View Disneyland Info', 'text_domain' ),
    'add_new_item' => __( 'Add New Info', 'text_domain' ),
    'add_new' => __( 'Add New Info', 'text_domain' ),
    'edit_item' => __( 'Edit Disneyland Info', 'text_domain' ),
    'update_item' => __( 'Update information', 'text_domain' ),
    'search_items' => __( 'Search products', 'text_domain' ),
    'not_found' => __( 'No Disneyland Information Found', 'text_domain' ),
    'not_found_in_trash' => __( 'No Disneyland Information found in Trash', 'text_domain' ),
    );
    $rewrite = array(
    'slug' => 'disneyland',
    'with_front' => true,
    'pages' => true,
    'feeds' => true,
    );
    $args = array(
    'label' => __( 'disneyland', 'text_domain' ),
    'description' => __( 'Disneyland Key Information', 'text_domain' ),
    'labels' => $labels,
    'supports' => array( 'title', 'editor', 'thumbnail', 'comments', 'revisions', 'custom-fields', 'post-formats', ),
    'taxonomies' => array( 'category', 'post_tag' ),
    'hierarchical' => true,
    'public' => true,
    'show_ui' => true,
    'show_in_menu' => true,
    'show_in_nav_menus' => true,
    'show_in_admin_bar' => true,
    'menu_position' => 5,
    'menu_icon' => '/Disneyland-Custom-Post-Type-Menu-Icon.png',
    'can_export' => true,
    'has_archive' => false,
    'exclude_from_search' => false,
    'publicly_queryable' => true,
    'rewrite' => $rewrite,
    'capability_type' => 'page',
    );
    register_post_type( 'products', $args );
    
    }
    
    // Hook into the 'init' action
    add_action( 'init', 'custom_post_type', 0 );
    
    }

    [Please always use the code buttons when posting code]

    If you know of a good tutorial or know what the problem is please help!

  2. bcworkz
    Member
    Posted 7 months ago #

    Even though you defined the rewrite slug as 'disneyland' and you can use that slug in permalinks, the actual post_type slug used to load template files remains the post_type argument you supplied in register_post_type(): 'products'

    Your template filenames need to be archive-products.php and content-products.php. Behind the scenes you will see the post_type stored in the posts table is 'products', and the query var 'post_type' in all related queries is 'products' as well. For the forward facing interface, you've relabeled everything as disneyland, but not behind the scenes.

  3. d0cpaul
    Member
    Posted 7 months ago #

    Sweet! Thanks for the info I finally got everything I needed working. You rock!

  4. d0cpaul
    Member
    Posted 7 months ago #

    Actually I'm still having one more problem, Every time I try to view one of the categories for the custom post type it says,

    Sorry, no posts matched your criteria.

    Any ideas?

  5. bcworkz
    Member
    Posted 7 months ago #

    When you click a typical category permalink such as http://www.example.com/category/packages/ the category query is constructed using the default post_type 'post'. How you get the query to return products post_type depends on what behavior you want.

    If you just need the category link clicked on a template to return the expected products post_types, modify the permalink to be like so: http://www.example.com/category/packages/?post_type=products

    If you want all category queries to always include products, setup a action hook to 'pre_get_posts'. If any query is a category query, set the 'post_type' query var to be an array of all post_types you want returned.

  6. d0cpaul
    Member
    Posted 7 months ago #

    OK I have two questions,

    1. If I use that action hook will the URL display as follow:
    http://www.example.com/category/packages/ if I wanted the template to display the posts? Can you provide an example of the action query that I could use?

    2. Let's say I wanted to customize the category template and add some of my own HTML/PHP, what template file should I be customizing?

    Sorry for all the questions I'm pretty new to PHP and have limited knowledge of it.

  7. bcworkz
    Member
    Posted 7 months ago #

    No worries, we were all newbies at one point. If you can't figure something out, you gotta ask!

    Using action hooks does not change the browser address display, the process is invisible to the browser. To cause category archive pages to display both posts and products, do something like this (untested):

    add_action('pre_get_posts', 'd0p_query_products');
    function d0p_query_products( $query ) {
       if( is_category()) {
          $query->set('post_type', array('post', 'products'));
       }
    }

    What template file you edit depends on what your want to edit and what theme you are using. WP web pages are usually composed from several different templates. It can be difficult to determine which is being used for any particular web page. So much so, there is a plugin to help you figure this out. I usually just add HTML comments near the top of each file that's a likely candidate. The comments show up nicely when you view page source with syntax highlighting.

    Don't get carried away writing comments unless it's your own private theme. Theme updates will often remove your efforts. This is why theme customizations should be done as a child theme. Your efforts will then persist through parent theme updates.

    That said, if you want to alter how the posts themselves are displayed the file is usually "content.php"

Reply

You must log in to post.

About this Topic