Jump to content

Filter to generate a dynamic page heading for listings


Alejandro

Recommended Posts

JReviews offers the possibility of customizing listing page titles using dynamic values through the Listing Type SEO Manager. The page heading itself comes from whatever you set as the title for the listing, unless you are using the automated title generation feature found under Listing Type / Standard fields.

In cases where you might want to programmatically generate dynamic page headings, you can use the following filter combination. First it stores a copy of the original title, then it modifies the page heading. The stored copy of the original title needs to be restored in the second filter to be used by the dynamic Pate Title logic.

<?php

class JReviewsCustomHeadings {

	static $title;

	public function __construct()
	{
		Clickfwd\Hook\Filter::add('before_theme_render_viewvars', [$this, 'pageHeading']);
		
		Clickfwd\Hook\Filter::add('after_filter_output', [$this, 'pageTitle']);
	}

	public function pageHeading($viewVars, $params)
	{		
		if ($params['route'] == 'com_content.com_content_view') {
			/**
			 * You can easily replace a hardcoded value with a dynamic value coming from a custom field,
			 * or change the hardcoded value based on the listing type, category
			 */ 
			 // $postfix = $viewVars['listing']['Field']['pairs']['jr_titlepostfix']['value'][0] ?? '';
			$postfix = ' - with a postfix';

			static::$title = $viewVars['listing']['Listing']['title'];
			
			$viewVars['listing']['Listing']['title'] .= $postfix;
		}

		return $viewVars;
	}

	public function pageTitle($output, $params)
	{
		// Restore the listing title to original value to be used by Page Title logic for SEO
		if ($params['route'] == 'com_content.com_content_view') {
			$params['viewVars']['listing']['Listing']['title'] = static::$title;
		}

		return $output;
	}
}

new JReviewsCustomHeadings();

 

Link to comment
×
×
  • Create New...