ee-garage.com

Render default site meta

Rendering the default meta template and data to your template is as easy as adding {exp:nsm_better_meta:template} to your template. The default meta values can be overridden using the tag parameters.

Render entry specific meta

To output entry specific meta you must add either the entry_id or url_title parameter to the {exp:nsm_better_meta:template} tag.

Lucky for us it is a requirement of the ExpressionEngine templating system that either an entry_id or the url_title in the url for specific entry pages. Knowing this allows us to use segment variables as parameter values in the {exp:nsm_better_meta:template} tag.

Example 1: An entry id in the URL

URL: http://leevigraham.com/blog/post/3

<head>
  {exp:nsm_better_meta:template entry_id="{segment_3}"}
</head>

Example 2: A url title in the URL

URL: http://leevigraham.com/blog/post/my-post-url-title

<head>
  {exp:nsm_better_meta:template url_title="{segment_3}"}
</head>

Example 3: A unique page url

Pages have custom unique URLS assigned in the Pages module that don't contain an entry_id or url_title. In this case we need to be a little more creative.

URL: http://leevigraham.com/contact

{exp:channel:entries require_entry="yes"}
  {if no_results}{redirect="404"}{/if}
  <head>
    {exp:nsm_better_meta:template entry_id="{entry_id}"}
  </head>
  <body>
    <h1>{title}</h1>
{/exp:channel:entries}

    {-- Use dynamic="off" to dislay entries from a second channel --}
    <ol>
      {exp:channel:entries channel="some_other_blog" dynamic="off"}
        <li>{title}</li>
      {/exp:channel:entries}
    </ol>
  </body>

The URL above renders a single entry page after matching the page URL. The only way to get the entries ID is to wrap the {exp:nsm_better_meta:template} tag in an {exp:channel:entries} tag and explicitly pass the entries id.

Render homepage specific meta

In some cases the site homepage requires special treatment to ensure optimised SEO. This usually requires hiding the default site title and adding a more explicit title.

<head>
  {!-- Override the page title and hide the default site title --}
  {exp:nsm_better_meta:template
    title="Super amazing unique homepage title"
    hide_site_title = "y"
  }
</head>

Render category archive meta

Category meta requires a slightly different approach because there is no entry attached to the URL. In the example below we will output the meta for the "family pics" category archive using the tag.

URL: http://leevigraham.com/gallery/category-archive/category/family-pics

Template:

<head>
  {!-- Get the category information --}
  {exp:channel:category_heading channel="photo_blog"}
    {!--
      Overide the default page title with the category name.
      Add "Photo Gallery" to the page title using the title_suffix parameter
    --}
    {exp:nsm_better_meta:template
      title="{category_name}"
      title_suffix="Photo Gallery"
      description="{category_description}"
    }
  {/exp:channel:category_heading}
</head>

Output:

<head>
  <title>Family Pics | Photo Gallery | Site Title</title>
  <meta name="description" content="Pics from our family holiday" />
</head>

More information on {exp:channel:category_heading} the can be found in the ExpressionEngine documentation.

Using a global header embed

In the majority of site builds there is an embedded global header template embed. This template should include the {exp:nsm_better_meta:template} tag. The basic idea is that the tag parameters are passed through the header embed as embed parameters and then into the tag.

Example primary template

Using the URL title example above the template with an embedded header would look like:

<html>
  {embed="_partials/.header" entry_id="{segment_3}"}
</html>

Example header embed template

{exp:nsm_better_meta:template} will automatically inherit the embed params. IE: if the header embed has an entry_id parameter {exp:nsm_better_meta:template} will inherit it's value.

The inherit behavior means that tag parameters do not need to be explicitly defined.

The "_partials/.header" template:

<head>
  {exp:nsm_better_meta:template}
</head>

Render an XML sitemap

Firstly create a new template group and name it xml-sitemaps. Next, create a new template in the xml-sitemaps group called "blog" (or news or whatever your channel is) & set it's type to XML. Note: Double check the template type is XML or your sitemap will not render correctly.

In the "blog" template output the channel entries using the {exp:nsm_better_meta:entries_xml} tag.

You can submit a single sitemap to Google but the prefered method is to add each of the channel XML sitemap files to an sitemap index file.

Outputting channel entries

The recommended way of creating sitemap entries is by using the {exp:nsm_better_meta:entries_xml} tag. An example below renders 500 entries from the "blog" channel where the entry status is "not closed". The loc parameter value variables will be replaced with entry specific values.

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{exp:nsm_better_meta:entries_xml
  loc="http://test.com/blog/post/{entry_id}/{url_title}"
  status="not closed"
  channel="blog"
}
</urlset>

It's not a bad idea to include sitemap references to your channel category and monthly archive pages if they exist. This can easily be achieved using standard ExpressionEngine tags.

The example below outputs the category archive index pages for each category in the "blog" channel using the {exp:channel:categories} tag. Note that the <lastmod> (optional) isn't added to the output as there is no way to find this value.

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{exp:channel:categories
  disable="category_fields"
  show_empty="yes"
  style="linear"
  channel="blog"
}
  <url>
    <loc>http://test.com/blog/category-archive/cat/{category_url_title}/</loc>
    <changefreq>weekly</changefreq>
    <priority>0.5</priority>
  </url>
{/exp:channel:categories}
</urlset>

Complete example

Below is a complete example for the "blog" channel including index page, entries and category archive. We've hard coded the index page but you could just as easily use the {exp:channel:entries} tag to pull the last modified date.

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  {-- BLOG INDEX --}
  <url>
    <loc>http://test.com/blog/</loc>
    <lastmod>{current_time format="%Y-%m-%d"}</lastmod>
    <changefreq>always</changefreq>
    <priority>1.0</priority>
  </url>

  {-- BLOG ENTRIES --}
  {exp:nsm_better_meta:entries_xml
    loc="http://test.com/blog/post/{entry_id}/{url_title}"
    status="not closed"
    channel="blog"
  }

  {-- BLOG CATEGORY ARCHIVE PAGES --}
  {exp:channel:categories
    disable="category_fields"
    show_empty="yes"
    style="linear"
    channel="blog"
  }
    <url>
      <loc>http://test.com/blog/category-archive/cat/{category_url_title}/</loc>
      <changefreq>weekly</changefreq>
      <priority>0.5</priority>
    </url>
  {/exp:channel:categories}
</urlset>

If your sitemaps are getting too large, you may want to consider using Sitemap index files.

Submitting your sitemap to Google

Google doesn't like sitemaps to be located in your sites sub-folders. Removing the index.php file may possibly throw an error because http://mysite.com/index.php?xml_sitemaps/index/ will be redirected as http://mysite.com/xml_sitemaps/index/. For this reason it's important to submit the full URL to Google including the index.php.

Releases RSS Feed © Copyright 2017 | Policies All prices shown are in Australian Dollars ($AUD) Newism