What are the methods for search?

Post Reply
User avatar
cobber
Master Bludit
Posts: 78
Joined: Sun Feb 28, 2016 10:15 am
Location: Scotland
Has thanked: 21 times
Been thanked: 5 times

Been trying to get search working and want to know what methods exist to output the results of the search.

In

Code: Select all

$page 
we have

Code: Select all

$page->title()
etc.

I have the search page appearing but am unsure how to output the results. I have looked at the plugin code and can't see anything.

For example on the search landing page I would like to output the results of the search. I assume the results will be an object which I can iterate through.

Was hoping for some input
User avatar
cobber
Master Bludit
Posts: 78
Joined: Sun Feb 28, 2016 10:15 am
Location: Scotland
Has thanked: 21 times
Been thanked: 5 times

Anyone have an idea?
User avatar
multicolordev
Master Bludit
Posts: 137
Joined: Thu May 26, 2022 12:33 pm
Has thanked: 15 times
Been thanked: 91 times

you want search from input and give results title with link yeah?
Image
User avatar
cobber
Master Bludit
Posts: 78
Joined: Sun Feb 28, 2016 10:15 am
Location: Scotland
Has thanked: 21 times
Been thanked: 5 times

For anyone interested in the future. . .

I made a theme for myself and wanted to add the search but for some reason it failed to work. The results either wouldn't appear or would cause an error and break the page.

In my own index.php code, I made a conditional to use particular templates. (this means the user can select a template of choice when creating a new page).

Then in the index, we run the conditional to check if a template is being used. (Notice I made a specific check for search).

Code: Select all

    if($WHERE_AM_I == "search"){
        include(THEME_DIR_PHP.'search.php');
    }
    else if ($page->custom('template')) {   
 
            // If a custom template is specified 
            if ($WHERE_AM_I=='page') {
                include(THEME_DIR_PHP.$page->custom('template').'.php');
            } else {
                include(THEME_DIR_PHP.'home.php');
            }   
         
    } else {    
           // If not
            if ($WHERE_AM_I=='page') {
                include(THEME_DIR_PHP.'page.php');
            } else {
                include(THEME_DIR_PHP.'home.php');
            }    
     
    } 
The solution was so simple, I could kick myself. It was staring me in the face all this time , in plain sight with in the documentation. Within the 'search' template I made, I added the following code to obtain the search results.

Code: Select all

<?php
                global $content;

                foreach ($content as $page) {
                    echo "<h2><a href='".$page->permalink()."'>".$page->title()."</a></h2>";
                    echo "<p>".$page->description()."</p>";
                }
 ?>
 
Anyway, hope this helps someone in the future.
Post Reply