[wp-hackers] Can I make a custom post type display with page.php rather than single.php

Nikola Nikolov nikolov.tmw at gmail.com
Sat Oct 19 16:23:00 UTC 2013


Yep - it's possible. Hook to the "single_template" filter - as described
here -
https://codex.wordpress.org/Plugin_API/Filter_Reference/single_template then
with the use of the locate_template() function -
http://codex.wordpress.org/Function_Reference/locate_template search for
the templates you want to use - something like this:

$found_template = locate_template( array( 'single-rsvpmaker.php',
'page.php' );

return $found_template ? $found_template : $template;

Where $template would be the value that is passed to your single_template
filtering function.

So to put it all together, it would look something like this:

function filter_rsvpmaker_template( $template ) {
    // Make sure that this is the template for an rsvpmaker post
    $found_template = 'rsvpmaker' == get_post_type() ? locate_template(
array( 'single-rsvpmaker.php', 'page.php' ) : false;

    // If we've found a template - return that instead of the default
template
    return $found_template ? $found_template : $template;
}
add_filter( 'single_template', 'filter_rsvpmaker_template' );

This might possibly have some side effects - the fact that you're loading a
page template instead of a single post template, but I'm not a 100% sure.


On Sat, Oct 19, 2013 at 5:20 PM, David F. Carr <david at carrcommunications.com
> wrote:

> I'd like to make my RSVPMaker event scheduling plugin a little more plug
> and play, and it just occurred to me there might be a simpler way of
> getting my event post type to display better.
>
> I've been telling people to create a single-rsvpmaker.php template and add
> it to their theme in order to avoid confusion between the event date
> displayed in the body of the post and the post publication date that's
> prominently displayed in many themes. In the absence of a
> single-customtype.php template, WordPress normally uses the theme's
> single.php or equivalent.
>
> It occurs to me that most themes do not display the publication date as
> prominently in the page template. So the behavior I'd like to achieve would
> be to modify the choice of template to be:
>
> First choice: single-rsvpmaker.php (if there is one)
> Second choice: page.php
> Third choice: default WordPress behavior.
>
> That way, the user would only have to create a custom template file if
> page.php is not appropriate. More people who don't feel comfortable mucking
> around in code would get the desired behavior out of the box.
>
> Is this doable? If so, can you give pointers on how to accomplish it?
>
> --
> David F. Carr
> Author, Social Collaboration for Dummies
> http://www.wiley.com/buy/9781118658543
> LinkedIn - http://www.linkedin.com/in/davidfcarr
> Facebook - http://www.facebook.com/carrcomm
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>


More information about the wp-hackers mailing list