Creating page & post templates without template file

Usually if you want to customise a post (generic: ie all) type in WordPress you would create a custom page template for it ie, following the template hierarchy, a page will be customised based on it's name and where it falls in that hierarchy. The further to the left in the following template graphic, the first it will get chosen. If I've ever needed to create a custom page format, that's what I've done.

With pages (and not posts) you can pick a "default" template that applies to the page. This usually relates to whether the page will have a sidebar or not, being that the default will have a sidebar and there will usually be another option for "full width".

I have recently used a plugin that has a nice feature of making each page unique in the front end editor. Something that I thought would only be possible through a custom page/post template file for that content.

That is to say, that without creating a custom post type, or template file, the plugin utilises some hook whereby the page format is custom altered for that post only.

I want to do something similar where I can dictate that the majority of my pages follow the standard format, but for some pages that are in some way or other identified I want that format changed and I'd prefer that to be by way of hook or filter, and done not through extra pages.

The changes are largely CSS based.

Ideally, I'd like to have a dropdown similar to the "default template" metabox that allows me to pick which one to use and be applied to that page.

For the sake of argument and example, the default would be unchanged, and another choice would change the max-width and background-color of the .col-ful class as well as an extra widget area between the content and the footer.

I know I could just create a custom page type and use it for those pages, but I want to still be able to utilise the custom naming principle of the hierarchy to allow for further customisation of the page if necessary. ie, I want it to behave on the outside of the content like the default template dropdown, but still allow customised content formats that the named page types allow. PLUS I want the template to be available across multiple post types, not just pages.

edit: I like the idea proposed by LSW-Mo in the comments, and initially I thought it would work, however, the property I need to modify is higher up in the .col-ful classed div (highlighted in the image). I want to override the max-width attribute which is just set way too low for my liking (visible higher in the right pane).

If anyone could suggest how to modify that in a manner as per described by LSW-Mo, that would be appreciated.

Topic page-template template-hierarchy Wordpress

Category Web


I ended up getting this working and it was much easier than I expected thanks to the comments by @LWS-Mo and @Milo.

In my page I add a custom field with name=add_class and key=extra-wide.

I then use code snippets plugin (I didn't want it in the theme's function.php since I toggle between the main theme and a child theme. Plus in code-snippets, I am able to selectively enable and disable different snippets). Using that plugin I added the code:

add_filter( 'body_class', 'add_extra_body_classes' );
function add_extra_body_classes( $classes ) {
    global $post;
    $my_post_meta = get_post_meta($post->ID, 'add_class', true);
    if ( ! empty ( $my_post_meta ) ) {
        return array( $my_post_meta );
        return array_merge( $classes, array( $my_post_meta ) );
    }
    return $classes;
}

Then in custom CSS I added:

.extra-wide #page #content .col-full{
    max-width: 95%;
}       

which allowed me to drill down to the first level change that I needed. From there it was just follow the bouncing ball and change the CSS to get what I wanted.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.