PHP code to convert a website sitemap xml file to html sitemap with maximum of 100 links per page

PHP code to convert a website sitemap xml file to html sitemap with maximum of 100 links per page with pagination links


Here is a PHP code sample that you can use to convert a website sitemap XML file to an HTML sitemap with a maximum of 100 links per page and pagination links:

<?php

// Set the URL of the sitemap XML file
$sitemap_url = 'https://www.example.com/sitemap.xml';

// Set the maximum number of links per page
$max_links_per_page = 100;

// Read the sitemap XML file
$sitemap_xml = file_get_contents($sitemap_url);

// Parse the sitemap XML into an object
$sitemap_obj = simplexml_load_string($sitemap_xml);

// Get the total number of links in the sitemap
$total_links = count($sitemap_obj->sitemap->item);

// Calculate the number of pages needed to display all the links
$num_pages = ceil($total_links / $max_links_per_page);

// Get the current page number from the URL query string
$current_page = isset($_GET['page']) ? (int)$_GET['page'] : 1;

// Calculate the offset for the links on the current page
$offset = ($current_page - 1) * $max_links_per_page;

// Initialize a string to hold the HTML output
$html = '';

// Start the HTML output with the page title and styles
$html .= '<!DOCTYPE html>
<html>
<head>
    <title>Sitemap</title>
    <style>
        body {
            font-family: Arial, sans-serif;
        }
        ul {
            list-style: none;
}
a {
color: #333;
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
</style>

</head>
<body>
    <h1>Sitemap</h1>';
// Start the unordered list for the sitemap links
$html .= '<ul>';

// Loop through the links on the current page and add them to the HTML output
for ($i = $offset; $i < $offset + $max_links_per_page; $i++) {
if (isset($sitemap_obj->sitemap->item[$i])) {
$html .= '<li><a href="' . $sitemap_obj->sitemap->item[$i]->link . '">' . $sitemap_obj->sitemap->item[$i]->title . '</a></li>';
}
}

// End the unordered list for the sitemap links
$html .= '</ul>';

// Add pagination links to the HTML output
if ($num_pages > 1) {
$html .= '<p>';
for ($i = 1; $i <= $num_pages; $i++) {
if ($i === $current_page) {
$html .= '<strong>' . $i . '</strong> ';
} else {
$html .= '<a href="?page=' . $i . '">' . $i . '</a> ';
}
}
$html .= '</p>';
}
// End the HTML output
$html .= '</body>

</html>';
// Output the HTML sitemap
echo $html;
?>

 

This code first sets the URL of the sitemap XML file and the maximum number of links to include on each page. It then reads the XML file using the `file_get_contents` function and parses it into an object using the `simplexml_load_string` function.

Next, the code calculates the total number of links in the sitemap and the number of pages needed to display all the links. It also gets the current page number from the URL query string, and calculates the offset for the links on the current page.

The code then initializes a string to hold the HTML output, and starts the output with the page title and styles. It then starts an unordered list for the sitemap links and loops through the links on the current page, adding them to the HTML output as list items.

Once all the links on the current page have been added, the code ends the unordered list and adds pagination links to the HTML output if there is more than one page of links. Finally, it ends the HTML output and outputs it to the screen.

You can customize this code to suit your specific needs and requirements, such as using different HTML tags or styles, or handling errors in a different way.

 

There are many benefits and practically no disadvantages when having an HTML sitemap on your website. Big banks and eCommerce sites like JP Morgan Chase, Bank of America, Goldman Sachs, Macy’s, and Home Depot, have an HTML sitemap on their website, so why not you?

Chapters (Timestamps):

00:00 - Introduction

00:29 - Benefits of HTML Sitemap

02:11 - Free HTML Sitemap Plugin

02:35 - How to Add an HTML Sitemap

05:03 - Control What to Display on HTML Sitemap

05:56 - Display Authors on HTML Sitemap

06:47 - Will AIOSEO HTML sitemap work with Rank Math after Migration?

07:35 - Should you Submit the HTML Sitemap to Google Search Console?

08:35 - Outro

Recent Blog Posts


How to Start an SEO Campaign about Electric Cars

How to Start an SEO Campaign about Electric Cars Electric cars have become an increasingly...

How to Start an SEO Campaign for a Local Grocery Store

How to Start an SEO Campaign for a Local Grocery Store Owning a local grocery...

Mastering Google Rankings: A Comprehensive Guide to SEO Success

Unlocking Google’s Top Spot: The Comprehensive Guide to SEO Success Welcome to your journey towards...

Step-by-Step Guide: Creating & Managing an SEO Campaign for Tax Benefits

Step-by-Step Guide: Creating & Managing an SEO Campaign for Tax Benefits 1. Research and Define...

Step-by-Step Guide: Creating and Managing an SEO Campaign for Annuities and Retirement Strategies

Step-by-Step Guide: Creating and Managing an SEO Campaign for Annuities and Retirement Strategies 1. Understanding...

How to Create and Manage an SEO Campaign for Your Food and Grocery Blog

If you’re running a food and grocery blog, driving traffic to your website is essential....

Maximizing Your Real Estate SEO Campaign in a High Mortgage Rate Market

Maximizing Your Real Estate SEO Campaign in a High Mortgage Rate Market In the ever-competitive...

How to Create and Manage an Effective SEO Campaign

Are you looking to boost your website’s visibility and drive traffic by leveraging the power...

Unmasking the Spread of Disinformation: How X Amplified Confusion in the Israel-Hamas Crisis

In the digital age, information spreads faster than wildfire, and the recent Israel-Hamas conflict is...

AI-Powered Content Marketing: From Ideas to Optimization

In the digital age where content reigns supreme, marketers are on the lookout for ways...


Jeff M.

CEO / Co-Founder

Enjoy the little things in life. For one day, you may look back and realize they were the big things. Many of life's failures are people who did not realize how close they were to success when they gave up.