Jump to:

23490 Posts in 18996 Topics by 2878 members

General Questions

SilverStripe Forums » General Questions » MetaDescription in search results

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1 2
Go to End
Author Topic: 2290 Views
  • DeklinKelly
    Avatar
    Community Member
    197 Posts

    MetaDescription in search results Link to this post

    I want to show the Meta Description in search results. I am using the standard search form on Silverstripe 2.4.

    This code does NOT display the $MetaDescription and I think the problem is that the built-in search functionality does NOT return MetaDescription along with other SiteTree objects.

    How can I get the $MetaDescription in these results?

    Here is my template code:

    <h1>Search Results</h1>
    <div class="typography">
       <% if Results %>
        <ul id="SearchResults">
        <% control Results %>
        <li>
        <% if MenuTitle %>
        <h3><a class="searchResultHeader" href="$Link">$MenuTitle</a></h3>
        <% else %>
        <h3><a class="searchResultHeader" href="$Link">$Title</a></h3>
        <% end_if %>
              <% if MetaDescription %>
                <p>$MetaDescription</p>
              <% end_if %>
        <a class="readMoreLink" href="$Link" title="{$Title}">Click Here</a>
        </li>
        <% end_control %>
        </ul>
        <% else %>
        <p>Sorry, your search query did not return any results.</p>
        <% end_if %>

        <% if Results.MoreThanOnePage %>
        <div id="PageNumbers">
        <% if Results.NotLastPage %>
        <a class="next" href="$Results.NextLink" title="View the next page">Next</a>
        <% end_if %>
        <% if Results.NotFirstPage %>
        <a class="prev" href="$Results.PrevLink" title="View the previous page">Prev</a>
        <% end_if %>
        <span>
        <% control Results.SummaryPagination(10) %>
        <% if CurrentBool %>
        $PageNum
        <% else %>
        <a href="$Link" title="View page number $PageNum">$PageNum</a>
        <% end_if %>
        <% end_control %>
        </span>

        </div>
        <% end_if %>
    </div>

    Thanks!

  • ryanwachtl
    Avatar
    Community Member
    46 Posts

    Re: MetaDescription in search results Link to this post

    Hi DeklinKelly,

    This Topic should help you out: http://www.silverstripe.org/template-questions/show/276854

    - Ryan

  • DeklinKelly
    Avatar
    Community Member
    197 Posts

    Re: MetaDescription in search results Link to this post

    Thanks. But how do I use this code?

    What function or class should I put in mysite/Page.php to make this work?

    <?php
    class CustomSearch extends SearchForm
    {
       
       protected $classesToSearch = array("SiteTree");
       
       public function searchEngine($keywords, $pageLength = null, $sortBy = "Relevance DESC", $extraFilter = "", $booleanSearch = false, $alternativeFileFilter = "", $invertedMatch = false) {
          if(!$pageLength) $pageLength = $this->pageLength;
           $keywords = addslashes($keywords);
       
          $extraFilters = array('SiteTree' => '');
           
           if($booleanSearch) $boolean = "IN BOOLEAN MODE";
           if($extraFilter) {
              $extraFilters['SiteTree'] = " AND $extraFilter";
           }
           
           if($this->showInSearchTurnOn)   $extraFilters['SiteTree'] .= " AND showInSearch <> 0";

          $start = isset($_GET['start']) ? (int)$_GET['start'] : 0;
          $limit = $start . ", " . (int) $pageLength;
          
          $notMatch = $invertedMatch ? "NOT " : "";
          if($keywords) {
             $match['SiteTree'] = "MATCH (Title, MenuTitle, Content, MetaTitle, MetaDescription, MetaKeywords) AGAINST ('$keywords' $boolean)";
          
             // We make the relevance search by converting a boolean mode search into a normal one
             $relevanceKeywords = str_replace(array('*','+','-'),'',$keywords);
             $relevance['SiteTree'] = "MATCH (Title) AGAINST ('$relevanceKeywords') + MATCH (Title, MenuTitle, Content, MetaTitle, MetaDescription, MetaKeywords) AGAINST ('$relevanceKeywords')";
          } else {
             $relevance['SiteTree'] = 1;
             $match['SiteTree'] = "1 = 1";
          }

          // Generate initial queries and base table names
          $baseClasses = array('SiteTree' => '');
          foreach($this->classesToSearch as $class) {
             $queries[$class] = singleton($class)->extendedSQL($notMatch . $match[$class] . $extraFilters[$class], "");
             $baseClasses[$class] = reset($queries[$class]->from);
          }
          
          // Make column selection lists
          $select = array(
             'SiteTree' => array("ClassName","$baseClasses[SiteTree].ID","ParentID","MetaDescription","Title","URLSegment","Content","LastEdited","Created","_utf8'' AS Filename", "_utf8'' AS Name", "$relevance[SiteTree] AS Relevance", "CanViewType"),
          );
          
          // Process queries
          foreach($this->classesToSearch as $class) {
             // There's no need to do all that joining
             $queries[$class]->from = array(str_replace('`','',$baseClasses[$class]) => $baseClasses[$class]);
             $queries[$class]->select = $select[$class];
             $queries[$class]->orderby = null;
          }

          // Combine queries
          $querySQLs = array();
          $totalCount = 0;
          foreach($queries as $query) {
             $querySQLs[] = $query->sql();
             $totalCount += $query->unlimitedRowCount();
          }
          $fullQuery = implode(" UNION ", $querySQLs) . " ORDER BY $sortBy LIMIT $limit";

          // Get records
          $records = DB::query($fullQuery);

          foreach($records as $record)
             $objects[] = new $record['ClassName']($record);

          if(isset($objects)) $doSet = new DataObjectSet($objects);
          else $doSet = new DataObjectSet();
          
          $doSet->setPageLimits($start, $pageLength, $totalCount);

          return $doSet;
       }
    }

    ?>

  • ryanwachtl
    Avatar
    Community Member
    46 Posts
  • DeklinKelly
    Avatar
    Community Member
    197 Posts

    Re: MetaDescription in search results Link to this post

    Thanks, the directions say to replace a line, but I don't have that line to replace.

  • ryanwachtl
    Avatar
    Community Member
    46 Posts

    Re: MetaDescription in search results Link to this post

    What version of SS are you using? What does your current Page.php look like?

  • DeklinKelly
    Avatar
    Community Member
    197 Posts

    Re: MetaDescription in search results Link to this post

    I use SilverStripe version 2.4.

    Here is my Page.php

    <?php
    class Page extends SiteTree {

       public static $db = array(
       );

       public static $has_one = array(
       );

    }
    class Page_Controller extends ContentController {

       public static $allowed_actions = array (
       );

       public function init() {
          parent::init();

       }
    }

  • ryanwachtl
    Avatar
    Community Member
    46 Posts

    Re: MetaDescription in search results Link to this post

    Sorry for the confusion looks like that Topic doesn't cover 2.4

    Here is a quick and dirty way to get your MetaDescription in your template. It will return the Meta Description for a Page if it exists or is not empty, if not it will return false and your template will fall back to the default display.

    in Page.php

    ...
    public static $has_one = array(
    );

    function getSearchMetaDescription() {
       
    if ( $data = DataObject::get_by_id("Page", $this->ID) ) {
    if ( $data->MetaDescription != "" ) { return $data->MetaDescription; }
    else { return false; }
    }
    else { return false; }

    }
    ...

    then update template Page_results.ss:

    replace:

    $Content.LimitWordCountXML

    with:

    <% if SearchMetaDescription %>
    <p>$SearchMetaDescription</p>
    <% else %>
    <p>$Content.LimitWordCountXML</p>
    <% end_if %>

    2290 Views
Page: 1 2
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.