0

I have a plugin that creates a custom post type called Squeeze Pages. It has various short codes users can enter to make a squeeze page but I'm not sure how I can go about making the squeeze pages have a custom design, completely different from the homepage.

Is this possible? If so, how can it be done?

3 Answers 3

1

The code below retrieves the custom post type of the currently displayed content and then decides wheter to print on of your stylesheets. Everything happens depending on your page name. You could also var_dump($ctp_obj_name); or printr($ctp_obj_name); and look what else the custom post type object has to offer and decide what you'll use instead of the name.

The following should reside in your functions.php or in some file included from there:

add_action( 'init', 'print_squeeze_style' );
function print_squeeze_style() {
    define( 'YOUR_PATH', get_bloginfo('stylesheetpath').'/squeeze_css_dir/' ); // define css folder for squeeze here
    $post_type = 'your_custom_post_type'; // define custom post type name here

    // ID of post type currently displayed 
    $ctp_ID = get_the_ID();
        // retrieve the whole ctp object of the current "post"
        $ctp_obj = get_post_type( $ctp_ID );
            $ctp_obj_name = $ctp_obj->name; // get name
            $ctp_obj_type = $post->post_type; // type

    // file name depending on Custom Post Type Page Name
    if ( $ctp_obj_name == 'page name A' ) {
        $file = 'filename_a.css';
    }
    elseif ( $ctp_obj_name == 'page name B' ) {
        $file = 'filename_b.css';
    }
    else {
        $file = 'filename_default.css';
    }

    // Register the stylesheet for printing
    if ( post_type_exists( $post_type ) )
        wp_register_style( 'squeeze-css', YOUR_PATH.$file, false, '0.0', 'screen' );


    // print styles depending on Custom Post Type Page Name
    // Then output/print style in head
    if ( $ctp_obj_type == $post_type ) {
        add_action( 'wp_head', wp_print_styles( 'squeeze-css' ) );
    }
}

As stated below (please upvote previous Answers too), you'll need an filter on the body_class() function output to do stuff like body.squeeze div.wrapper div.post .some_element_class

add_filter( 'body_class', 'body_class_for_squeeze' );
function body_class_for_squeeze( $classes ) {
    $post_type = 'your_custom_post_type'; // define custom post type name here
    // ID of post type currently displayed 
    $ctp_ID = get_the_ID();
    // retrieve the whole ctp object of the current "post"
    $ctp_obj = get_post_type( $ctp_ID );
        $ctp_obj_name = $ctp_obj->name; // get name
        $ctp_obj_type = $post->post_type; // type

        // abort if we're not on a squeeze page
        if ( $ctp_obj_type !== $post_type )
            return;
            $classes[] .= 'squeeze';

    return $classes;
}
1
  • Just from having a brief look at my code: $ctp_obj_type = $post->post_type; should be $ctp_obj_type = $ctp_obj->post_type; both in the function and in the filter callback fn.
    – kaiser
    Feb 8, 2011 at 13:53
1

It is easier to do in theme, with native single-{post_type}.php template in hierarchy.

With plugin this is more tricky. My idea would be to to try filter single_template hook in get_single_template() with path to template file in your plugin's folder (don't forget to check for your post type).

4
  • Or if not the single template(ie. if the plugin is used in pages), then use this approach but tack onto page_template instead. Same solution, different hook(just to clarify Rarst's answer is usable in both cases).
    – t31os
    Feb 7, 2011 at 13:16
  • @t31os I am not entirely sure, but I think CPT are in is_single(), not is_page() templates branch?
    – Rarst
    Feb 7, 2011 at 14:20
  • I simply was saying that if the list of custom posts is being displayed on a page, this solution would still be workable, just using an alternative hook. ;)
    – t31os
    Feb 7, 2011 at 16:02
  • @t31os ah... yeah, until we get CPT archives in 3.1 *headdesk*
    – Rarst
    Feb 7, 2011 at 16:16
1

Your best bet is to create a secondary CSS sheet for the squeeze page. Then, include a function that adds a new body class and build your CSS off that.

Your Answer

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

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