K
K
krispey1022021-06-10 10:15:14
PHP
krispey102, 2021-06-10 10:15:14

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>
';
?>

It works properly, but it only shows the main page several times, which is why there are 10-15 main pages in the generated sitemap, how to fix it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
krispey102, 2021-06-10
@krispey102

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>';
  }

A
Alexander Toropov, 2021-06-10
@nefone

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 question

Ask a Question

731 491 924 answers to any question