0

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.

WordPress template hierarchy

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.

enter image description here

4
  • You said "The changes are largely CSS based.". So, you could create a custom field (for example a select-field). The different options of this field could represent CSS class-names. Than you could use an add_filter call to post_class to add the saved/selected classname to the post/post-markup. Codex Link to post_class. After that you just need to write some custom CSS code, as your post-markup should look like <article id="post-14" class="post-14 post type-post status-publish hentry mycustomclassforstyling">.
    – LWS-Mo
    Commented Sep 11, 2017 at 13:58
  • That does sound like the sort of approach I'm after but it'll have to work on a lower level class though. I need it to override the .col-ful class which sets the overall page width in the wooCommerce theme.
    – Madivad
    Commented Sep 12, 2017 at 1:02
  • use body_class instead of post_class
    – Milo
    Commented Sep 12, 2017 at 4:42
  • @LWS-Mo if you want to create an answer with your solution I can mark it as accepted. Using your solution with the body_class instead of post_class I was able to get it working.
    – Madivad
    Commented Sep 12, 2017 at 12:33

1 Answer 1

1

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.

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.