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.

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

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Creating page & post templates without template file

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.