Answers.Body: <p>Completely untested, but something like this should work:</p>
  
  <pre><code>public function getPageUrl($slug)
  {
      $criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
      $criteria-&gt;slug = $slug;
      $entry = $criteria-&gt;first();
  
      if ($entry)
      {
          return UrlHelper::getUrl($entry-&gt;uri);
      }
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-19T10:44:27.243Z
Answers.Text: Thanks again Brad, that's the second time you've pointed me in the direction of the Helpers. http://buildwithcraft.com/classreference#craft.app.helpers
Answers.UserDisplayName: Mark Notton
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-07-14T13:05:59.363Z
Answers.Text: You can even simplify that by just invoking the `getUrl()` method on the `$entry` object. There's no need for calling the `UrlHelper` (the `getUrl()` method is already using it). So, you can shorten the last line to this:
  `return $entry->getUrl();`
Answers.UserDisplayName: Arvid
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-11-18T18:40:16.277Z
Answers.LastActivityDate: 2014-11-18T18:40:16.277Z
Answers.UserDisplayName: Brad Bell
Body: <p>Wondered what the best approach would be to get the url for a given entry page using only a slug for reference? I imagined it being something like this:</p>
  
  <pre><code> public function getPageURL($slug) {
      $page = craft()-&gt;elements-&gt;slug($slug);
      @return getURL($page);
    }
  </code></pre>
  
  <p>Usage example:</p>
  
  <pre><code>&lt;a href="{{ craft.PLUGINNAME.getPageURL("staff") }}"&gt;Staff&lt;/a&gt;
  </code></pre>
  
Tags: <plugin-development><elementcriteriamodel><slug>
Title: Get a page URL using only the Slug?
ThreadText: # Get a page URL using only the Slug?
  
  (Asked by: username_0 on 2014-11-18)
  
  Wondered what the best approach would be to get the url for a given entry page using only a slug for reference? I imagined it being something like this:
  
  ```
   public function getPageURL($slug) {
      $page = craft()->elements->slug($slug);
      @return getURL($page);
    }
  
  ```
  
  Usage example:
  
  ```
  <a href="{{ craft.PLUGINNAME.getPageURL("staff") }}">Staff</a>
  
  ```
  
  **Tags:** plugin-development, elementcriteriamodel, slug
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2014-11-18. Score: 2 | Accepted answer
  
  Completely untested, but something like this should work:
  
  ```
  public function getPageUrl($slug)
  {
      $criteria = craft()->elements->getCriteria(ElementType::Entry);
      $criteria->slug = $slug;
      $entry = $criteria->first();
  
      if ($entry)
      {
          return UrlHelper::getUrl($entry->uri);
      }
  }
  
  ```
  
  > **Comments:**
  > **username_0**: Thanks again username_1, that's the second time you've pointed me in the direction of the Helpers. http://buildwithcraft.com/classreference#craft.app.helpers  
  > **username_2**: You can even simplify that by just invoking the `getUrl()` method on the `$entry` object. There's no need for calling the `UrlHelper` (the `getUrl()` method is already using it). So, you can shorten the last line to this: `return $entry->getUrl();`  
  
  ---
  
  