davekin / pico_sitemap Goto Github PK
View Code? Open in Web Editor NEWGenerate a sitemap.xml for Pico
Generate a sitemap.xml for Pico
I added another Var to exclude folders from the Sitemap, for example: slider, home-parts, search
And added the <lastmod>
Attribute.
class Pico_Sitemap {
private $is_sitemap;
private $exclude;
public function __construct(){
$this->is_sitemap = false;
$this->exclude = array('home', 'slider', 'suche');
}
public function get_pages(&$pages, &$current_page, &$prev_page, &$next_page) {
if($this->is_sitemap) {
global $config;
header($_SERVER['SERVER_PROTOCOL'].' 200 OK');
header('Content-Type: application/xml; charset=UTF-8');
$xml = '<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
foreach( $pages as $page ) {
preg_match('/\/('.implode('|', $this->exclude).')\//', $page['url'].'/', $matches);
if(empty($matches)) {
$index = '';
$date = '';
$path = realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'content'.DIRECTORY_SEPARATOR.str_ireplace($config['base_url'], '', $page['url']);
if(is_dir($path)) { $index = 'index'; }
$file = realpath($path.$index.'.md');
if(file_exists($file)) { $date = date ("Y-m-d", filemtime($file)); }
$xml .= '<url><loc>'.$page['url'].'</loc><lastmod>'.$date.'</lastmod></url>';
}
}
$xml .= '</urlset>';
header('Content-Type: text/xml');
die($xml);
}
}
See http://philipmcgaw.com/sitemap.xml for demo,
Installed plugin as described, however the $base_url is not being written to XML,
I added a $twig_var
for a HTML-Sitemap with Bootstrap3 Markup und combined the other things from my last Issue #5
<?php
/**
* Generate an xml sitemap for Pico
*
* @author Dave Kinsella
* @link https://github.com/Techn0tic/Pico_Sitemap
* @license http://opensource.org/licenses/MIT
*/
class Pico_Sitemap {
private $is_sitemap;
private $is_html_sitemap;
private $exclude;
private $pages;
public function __construct() {
$this->is_sitemap = false;
$this->is_html_sitemap = false;
$this->exclude = array('home', 'slider', 'suche', 'sitemap');
$this->pages = array();
}
public function request_url(&$url) {
if($url == 'sitemap') $this->is_html_sitemap = true;
if($url == 'sitemap.xml') $this->is_sitemap = true;
}
public function get_pages($pages) {
if($this->is_sitemap || $this->is_html_sitemap) {
global $config;
foreach($pages as $page) {
preg_match('/\/('.implode('|', $this->exclude).')\//', $page['url'].'/', $matches);
if(empty($matches)) {
$index = '';
$date = false;
$path = realpath(dirname(__FILE__)).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'content'.DIRECTORY_SEPARATOR.str_ireplace($config['base_url'], '', $page['url']);
if(is_dir($path)) { $index = 'index'; }
$file = realpath($path.$index.'.md');
if(file_exists($file)) { $date = date ("Y-m-d", filemtime($file)); }
array_push($this->pages, array( 'url' => $page['url'], 'title' => $page['title'], 'lastmod' => $date, 'index' => $index == '' ? false : true ));
}
}
sort($this->pages);
}
}
public function before_render(&$twig_vars, &$twig, &$template) {
if($this->is_sitemap) {
header($_SERVER['SERVER_PROTOCOL'].' 200 OK');
header('Content-Type: application/xml; charset=UTF-8');
$xml = '<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';
foreach($this->pages as $page) {
$xml .= '<url><loc>'.$page['url'].'</loc><lastmod>'.$page['lastmod'].'</lastmod></url>';
}
$xml .= '</urlset>';
header('Content-Type: text/xml');
die($xml);
} elseif($this->is_html_sitemap) {
$panel_raw = '<div class="col-md-4"><div class="panel panel-primary"><div class="panel-heading"><h3 class="panel-title"><a href="%url%">%title%</a></h3></div><div class="list-group">%childs%</div></div></div>';
$child_raw = '<a href="%url%" class="list-group-item"><i class="icon-link"></i> %title%</a>';
$panel = '';
$childs = array();
$out = '';
$out .= '<div class="row">';
foreach($this->pages as $page) {
if($page['index']) {
$panel = str_replace('%childs%', implode('', $childs), $panel);
$out .= $panel;
$childs = array();
$panel = $panel_raw;
$panel = str_replace('%url%', $page['url'], $panel);
$panel = str_replace('%title%', $page['title'], $panel);
} else {
$child = $child_raw;
$child = str_replace('%url%', $page['url'], $child);
$child = str_replace('%title%', $page['title'], $child);
array_push($childs, $child);
}
}
$panel = str_replace('%childs%', implode('', $childs), $panel);
$out .= $panel;
$out .= '</div>';
$twig_vars['sitemap'] = $out;
}
}
}
This page contains the following errors:
error on line 1 at column 7: XML declaration allowed only at the start of the document
Below is a rendering of the page up to the first error.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.