![](/img/trans.png)
[英]Getting error as undefined variable on sitemap.xml file in CodeIgniter PHP
[英]How to update sitemap.xml file in codeigniter?
如果有人對用於通過codeinginter更新sitemap.xml文件的codeigniter庫有想法? 試圖按照本教程進行操作,但不知道要創建哪個文件以及在何處: https : //github.com/chemicaloliver/codeigniter-sitemaps 。 我將不勝感激。
謝謝。
只需將庫文件放在名稱為sitemaps的application / libraries文件夾中,然后在config文件夾中添加sitemaps配置文件。 然后將庫加載為
$this->load->library('sitemaps');
並在您的控制器方法中
function add()
{
$item = array(
"loc" => site_url("page/" . "your title of page"),
"lastmod" => date("c", strtotime('2017-01-01')),
"changefreq" => "daily",
"priority" => "0.8"
);
$this->sitemaps->add_item($item);
// file name may change due to compression
$file_name = $this->sitemaps->build("sitemap.xml");
$this->sitemaps->ping(site_url($file_name));
}
然后只需修改您的libraru僅保留XML文件並更新它。 因此將庫文件的構建功能修改為
function build($file_name = null, $gzip = NULL)
{
$map = $this->CI->config->item('sitemaps_header') . "\n";
$xml=simplexml_load_file($file_name);
foreach($xml->children() as $books) {
$books['loc'] = htmlentities($books['loc'], ENT_QUOTES);
$map .= "\t<url>\n\t\t<loc>" . $books->loc . "</loc>\n";
$map .= "\t\t<lastmod>" . $books->lastmod . "</lastmod>\n";
$map .= "\t\t<changefreq>" . $books->changefreq . "</changefreq>\n";
$map .= "\t\t<priority>" . $books->priority . "</priority>\n";
$map .= "\t</url>\n\n";
}
foreach ($this->items as $item)
{
$item['loc'] = htmlentities($item['loc'], ENT_QUOTES);
$map .= "\t<url>\n\t\t<loc>" . $item['loc'] . "</loc>\n";
$attributes = array("lastmod", "changefreq", "priority");
foreach ($attributes AS $attr)
{
if (isset($item[$attr]))
{
$map .= "\t\t<$attr>" . $item[$attr] . "</$attr>\n";
}
}
$map .= "\t</url>\n\n";
}
//unset($this->items);
$map .= $this->CI->config->item('sitemaps_footer');
if (!is_null($file_name))
{
$fh = fopen($file_name, 'w');
if (!$fh)
{
$this->set_error('Could not open sitemaps file for writing: ' . $file_name);
return FALSE;
}
if (!fwrite($fh, $map))
{
$this->set_error('Error writing to sitemaps file: ' . $file_name);
return FALSE;
}
fclose($fh);
if ($this->CI->config->item('sitemaps_filesize_error') && filesize($file_name) > 1024 * 1024 * 10)
{
$this->set_error('Your sitemap is bigger than 10MB, most search engines will not accept it.');
return FALSE;
}
return $file_name;
}
else
{
return $map;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.