Jump to content

New Header.inc based on Parent Page


louisstephens
 Share

Recommended Posts

So I have set up a pretty basic template for pages using: 

<?php include('header.inc'); ?>

and page structure is something like mydomain.com/whales/page-name

I was hoping to have a different header.inc for every group of pages (so based on the parent page include the relevant header.inc).

For example, /whales/ would have a different header than /goldfish/ (sorry for the silly examples).

However, I can not for the life of me find a solution, nor am I having any luck in creating the desired outcome. Has anyone done anything like this before? Am I approaching this is the wrong way?

Link to comment
Share on other sites

Not sure I follow but if your header.inc files are each named similar to the parent pages, e.g. whales.inc for /whales/, goldfish.inc for /goldfish/, then something like the following should work.  Add the code to the template file of the child pages (i.e. the template file used by the children of 'whales', 'goldfish', etc..

include ($page->parent->name . ".inc");

Ideally, you also need to deal with what should happen if PHP does not find the specified include file.

Welcome to the forums :-)

Edited by kongondo
  • Like 1
Link to comment
Share on other sites

Thanks kongondo! I am falling in love with process wire ( long time user of Wordpress). I was kind of hoping to keep one or two templates to use and just switch the header ( as they will all use the same fields etc etc and the number there will potentially be 25 different versions of one template header) I am sorry, I should have clarified in my original post.

  • Like 1
Link to comment
Share on other sites

No worries...I was just about to post to ask for clarification  (i.e. in cases where there are lots of headers to include)....

OK, just to get this straight...

  • You have 25 different headers?
  • You do not wish to use 25 different xxxx.inc files :-)
  • What are the differences between the headers? Markup or content (text)?

I ask because there are different approaches. This could either be done template file-side or pages-side. E.g. different header content could be stored as pages and rendered as headers. This topic might also be of interest: https://processwire.com/talk/topic/740-a-different-way-of-using-templates-delegate-approach/

  • Like 1
Link to comment
Share on other sites

Thanks again Kongondo! 

I finally figured out a way to do this (might not be the best).

<?php
  $urlGrab = explode( '/', $page->url);echo $urlGrab[2]; 
  $newHeader = $urlGrab[2];

     switch ($newHeader) {
	case "whale":
	      include( './inc/headerImage.inc');
	break;
	case "guppie":
	      include( './inc/headerImage2.inc');
	break;
	default:
	      include( './inc/headerImage.inc');
      }
?>

Is there a better way to create the desired effect?

Link to comment
Share on other sites

Although it works, seems a bit strange that you are exploding the page's URL string to get (I suppose) the name of the parent page. ProcessWire already has methods and properties to get you all the information you need about a page (similar to my example above). What might be a 'better' way depends really on the answer to the questions:

  • What is the content of headerImage.inc (is it just an image?)
  • Is that content dynamic in itself (i.e. will you be editing headerImage.inc) or static?

Otherwise, like I said, there could be a dozen (better :-)) ways to achieve the desired effect. You could still get away with one or two templates. Depending on the content of headerImage.inc you could have for example a single page with all the header.incs as fields, a single image field that holds multiple images, several pages, etc. Hard to answer without answers to my questions above :-)...

  • Like 1
Link to comment
Share on other sites

Actually, the content of headerImage.inc (which will changing to just contentheader.inc) will contain a different header piece (image and a brief description) that will be displayed underneath the actual website header (logo, navigation).

As for the second question, the content could be static, but I would like to keep the option open to have it dynamic just in case the information in that section needs to change.

Keeping in line with my examples, "Whale" will have 10 subpages and will use one of the contentheaders. However, "Guppie" will be identical content with a different contentheader. I guess a better way to explain this would be : "I wish to "brand" each set of pages differently, even though they have the same content".

Sorry if I made this more confusing than it probably is. 

Link to comment
Share on other sites

That's a better description...Like @tpr has suggested, this would be better managed on the admin side rather than in an include file for at least two reasons:

1. Your images will have a description - no need to type that text in some template file where your client/editors will not be able to change it

2. Dynamism...in case needed in the future.

Here are some options:

  1. Option 1: Create an image field (e.g. call it content_header). Attach this image to the template used by your parent pages (Whale, Guppie, etc). I am assuming all these parent pages have a single shared template (please note, I am talking about a template here; not a template file). Image fields have a description text area. Use that for your 'brief descriptions'. content_header->description will give you access to those descriptions. More here on image fields. Then on the children pages' template file(s) call on their parents' image field and description...e.g. $page->parent->content_header->description
  2. Option 2: Create a dedicated page to hold all the content headers and their descriptions. Let's call it 'Content Headers'. Add one image field to it that accepts multiple images. Add your '25' images named similar to your parent pages, e.g. whale.jpg, guppie.jpg, etc (if practical, or use some other naming format). I can't remember if you can use the API to find an image using its name (i.e. $image->basename). Anyway, you can activate the 'tag' text area of an image field (in the field's settings). In that case, no need to give your images any specific name. Then, give each image a tag that is identical to your parent pages names or titles. You can find each specific image using their tag, i.e., $page->images->getTag('mytag'); Or, in your case you refer to the dedicated page, e.g. $p = $pages->get('name=content-headers'); $chImage = $p->content_header->getTag("$page->parent->name"); If you gave the whale header image a tag called 'whale', then, when viewing Whale's child pages, this image will be retrieved. The advantage here is that you can edit all your content headers in one place.
  3. Option 3: If your descriptions are quite long, then, create a dedicated text field(s) and a separate image field using either of the above options
  4. Option 4: I think you could also use repeaters or PageTable(?)

Haven't tested the above code and I have been away a while, so, there could be other better ways to accomplish what you are after.

Edited by kongondo
  • Like 5
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...