0

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!

3
  • 3
    You can answer (and accept) your own question instead of posting your answer right in the question :)
    – Anh Tran
    Sep 2, 2012 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. Sep 2, 2012 at 10:45
  • I think 24 hours has passed. :) Sep 14, 2012 at 4:12

1 Answer 1

1

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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