WordPress.org

Support

Support » Themes and Templates » add a post type exclusive to a page

add a post type exclusive to a page

  • Hi all, in my theme, I have a custom page, but I want to apply a certain post type to be used only on this page and not any other pages. The reason is because I will be styling this post type a certain way, and I dont want someone to try to add it anywhere else other than this particular page.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi,
    When you say a certain post type do you mean a custom post type, is this a page, a post or a custom post type?

    When we know this we will be able to help more.

    Also have a look at the template heirarchy, as single-{$posttype}.php would work, lets use film as a post type then, single-film.php could be used.

    For a specific page page-{$slug}.php so page-news.php, or page-{$id}.php, page-234.php

    Then you can use the post_class(‘my-class’) in the new file just for that single instance, I wrote about post classes recently so this post may help.

    I have a custom post type in a child theme called information, I just copy single.php as information.php then add in functions.php

    /* Information Posts Template selection - a single.php just for our information posts */
     function dr_info_template_redirect() {
    	global $wp;
    	if ($wp->query_vars['post_type'] == 'information') {
    		if( file_exists( get_stylesheet_directory() . '/information.php') ) {
    			include( get_stylesheet_directory() . '/information.php');
    			die();
    		}
    	}
     }
    add_action("template_redirect", 'dr_info_template_redirect');

    HTH

    David

    In your example above, what is the name of file that you use to include information.php in?

    Hi,
    Creating the post type:
    I have a custom post type called information this is created with code in functions.php

    The template page:
    I open single.php and save this as information.php

    The content: (loop)
    If there is a loop-single.php or content-single.php file, I open and save this as loop-information.php or content-information.php

    Within these files I can add a body class, post class, or add any page specific changes like the post thumbnail etc:

    The code snippet above is also in the functions.php file, this tells WordPress to use information.php for the ['post type'] == 'information'

    Conclusion:

    The reason is because I will be styling this post type a certain way

    Looking again at the template heirarchy and your question, if it is a custom post type, just forget the code, open single.php and save it as single-{$posttype}.php (like: single-information.php)

    Do as above with the loop file if you are required to change the content layout.

    Then just add your styles classes to the new files.

    HTH

    David

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘add a post type exclusive to a page’ is closed to new replies.
Skip to toolbar