Answer the question
In order to leave comments, you need to log in
Sitemap.php auto-generator makes a duplicate main page, how to remove it?
There is a map generator in php
<?php
//Отключаем статистику Bitrix
define("NO_KEEP_STATISTIC", true);
//Подключаем движок
require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_before.php");
//устанавливаем тип ответа как xml документ
header('Content-Type: application/xml; charset=utf-8');
$array_pages = array();
//Простые текстовые страницы: начало
$array_pages[] = array(
'NAME' => 'Главная страница',
'URL' => '/',
);
$array_pages[] = array(
'NAME' => 'Вакансии',
'URL' => '/vacancies/',
);
$array_pages[] = array(
'NAME' => 'Производство',
'URL' => '/directions/production/',
);
$array_pages[] = array(
'NAME' => 'IT & Digital',
'URL' => '/directions/digital/',
);
$array_pages[] = array(
'NAME' => 'Офис',
'URL' => '/directions/office/',
);
$array_pages[] = array(
'NAME' => 'Молодым специалистам',
'URL' => '/directions/graduates/',
);
$array_pages[] = array(
'NAME' => 'Предприятия',
'URL' => '/units/',
);
$array_pages[] = array(
'NAME' => 'Предприятия',
'URL' => '/privacy_policy/'
);
//Простые текстовые страницы: конец
$array_iblocks_id = array('1', '4', '5'); //ID инфоблоков, разделы и элементы которых попадут в карту сайта
if(CModule::IncludeModule("iblock"))
{
foreach($array_iblocks_id as $iblock_id)
{
//Список разделов
//Список элементов
$res = CIBlockSection::GetList(
array(),
Array(
"IBLOCK_ID" => $iblock_id,
"ACTIVE" => "Y" ,
),
false,
array(
"ID",
"NAME",
"SECTION_PAGE_URL",
));
while($ob = $res->GetNext())
{
$array_pages[] = array(
'NAME' => $ob['NAME'],
'URL' => $ob['SECTION_PAGE_URL'],
);
}
//Список элементов
$res = CIBlockElement::GetList(
array(),
Array(
"IBLOCK_ID" => $iblock_id,
"ACTIVE_DATE" => "Y",
"ACTIVE" => "Y" ,
),
false,
false,
array(
"ID",
"NAME",
"DETAIL_PAGE_URL",
));
while($ob = $res->GetNext())
{
$array_pages[] = array(
'NAME' => $ob['NAME'],
'URL' => $ob['DETAIL_PAGE_URL'],
);
}
}
}
//Создаём XML документ: начало
$date = date("d/M/y H:m:s");
$xml_content = '';
$site_url = 'https://'.$_SERVER['HTTP_HOST'];
$quantity_elements = 0;
foreach($array_pages as $v)
{
$quantity_elements++;
$xml_content.='
<url>
<loc>'.$site_url.$v['URL'].'</loc>
</url>';
}
//Создаём XML документ: конец
//Выводим документ
echo '<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
'.$xml_content.'
</urlset>
';
?>
Answer the question
In order to leave comments, you need to log in
Decided why multiple master pages are being created. The $url variable holds the url of the detailed page, but it looks like some elements didn't have a url, so it just outputs a domain link. Made a check for emptiness and now does not display empty elements
if ($v['URL']) {
$xml_content.='
<url>
<loc>'.$site_url.$v['URL'].'</loc>
</url>';
}
Make the value in the $array_pages array unique using the array_unique function
//Создаём XML документ: начало
$date = date("d/M/y H:m:s");
$xml_content = '';
$site_url = 'https://'.$_SERVER['HTTP_HOST'];
$quantity_elements = 0;
$array_pages_uniq = array_unique($array_pages);
foreach($array_pages_uniq as $v)
{
$quantity_elements++;
$xml_content.='
<url>
<loc>'.$site_url.$v['URL'].'</loc>
</url>';
}
//Создаём XML документ: конец
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question