Tip: use _permalinks to generate your sitemap

9 posts by 4 authors in: Forums > CMS Builder
Last Post: January 13, 2017   (RSS)

By mizrahi - January 10, 2017

If you're using the permalinks plugin, you can use this table to very quickly generate the list of URLs for a sitemap file. The only catch is that you need to filter out any of those that are marked as "OLD". 

1. Setup a file named sitemap.php with this code...

<?php
  header('Content-type: application/xml');
  require_once('admin/lib/viewer_functions.php');
 
   list($permalinksRecords, $permalinksMetaData) = getRecords(array(
    'tableName'   => '_permalinks',
    'loadUploads' => false,
    'allowSearch' => false,
    "where" => "!old",
  ));
  
  $base_url = "http://www.yoursite.com/";

  $output = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
  $output .= '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";
  echo $output;
?>
  <url>
    <loc><?php echo $base_url; ?></loc>
  </url>
  <?php foreach ($permalinksRecords as $record): ?>
    <url>
      <loc><?php echo $base_url; ?><?php echo $record['permalink'] ?></loc>
    </url>
  <?php endforeach ?>
</urlset>

2. and in your robots.txt, add this line...

Sitemap: http://www.yoursite.com/sitemap.php

By ross - January 10, 2017

Hi there.

Thanks for posting. This looks like a great tip.

Anyone who gives it shot, post the results :).

Thanks!

-----------------------------------------------------------
Cheers,
Ross Fairbairn - Consulting
consulting@interactivetools.com

Hire me! Save time by getting our experts to help with your project.
Template changes, advanced features, full integration, whatever you
need. Whether you need one hour or fifty, get it done fast with
Priority Consulting: http://www.interactivetools.com/consulting/

By Steve99 - January 11, 2017

We've been using the permalinks database to create xml sitemaps in a very similar fashion as this, and save ours as sitemap.xml.php and reference in robots.txt file.

In addition, we add the cmsb special field "hidden" to toggle sitemap visibility where needed (dev pages, etc.).

Steve

By Toledoh - January 11, 2017

I was using the permalinks table to generate the sitemap, but started running into problems at times, especially since I use a category section to layout the structure of my site, I ended up with permalinks to pages that didn't really exist (parent categories with no content). Managing that by hiding records was a bit of a hassle, and I don't give clients access to that admin data anyway.

So I now generate the sitemap directly from the category menu which normally runs my entire site navigation, using the permalinks field.  I can not show the record if it has a child, or hidden etc.  I then add in additional section views for more modular / list type sections such as blog items and products.

Cheers,

Tim (toledoh.com.au)

By Steve99 - January 12, 2017

Hey Tim,

Your described approach is how we almost always handle our xml sitemap generation, as the majority of our sites are also managed in the same dynamic fashion via category menu. The method described by mizrahi does work fine for sites built with single record editors though.

For parent level categories without content, we've been using a checkbox field "is_category_label_only". If that is ticked, then we filter it from being output to the sitemap.

On a side note, we also use that field in handling our dynamically created bootstrap main site navigation. For example, we'll use $navLink in the href -- setting it to # if it's a parent category without content, or setting it to $categoryRecord['_link'] if it's a real page.

Cheers,
Steve

By Toledoh - January 12, 2017

Yep!  And similarly, sometimes I want to break out of the category page listing, so I have a field "redirect".  If I enter a path in that field, the navigation text is controlled, but I use the redirect path for the link.

(Wouldn't it be great to one day organise a CMSB conference - a 2 day sit-in to look at how we all approach different challenges.)

Cheers,

Tim (toledoh.com.au)

By Steve99 - January 13, 2017

The "redirect" field is a good idea.

We've been using an Admin Access Only field called "custom_include" to include separate php files for custom page views (modular / list type sections).

For example, News Listing would be handled by a separate multi-record editor - we'd put all the News Listings code/layout on it's own php include file called "news-listings-view.php" and save that custom page views file name in the "custom_include" field. Then for field validation we use a modified version of the code block CMSB uses for checking Viewer Urls.

By Toledoh - January 13, 2017

Similar again!  I create a list of "includes" that are available via a select box, then use;

<?php if ($selectedCategory['include']==1) { include("...");} ?>
<?php if ($selectedCategory['include']==2) { include("...");} ?>

Each of the includes I have in a folder I include in any site full of standard "things" I include such as email forms, various layouts of list pages etc.

Cheers,

Tim (toledoh.com.au)