Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm writing a squeeze page plugin and I've hit a bump. The plugin needs to be able to display custom post types with a custom template and also be able to be used for the homepage.

So I've got the code to set a template for the custom post type:

add_filter('single_template', 'my_custom_template');

function my_custom_template($single) {
global $wp_query, $post;

if ($post->post_type == "squeeze_page"){
    if(file_exists(ABSPATH . '/wp-content/plugins/wpsqueezr/squeeze_page.php'))
        return ABSPATH . '/wp-content/plugins/wpsqueezr/squeeze_page.php';
}
return $single;
}

That works fine for displaying the custom post type. Adding it to the Reading settings works:

add_filter( 'get_pages',  'add_my_cpt' );

function add_my_cpt( $pages )
{
     $my_cpt_pages = new WP_Query( array( 'post_type' => 'squeeze_page' ) );
     if ( $my_cpt_pages->post_count > 0 )
     {
         $pages = array_merge( $pages, $my_cpt_pages->posts );
     }
     return $pages;
}

Now here's where it gets messy. That all works fine, but if you set the custom post type to the homepage, it redirects to the custom post type page, no good.

I found this code which adds the custom post type to the frontpage, but it doesn't show the custom template I've setup for single pages:

function enable_front_page_stacks( $query ){
    if(( ! isset($query->query_vars['post_type']) || '' == $query->query_vars['post_type']) && 0 != $query->query_vars['page_id'])
        $query->query_vars['post_type'] = array( 'page', 'squeeze_page' );
}
add_action( 'pre_get_posts', 'enable_front_page_stacks' );

I need to show the custom template for the custom post type on the homepage, not the custom post type in the homepage template. I tried the home_template filter but that didn't do anything.

Any ideas?

Edit: Worked it out, just add a page_template filter as well as a single_template filter. I hope this helps someone else! Cheers!

share|improve this question
2  
You can answer (and accept) your own question instead of posting your answer right in the question :) –  Rilwis Sep 2 '12 at 7:07
    
Actually you have to have a certain amount of reputation before you can answer your own questions within 24 hours or something. –  Damian Caynes Sep 2 '12 at 10:45
    
I think 24 hours has passed. :) –  Brian Fegter Sep 14 '12 at 4:12
add comment

1 Answer

Forgot about this post.

Worked it out, just add a page_template filter as well as a single_template filter. I hope this helps someone else! Cheers!

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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