WordPress.org

Ready to get started?Download WordPress

Forums

Request: custom post behaviours (or failing that - ability to force is_page) (3 posts)

  1. theWebalyst
    Member
    Posted 3 years ago #

    [Note: cross posted to "What Should 2011 Hold for WordPress?"]

    Using WP3.1RC-1 I've found custom post types to be far more limited in application than they need be.

    For example, there is as far as I can see no way to change the way a custom post type is treated from 'like a post' to 'like a page' unless you edit the theme. But since plugins are how you build new apps with this powerful new feature, it is severely limiting their usefulness and ease of application, if every such plugin has to have the website's theme tailored for it to work effectively.

    An example customisation would be for is_page() to be something that can be altered for a custom post type.

    Ideally WP would have a set of behaviours defined, each aligned to the standard types (post, page, archive etc.) and for the custom post type to be able to say: "treat me as if I am /like/ type y". So themes render me as if I am y, even though I am type x, and no theme has ever head of type x.

    A natural extension of this will be the ability to define custom behaviours (perhaps a better term would be "display pattern"?), in which case a theme can be written to do something particularly bespoke for this new kind of data object.

    Mark
    P.S. In the mean time, if anyone knows how to make a custom post type appear to a theme as if it were a page rather than a post PLEASE let me know! Its a major handicap to plugin writers until this can be done.

  2. Mark Barnes
    Member
    Posted 3 years ago #

    I 100% agree that you should be able to define whether custom post types are treated as post or pages by the theme. And, if 'hierarchical' is set to true, then they should be treated as pages by default.

    Nevertheless, after several hours, I managed to find two different work arounds that works with most themes. The first is to filter the code that decides what template to use. This makes sure the page uses the right template, but it doesn't actually set is_page() to true.

    add_filter ('template_include', 'sb_switch_template');
    
    function sb_switch_template ($template) {
    	global $post_type;
    	if ($post_type == 'sb_sermon')
    		return get_page_template();
    	else
    		return $template;
    }

    The second method is to actually change is_page(). This is a better method, but I'm not sure how it will work on 3.0 and below.

    add_action ('wp', 'sb_make_page');
    
    function sb_make_page() {
    	global $wp_query, $post_type;
    	if ($post_type == 'sb_sermon') {
    		$wp_query->is_page = true;
    		$wp_query->is_single = false;
    	}
    }

    You might be able to load the action slightly earlier (and not doing might confuse plugins that hook earlier than 'wp'), but this works fine normally.

  3. theWebalyst
    Member
    Posted 3 years ago #

    Mark, Thanks for following up my question. I've made a note to try your solution when I resume work on this plugin. Being able to move stuff out of the theme and into the plugin is very helpful.

    Thanks!

    Mark
    [sig moderated as per the Forum Rules]

Topic Closed

This topic has been closed to new replies.

About this Topic