I've got (currently) 3 custom post types that I need to display on my sites' home page using a custom query. Each custom post type has a different layout (1 post across, 2 posts across and 4 posts across). What I am trying to do is call a different template for each layout.

I've been working on this close to a month and can't figure it out. Any help would be appreciated.

I'm using the following code, and it isn't working:

<?php if ( 'one_column' == get_post_type() ) { include 'inc-onecolumn.php'; } elseif ( 'two_column' == get_post_type() ) { include 'inc-twocolumn.php'; } elseif ( 'four_column' == get_post_type() ) { include 'inc-fourcolumn.php'; } else { echo 'Hello!' } ?>

I've created a couple of test posts but nothing is showing up on the homepage except 'Hello!'.

First, am I on the right path?

Second, if I am on the right path, is it the following code that's incorrect (it's my template for 1 across)?

Template code: http://pastebin.com/5iGiRFPF

  • If you're specifically querying for a post_type, why if ifs? And must you include the files, can't you query with get_posts for one_column, display it, then query for two_column, display and so on.
    – Shane
    Feb 9 '12 at 19:44
  • I'm trying to figure out a way to have the client pick where a specific layout on the page can go... I don't know if the above will accomplish that or not. Probably not. Feb 9 '12 at 21:14
  • Why don't you use a custom fields for the layout option?
    – ifdion
    Feb 23 '12 at 1:08

There are a couple of confusing points to the question, namely that the home page is usually reserved for a stream of posts (where you could at one time trigger a 4 posts across layout, not necessarily all of them of the same post type), or a static post / page, where a such a check would be meaningless.

Still, I think you're better off thinking in terms of Template Hierarchy, rather than checking for post type via functions. As in, creating actual files that are automatically loaded by WordPress without the need of additional code.

For example, put the following files in your template's root directory: single-one_column.php, single-two_column.php, single-four_column.php, each of them containing or requesting the code within the files your trying to include, and WordPress will load them every time a user requests a post of that particular post type.

But since you're trying to load more than one post at a time, maybe you need archive templates, which are also available for post types. It works the same way, i.e. archive-one_column.php, archive-two_column.php and archive-four_column.php, where you will be able to control the number of posts displayed and the layout when (assuming you have pretty permalinks) a user visits yoursite.com/one_column, etc.

If you do need everything to happen in that one page, keep in mind that the get_post_type() function without any parameters needs to happen inside the loop in order to work. It's hard to say without seeing the complete page, but the problem might be that your trying to load your custom templates (where you have your custom loops) based on information that WP hasn't yet processed.

But if you're going to have queries that specifically request one post type, why would you need to use get_post_type() in the first place?

  • The issue is that we're using this more of a CMS than a blog. And we want to have different sections/categories on the homepage. So, these would be loading on yoursite.com. Imagine a homepage that has a slider, then a category called "activities" and under activities there are 4 posts and then under activities you have a section called "movies" and under movies there is only one post. Each of these have different image sizes associated with them and the 4 posts would be inline list items. Does that describe what I'm trying to accomplish better? Feb 9 '12 at 21:10
  • I've edited the answer. Feb 9 '12 at 23:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.