Jump to content

Search results for subpage entries


Tyssen
 Share

Recommended Posts

I'm using the following search template:

<?php

/**
 * Search template
 *
 */

$outMain = '';

if($q = $sanitizer->selectorValue($input->get->q)) {

	$input->whitelist('q', $q);

	$matches = $pages->find("title|body|sidebar~=$q, limit=50");

	$count = count($matches);

	if($count) {
		$outMain .= "<h1>Search results</h1>
		<p><strong>Found $count pages matching your query:</strong></p>" .
			"<ul class=\"nb\">";

		foreach($matches as $m) {
			$outMain .= "<li>
				<h2 class=\"h4\"><a href='{$m->url}'>{$m->title}</a></h2>
			</li>";
		}

		$outMain .= "</ul>";

	} else {
		$outMain .= "<h2>Sorry, no results were found.</h2>";
	}
} else {
	$outMain .= "<p>Please enter a search term in the search box (upper right corner)</p>";
}

include("./main.inc.php");

That works fine for pages that have their own URL but I have a bunch of child pages that don't have their own URLs, they're part of the parent page.

How do I modify the code above so that the search result links to the parent page when the entry has no URL of its own? 

Link to comment
Share on other sites

The pages are set up like:

General Courses

- Course 1

- Course 2

- Course 3

On the site, only a page for General Courses exists, all the individual courses are listed on that page, they don't have their own unique page that you can visit.

Link to comment
Share on other sites

If your course template has the fields "title|body|sidebar", they should be searched too. If these "sub-pages" are hidden, you need to add "include=hidden" to your selector string.

For the results:

foreach($matches as $m) {
  $outMain .= "<li>"
  $url = ($m->template == 'course') ? $m->parent->url : $m->url;
  $title = ($m->template == 'course') ? $m->parent->title : $m->title;
  <h2 class=\"h4\"><a href='{$url}'>{$title}</a></h2>
  </li>";
}

Note that the parent page may be listed multiple times with this code.

  • Like 2
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...