Jump to content

Recommended Posts

Posted

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? 

Posted

Hi Tyssen,

Each page in ProcessWire has its own (unique) url.

What do you mean with child-pages? Can you give an example how those pages are integrated in a parent page?

Cheers

Posted

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.

Posted

Page or Template?

Because you say subpage, but if it isnt a child page the word subpage is confusing,

How did you set up the Courses? as fields? as text? or as a child page?

Posted

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

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
  • Recently Browsing   0 members

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