Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lokothodida/7e696f945bf5aced3884 to your computer and use it in GitHub Desktop.
Save lokothodida/7e696f945bf5aced3884 to your computer and use it in GitHub Desktop.
GetSimple tutorial for archiving pages (in news/blogging fashion) with i18n.

One feature that I thought was missing from the i18n News system is that of providing automatic archives for news results. However, such a feat is actually achievable with the current i18n Search plugin if you take a moment to think outside of the box...

Plugins you'll need

The importance of tags

Tags are the lifeblood of i18n Search. They are the key to filtering out your results to output exactly the kind of content that you are looking for and ordering them in a sensible way. Initially, tags were simply there as key-words to be involved in the “meta” information of the page – words that would help your site/page be indexed by search engines and found by users. With i18n Search however, these tags can be used to set up virtual categories and a filtering system, provided that you can maintain some kind of logical structure and consistency with the tags that you utilise.

Ideally, one should think of the possible tags that their content could utilise to understand how many ways their content could be organised. Consider your news article for example. What ways could that type of content be organised by?

  • Predefined category (e.g. sports, web, media, general...)
  • Author
  • Year
  • Month
  • Day

If you want your content to be easily output in either of those orders, consider a tag for each of those methods:

  • Category: _catname (e.g _sports)
  • Author: _author (e.g. _johnsmith)
  • Year: _year (e.g. _2012)
  • Month: _month (e.g. _jun)
  • Day: _day (e.g. _fri)

The underscore (_) is there to prevent the tag showing up in any tag outputs we want to actually show on the page. So basically we can use these tags without the end user needing to actually see them (since they might be unappealing/useless for them to see on their own).

With these notions in mind, we can actually output results that have been refined thanks to the new tags created. If we wanted to only show the sports articles published in 2012 by John Smith, we could use this placeholder on the news page:

(% searchresults tags='_sports _johnsmith _2012' %)

Ensuring that the tags are separated only by spaces.

Setting up the archive system

Understanding the importance of tags will enable us to develop a functioning archive system that doesn't require much maintenance at all. This will system will work by us doing the following:

  • Having a landing page for the archive search results (which can simply be your normal 'search' page)
  • Ensuring that every news entry has a month and a year tag this has now been automatically provided by mvlcek in the latest i18n Search update as one singular tag
  • Ensuring that (only) one entry per month has a tag to indicate the monthly archive link
  • Providing a custom output that dynamically produces the links necessary to take you to the appropriate month, showing no other unnecessary information.

First of all, produce your landing page for the archived search results. As noted before, you can use your normal search page but it is nice to have a distinct, separate page which you can style to your likings. All this page needs is the basic placeholder for it to work:

(% searchresults %)

Next, look through all of your news pages and give each entry the appropriate month and year tags which will allow the search plugin to index your pages correctly. These month tags should be three characters long (the abbreviated name of each month, e.g. _jan, _feb, _mar, etc...) and the year tags should be the full 4-digit year (e.g. _1996, _2007, _2012, etc...).

Your pages (assuming that you have Custom Fields or Special Pages installed) will automatically be able to call the virtual tags _cre_yyyymm and _pub_yyyymm, so we'll simply use these for the tutorial.

Now for only one entry of each month, put a tag that will tell the search plugin that these are 'flags' for the months of the archive. Simply place a tag like _archivem (for archive monthly) on one entry from each month.

To stop the plugin simply outputting the normal format of our search results we can create a custom render of each result. Go to 'Theme → Edit Components' and create a new component called 'monthly_archive'. Paste into it the following:

<?php
$month = strftime('%m', $item->creDate); // pulls the month the entry was created in a 3-character form to be part of the search link as "_Month+_Year"
$month_long = strftime('%B', $item->creDate); // pulls the full month the entry was created for the archive link to be "Month Year"
$year = strftime('%Y', $item->creDate); // pulls the year the entry was created for the archive link to be part of the search link as "_Month+_Year"
$subdir = ''; // subdirectory of the landing page, e.g. 'search/'
$tags = ''; // any extra tags to help refine the search, each separated by '+', e.g. 'news+sports'

echo '<a href="'; get_site_url(); echo $subdir.'?tags=cre'.$year.$month.'+'.$tags.'">'.$month_long.' '.$year.'</a>'; ?>

If you want to still use the previous method with your own custom month/year tags, instead use the following:

<?php
$month = strftime('%b', $item->creDate); // pulls the month the entry was created in a 3-character form to be part of the search link as "_Month+_Year"
$month_long = strftime('%B', $item->creDate); // pulls the full month the entry was created for the archive link to be "Month Year"
$year = strftime('%Y', $item->creDate); // pulls the year the entry was created for the archive link to be part of the search link as "_Month+_Year"
$subdir = ''; // subdirectory of the landing page, e.g. 'search/'
$tags = ''; // any extra tags to help refine the search, each separated by '+', e.g. 'news+sports'

echo '<a href="'; get_site_url(); echo $subdir.'?tags='.$month.'+'.$year.'+'.$tags.'">'.$month_long.' '.$year.'</a>'; ?>

In either case, change $subdir to show the link of your landing page (e.g. the search page). Finally, where you want to display the archive links, either use:

(% searchresults tags='news _archivem' component='monthly_archive' order='created' %)

or

<?php get_i18n_search_results(array('tags'=>'news _archivem','component'=>'monthly_archive','order'=>'created')); ?>

Depending on whether the archive links will be shown on the page content or the template file. When you view the results, you should have something like this:

Finished archive results - theme used is "baronvonBasic"

To maintain your archive, all you need to do is have an _archivem tag on one entry per month to ensure that the archive can be viewed (if you are using the previous method, you must also add the _month and _year tags to all of your forthcoming entries). You can use these principles to organise your pages in any way that you wish, so long as you have the appropriate tags in place and a search that enables those tags to be picked out.

Hope this has helped!

Extras

Using Special Pages to make the process easier

If you have Special Pages installed, there are two ways you can make the process of adding these new tags in a little less tedious. First of all, if there are standard tags that you want to have on all pages of a particular type, you can configure your page type such that any new page created under that type will receive that tag automatically. Go to Plugins -> Configure I18N Special Pages -> [Your Special Page Name] and under Tags & Keywords add in any new tags that you need.

Secondly, if you are worried about forgetting to put the _archivem tag on your singular entry per month, you can create a dropdown box Special Field with the _archivem tag that you can select instead of having to enter the tag in yourself every time. If you make sure that this field is indexed as a tag, your pages should be searched just as they normally would without a hitch.

Alternative solution with Special Pages for _archivem tag

Ensure that you order the field such that it is next to a field that you always edit (e.g. your article subtitle or author avatar) so that you always see it and remember whether or not to change it.

Special thanks to...

  • mvlcek (creator of i18n and child plugins) -- huge thanks for updating from i18n Search 2.7 to 2.8 and providing the new automatic tagging feature for months/years!
  • ccagle8 (creator of GetSimple CMS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment