Forum

Thread tagged as: Question, Meta

How to populate page title tag in a layout file from content in a template file?

Hi

I'm currently finishing off an estate agents website and was looking to include some of the content from the content template into the title tag in the layout page.

So for example the title tag will read...

<title>3 Bedroom House For Sale in Norwich only £199,995</title>

..with "3" "Norwich" and "199,995" coming from the content.

I currently have the following all up and running which I've taken from https://solutions.grabaperch.com/architecture/layout-variables

$title = perch_content(perch_get('s'), 'bedrooms', true) . ' Bedroom Property For Sale in xxxx only £xxx';
perch_layout ('forsale.head', array(
    'title'=>$title
));

...but the number of bedrooms isn't displaying.

Any help would be gratefully received.

Glen

Glen Piggott

Glen Piggott 0 points

  • 5 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

The issue is here:

perch_content(perch_get('s'), 'bedrooms', true)

The first argument to perch_content() is the region name. The second is an array of options for displaying that region.

I'm not clear what the arguments you're specifying are intended to do.

Hi Drew

Thanks for the speedy reply. Updated code...

perch_content(perch_get('Property Details'), 'bedrooms', true)

'Property Details' is the region

'bedrooms' is the ID of the content (which is a small text box which include a number eg 4)

Drew McLellan

Drew McLellan 2638 points
Perch Support

perch_get() is for reading parameters from the GET request - i.e. from the query string on the URL. You don't need that here.

To get a specific item from the region, you need to use skip-template with perch_content_custom();

$data = perch_content_custom('Property Details', [
     'skip-template' => true,
]);

$title = $data[0]['bedrooms'] . ' Bedroom Property For Sale in xxxx only £xxx';

Drew - you're a superstar!

How can I format the number in the price? It's currently outputting as 295000 when it needs to be 295,000

format="#:0" is in the content template if that's any help?

Glen

Drew McLellan

Drew McLellan 2638 points
Perch Support

You can use number_format():

$title = number_format($data[0]['bedrooms'],0) . ' Bedroom Property For Sale in xxxx only £xxx';