WordPress.org

Forums

Custom Post Type UI
[resolved] Custom Post Type templates and custom directory locaiton (3 posts)

  1. lazy_entrepreneur
    Member
    Posted 1 year ago #

    To remove the clutter from the theme root directory and keep all custom design in a neat folder structure, i'm using the below code (in functions.php) to reference the custom templates for the custom post types.

    Basically it returns the root path of the theme, returns the Custom Post Type (for use like this localhost/mysite/mycustompost/), and does a check to see if it is an archive or single page.

    So long as the templates are named correctly (ie. single-mycustompost.php or archive-mycustompost.php), it should automatically select the correct template.

    Is there a better way of doing this, or have i missed anything that might cause and issue later?
    Is there a better way to set the location of the custom folder (/my-inc/my-templates/) than using the variable?

    Thanks in advance.

    // Custom Post Type Templates for single and archive pages
    // ----------------------------------------------------------------------
    add_filter('template_include', 'custom_template_include');
    function custom_template_include($template) {
    	$custom_template_location = '/my-inc/my-templates/';
            if ( is_archive() ) :
               if(file_exists(get_stylesheet_directory() . '/archive-' . get_query_var('post_type') . '.php'))
                  return get_stylesheet_directory() . '/archive-' . get_query_var('post_type') . '.php';
               return get_stylesheet_directory() . $custom_template_location . 'archive-' . get_query_var('post_type') . '.php';
            endif;
            if ( is_single() ) :
               if(file_exists(get_stylesheet_directory() . '/single-' . get_query_var('post_type') . '.php'))
                  return get_stylesheet_directory() . '/single-' . get_query_var('post_type') . '.php';
               return get_stylesheet_directory() . $custom_template_location . 'single-' . get_query_var('post_type') . '.php';
            endif;
        }
    	return $template;

    https://wordpress.org/plugins/custom-post-type-ui/

  2. lazy_entrepreneur
    Member
    Posted 1 year ago #

    Ok ... I've simplified the code. This now seems to work every time.
    If anyone is copying this, don't forget to go to:
    Settings -> Permalinks -> Common Settings ... set to "Post Names".

    // Selects Custom Post Type Templates for single and archive pages
    // ----------------------------------------------------------------------
    add_filter('template_include', 'custom_template_include');
    function custom_template_include($template) {
    	$custom_template_location = '/my-inc/my-templates/';
         if ( get_post_type () ) {
            if ( is_archive() ) :
               if(file_exists(get_stylesheet_directory() . $custom_template_location . 'archive-' . get_post_type() . '.php'))
                  return get_stylesheet_directory() . $custom_template_location . 'archive-' . get_post_type() . '.php';
            endif;
            if ( is_single() ) :
               if(file_exists(get_stylesheet_directory() . $custom_template_location . 'single-' . get_post_type() . '.php'))
                  return get_stylesheet_directory() . $custom_template_location . 'single-' . get_post_type() . '.php';
            endif;
        }
        return $template;
      }
  3. Michael Beckwith
    Member
    Plugin Author

    Posted 1 year ago #

    While not exclusive to CPT UI at all, thanks for this.

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • Custom Post Type UI
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic