• Iva

    (@supersonicsquirrel)


    I wanted to create a specific template file for a custom post type (e.g. no date, no author info and definitely no posts’ categories).

    So, I have registered a custom non-hierarchical post type and called it “song” which can have tags of its own (I called them “song-type”) and belong to categories of its own (I called them “release”).

    When I add a “song”, say that it belongs to such and such “release” and tag it with something; it’s still shown as a post in the “Uncategorized” post category.

    I searched a bit and found that adding ‘page-attributes’ to the array of what the custom type suppots gives Page functionalities to custom post types…so, I edited my function to look like this.

    function post_type_songs() {
    	register_post_type(
                    'song',
                    array(
                            'music' => __('song'),
                            'public' => true,
                            'show_ui' => true,
                            'supports' => array(
                                         'post-thumbnails',
                                         'page-attributes',
                                         'excerpts',
                                         'custom-fields',)
                    )
            );
    
    	register_taxonomy( 'releases', 'song', array( 'hierarchical' => true, 'music' => __('releases') ) ); 
    
            register_taxonomy( 'songtype', 'song',
    		array(
                             'hierarchical' => false,
    			 'music' => __('SongType'),
    			 'query_var' => 'songtype',
    			 'rewrite' => array('slug' => 'songtype' )
    		)
    	);
    }
    add_action('init', 'post_type_songs');

    So, I added this to the already existing list of what my custom post type supports (omitted the whole thing here) and Page functionalities were now visible on the edit screen; which included templates as well. I created a new template without date, author information, yet with keys for some custm fields. Yet, when I press save, the template goes back to default even though I set it to the one named Song output.

    Am I trying to do something that isn’t possible yet?

    P.S. This is what I was using for reference:
    https://wpengineer.com/impressions-of-custom-post-type/

    P.P.S. One more thing makes me see it’s not acting page-like: it has the unnecessary /blog/ in its URI, which pages do not have.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Not sure about using page templates for custom_post_types but you cant load a specific page template for a custom type, get_single_template() in wp-includes/theme.php actually looks for one.

    single-‘post_type’.php in the current 3.0 code base. so if you create a ‘single-song.php’ and put in you theme it will get used.

    More over if you want to take complete control of the template loaded you can do something similar to.

    function is_publication()
    {
    	$post_type = get_query_var('post_type');
    	// short had for if / else;
    	return $post_type == 'publications' ? true : false;
    }
    
    function get_publication_template()
    {
    	global $wp_query;
    
    	$object = $wp_query->get_queried_object();
    
     	$templates = array('publication-'. $object->ID .'.php', 'publication.php','single-'. $object->post_type .'.php', 'single.php' );
    	return locate_template( $templates );
    }
    
    function set_publication_template( $template )
    {
    	if ( is_publication() ) {
    		// custom template filename
    		$template = get_publication_template();
    	}
    	return $template;
    }
    
    add_filter('template_include', 'set_publication_template');

    where ‘publications’ was the name of my custom post type.

    Hope that helps.

    In theory, the below modification should check for/ and push a ‘page template’ to the front of the list of templates to look for. but not tested

    function get_publication_template()
    {
    	global $wp_query;
    
    	$object = $wp_query->get_queried_object();
    
     	$templates = array( 'publication-'. $object->ID .'.php',
    						'publication.php',
    						'single-'. $object->post_type .'.php',
    						'single.php' );
    
    	$page_template = get_post_meta( $object->ID , '_wp_page_template', true );
    	if (isset($page_template)) {
    		$template = array( $page_template ) . $templates;
    	}
    
    	return locate_template( $templates );
    }

    Hi Just tried that out and there was a bug in it anyway,

    if (isset($page_template)) {
    	$template = array( $page_template ) . $templates;
    }

    needs to be

    if ( !empty( $page_template ) ) {
    	$templates = array( $page_template ) + $templates;
    }

    however, the _wp_page_template post meta property never gets set, if you hit save, navigate away and navigate back the drop down is back on default, so would have to hack the code that saves that meta_box’s settings to save when the post type isn’t a page.

    Thread Starter Iva

    (@supersonicsquirrel)

    Thank you so much (and sorry for the late reply). From what I have seen, not even the two new tutorials linked from the development blog today touch this matter. ??

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Template files for post types other that Posts and Pages.’ is closed to new replies.