I am trying to generate a sitemap.xml with this https://www.xml-sitemaps.com for my new PHP website. It used to work perfectly fine with my old html website and discovered all the articles but now it’s not working.
Is anyone encountering the same problem? Is it because it only works with html and not PHP pages?
I am expecting a sitemap to be generated but it’s not getting generated. This is my code:
<?php include 'partials/header.php';
// fetch featured post from database
$featured_query = "SELECT * FROM posts WHERE is_featured = 1";
$featured_result = mysqli_query($connection, $featured_query);
$featured = mysqli_fetch_assoc($featured_result);
// fetch 9 posts from database
$query = "SELECT * FROM posts ORDER BY date_time DESC LIMIT 9";
$posts = mysqli_query($connection, $query);
// Fetch categories from database
$categories_query = "SELECT * FROM categories ORDER BY title";
$categories = mysqli_query($connection, $categories_query);
// Fetch the secondary article (for example, the second post in the list)
$secondary_query = "SELECT * FROM posts ORDER BY date_time DESC LIMIT 1, 1"; // Skips the first post and gets the second
$secondary_result = mysqli_query($connection, $secondary_query);
$secondary = mysqli_fetch_assoc($secondary_result);
// Fetch the author's username based on author_id
if ($secondary && isset($secondary['author_id'])) {
$author_id = $secondary['author_id'];
$author_query = "SELECT username FROM users WHERE id = $author_id";
$author_result = mysqli_query($connection, $author_query);
$author = mysqli_fetch_assoc($author_result);
if ($author && isset($author['username'])) {
$username = htmlspecialchars($author['username']);
}
}
$related_posts = [];
if ($secondary) {
$author_id = $secondary['author_id'];
$author_query = "SELECT username FROM users WHERE id = $author_id";
$author_result = mysqli_query($connection, $author_query);
$author = mysqli_fetch_assoc($author_result);
// Fetch related posts based on category or other criteria
$category_id = $secondary['category_id']; // Assuming category_id exists in the posts table
$related_query = "
SELECT * FROM posts
WHERE category_id = $category_id
AND id != {$secondary['id']} -- Exclude the current secondary post
ORDER BY date_time DESC
LIMIT 2";
$related_result = mysqli_query($connection, $related_query);
while ($row = mysqli_fetch_assoc($related_result)) {
$related_posts[] = $row;
}
}
// Fetch top stories for the sidebar
$top_stories_query = "
SELECT * FROM posts
WHERE id != {$featured['id']} AND id != {$secondary['id']}
ORDER BY date_time DESC
LIMIT 7";
$top_stories_result = mysqli_query($connection, $top_stories_query);
$top_stories = [];
while ($row = mysqli_fetch_assoc($top_stories_result)) {
$top_stories[] = $row;
}
?>
<!-- Main News and Articles -->
<!-- Secondary -->
<section class="main-presentation">
<div class="container">
<div class="secondary-article">
<?php if ($secondary): ?>
<a href="<?= ROOT_URL ?>post.php?slug=<?= htmlspecialchars($secondary['slug']) ?>">
<img src="./images/<?= htmlspecialchars($secondary['thumbnail']) ?>" alt="Secondary Article Image"
class="secondary-image">
<div class="secondary-article-content">
<h2 class="secondary-title"><?= htmlspecialchars($secondary['title']) ?></h2>
<p class="secondary-description">
<?php
function getSecondaryExcerpt($content, $charLimit = 150)
{
// Remove headers, list tags, and table tags
$cleanedContent = preg_replace(
'/<(h[1-6]|ul|li|table|thead|tbody|tfoot|tr|td|th)[^>]*>.*?</1>/is',
'',
$content
);
// Remove <br> tags
$cleanedContent = preg_replace('/<brs*/?>/i', ' ', $cleanedContent);
// Remove paragraph tags but preserve text
$cleanedContent = preg_replace('/</p>s*<p>/i', ' ', $cleanedContent);
$cleanedContent = preg_replace('/<p>s*</p>/i', ' ', $cleanedContent);
// Remove remaining <p> and </p> tags, keeping content intact
$cleanedContent = str_replace(['<p>', '</p>'], '', $cleanedContent);
// Limit the content to the specified number of characters
if (strlen($cleanedContent) > $charLimit) {
// Find the last space within the character limit to avoid cutting off words
$lastSpace = strpos($cleanedContent . ' ', ' ', $charLimit);
// Fallback in case strpos fails (e.g., no spaces found)
if ($lastSpace === false) {
$excerpt = substr($cleanedContent, 0, $charLimit) . '...';
} else {
$excerpt = substr($cleanedContent, 0, $lastSpace) . '...';
}
} else {
$excerpt = $cleanedContent;
}
return $excerpt;
}
// Example usage
echo getSecondaryExcerpt($secondary['body'], 150);
?>
</p>
<p class="secondary-author">
By <?php echo htmlspecialchars($username); ?> |
<?php echo date('F j, Y', strtotime($secondary['date_time'])); ?>
</p>
</a>
<div class="related-links">
<h6 class="related-title">RELATED</h6>
<?php if (!empty($related_posts)): ?>
<?php foreach ($related_posts as $index => $related): ?>
<a href="post.php?slug=<?= htmlspecialchars($related['slug']) ?>" class="related-link">
<?= htmlspecialchars($related['title']) ?>
</a>
<?php endforeach; ?>
<?php else: ?>
<p>No related articles found.</p>
<?php endif; ?>
</div>
</div>
<?php else: ?>
<p>No secondary article found.</p>
<?php endif; ?>
</div>
<!-- Main -->
<div class="main-article">
<a href="<?= ROOT_URL ?>post.php?slug=<?= htmlspecialchars($featured['slug']) ?>">
<img src="./images/<?= htmlspecialchars($featured['thumbnail']) ?>" alt="Main Article Image"
class="main-image">
<div class="main-article-content">
<h2 class="main-title"><?= htmlspecialchars($featured['title']) ?></h2>
<p class="main-description">
<?php
function getExcerpt($content, $charLimit = 300)
{
// Remove headers, list tags, and table tags
$cleanedContent = preg_replace(
'/<(h[1-6]|ul|li|table|thead|tbody|tfoot|tr|td|th)[^>]*>.*?</1>/is',
'',
$content
);
// Remove <br> tags
$cleanedContent = preg_replace('/<brs*/?>/i', ' ', $cleanedContent);
// Remove paragraph tags but preserve text
$cleanedContent = preg_replace('/</p>s*<p>/i', ' ', $cleanedContent);
$cleanedContent = preg_replace('/<p>s*</p>/i', ' ', $cleanedContent);
// Remove remaining <p> and </p> tags, keeping content intact
$cleanedContent = str_replace(['<p>', '</p>'], '', $cleanedContent);
// Limit the content to the specified number of characters
if (strlen($cleanedContent) > $charLimit) {
// Find the last space within the character limit to avoid cutting off words
$excerpt = substr($cleanedContent, 0, strpos($cleanedContent . ' ', ' ', $charLimit)) . '...';
} else {
$excerpt = $cleanedContent;
}
return $excerpt;
}
// Example usage
echo getExcerpt($featured['body'], 300);
?>
</p>
<p class="main-author">
<?php
// fetch author from users table using author_id
$author_id = $featured['author_id'];
$author_query = "SELECT * FROM users WHERE id=$author_id";
$author_result = mysqli_query($connection, $author_query);
$author = mysqli_fetch_assoc($author_result);
?>
By <?= htmlspecialchars($author['username']) ?> |
<?= date('M d, Y - H:i', strtotime($featured['date_time'])) ?>
</p>
</div>
</a>
</div>
<!-- Sidebar -->
<div class="sidebar">
<h2 class="sidebar-title">Other Top Stories</h2>
<ul class="sidebar-list">
<?php if (!empty($top_stories)): ?>
<?php foreach ($top_stories as $story): ?>
<li class="sidebar-item">
<a href="<?= ROOT_URL ?>post.php?slug=<?= htmlspecialchars($story['slug']) ?>" class="sidebar-link">
<?= htmlspecialchars($story['title']) ?>
</a>
</li>
<?php endforeach; ?>
<?php else: ?>
<li class="sidebar-item">No top stories available.</li>
<?php endif; ?>
</ul>
</div>
</div>
</section>
<!-- Plantilla Valoración Section -->
<section class="plantilla-valoracion">
<div class="container">
<form method="post" action="<?= ROOT_URL ?>checkout.php">
<div class="valoracion-content">
<h1 class="valoracion-title">Plantilla Valor Intrínseco</h1>
<h3 class="valoracion-subtitle">Con 8 modelos de Valoración</h3>
<p class="valoracion-description">Y un psicoanálisis del mercado</p>
<button class="valoracion-button">OBTENER</button>
</div>
</form>
</div>
</section>
<!-- Start Posts-->
<section class="posts">
<div class="container-sm posts-container">
<?php
// Fetch posts from the database
$posts_query = "SELECT * FROM posts"; // Update with your actual query
$posts = mysqli_query($connection, $posts_query);
if (!$posts) {
die('Error executing query: ' . mysqli_error($connection));
}
function getPostExcerpt($content, $charLimit = 300)
{
// Remove headers, list tags, and table tags
$cleanedContent = preg_replace(
'/<(h[1-6]|ul|li|table|thead|tbody|tfoot|tr|td|th)[^>]*>.*?</1>/is',
'',
$content
);
// Remove <br> tags
$cleanedContent = preg_replace('/<brs*/?>/i', ' ', $cleanedContent);
// Remove paragraph tags but preserve text
$cleanedContent = preg_replace('/</p>s*<p>/i', ' ', $cleanedContent);
$cleanedContent = preg_replace('/<p>s*</p>/i', ' ', $cleanedContent);
// Remove remaining <p> and </p> tags, keeping content intact
$cleanedContent = str_replace(['<p>', '</p>'], '', $cleanedContent);
// Limit the content to the specified number of characters
if (strlen($cleanedContent) > $charLimit) {
// Find the last space within the character limit to avoid cutting off words
$excerpt = substr($cleanedContent, 0, strpos($cleanedContent . ' ', ' ', $charLimit)) . '...';
} else {
$excerpt = $cleanedContent;
}
return $excerpt;
}
while ($post = mysqli_fetch_assoc($posts)): ?>
<article class="post">
<div class="post-thumbnail">
<img src="images/<?= htmlspecialchars($post['thumbnail']) ?>">
</div>
<div class="post-info">
<?php
// Fetch category from categories table using category_id
$category_id = $post['category_id'];
$category_query_normal = "SELECT title, slug FROM categories WHERE id=$category_id";
$category_result_normal = mysqli_query($connection, $category_query_normal);
$category_normal = mysqli_fetch_assoc($category_result_normal);
// Get title and slug
$category_title_normal = $category_normal['title'];
$category_slug_normal = $category_normal['slug'];
?>
<a href="<?= ROOT_URL . htmlspecialchars($category_slug_normal) ?>.php"
class="category-botton"><?= htmlspecialchars($category_title_normal) ?></a>
<h3 class="post-title"><a href="<?= ROOT_URL ?>post.php?slug=<?= htmlspecialchars($post['slug']) ?>">
<?= htmlspecialchars($post['title']) ?>
</a>
</h3>
<p class="post-body">
<?= getPostExcerpt($post['body'], 300) ?>
</p>
<div class="post-author">
<?php
// Fetch author from users table using author_id
$author_id = $post['author_id'];
$author_query = "SELECT * FROM users WHERE id=$author_id";
$author_result = mysqli_query($connection, $author_query);
$author = mysqli_fetch_assoc($author_result);
?>
<div class="post-author-info">
<h5><?= htmlspecialchars($author['username']) ?></h5>
<small>
<?= date('M d, Y - H:i', strtotime($post['date_time'])) ?>
</small>
</div>
</div>
</div>
</article>
<?php endwhile ?>
</div>
</section>
<!-- End Posts-->
<section class="center-section">
<!-- Search widget-->
<div>
<form action="<?= ROOT_URL ?>search.php" method="GET">
<div class="input-group">
<input type="search" name="search" placeholder="Search">
<button type="submit" name="submit" class="btn2">Go</button>
</div>
</form>
</div>
</section>
<section class="category-buttons">
<div class="container category-buttons-container">
<?php if (mysqli_num_rows($categories) > 0): ?>
<?php while ($category = mysqli_fetch_assoc($categories)): ?>
<a href="<?= ROOT_URL ?>category-posts.php?slug=<?= htmlspecialchars($category['slug']) ?>"
class="category-button"><?= htmlspecialchars($category['title']) ?></a>
<?php endwhile; ?>
<?php else: ?>
<div class="alert-message error">
<p><?= "No categories found" ?></p>
</div>
<?php endif ?>
</div>
</section>
<!--End of Category buttons-->
<!-- Page content-->
<div class="container-fluid mt-5">
<div class="row">
<div class="col-lg-8">
<!-- Comments section-->
<section class="mb-5">
<div class="card-index-comments bg-light">
<div class="card-body">
<!-- Comment form-->
<form class="mb-4"><textarea class="form-control" rows="3"
placeholder="Únete al debate con un comentario"></textarea></form>
<!-- Comment with nested comments-->
<div class="d-flex mb-4">
<!-- Parent comment-->
<div class="flex-shrink-0"><img class="rounded-circle" src="images/Buffet.jpg" alt="..." />
</div>
<div class="ms-3">
<div class="fw-bold">Warren Buffet</div>
Invertir en compañías de calidad es igual de importante como el momento en el que
inviertes, Sé codicioso cuando los demás tengan miedo.
<!-- Child comment 1-->
<div class="d-flex mt-4">
<div class="flex-shrink-0"><img class="rounded-circle" src="images/Lebron.jpg"
alt="..." /></div>
<div class="ms-3">
<div class="fw-bold">LeBron James</div>
Definitivamente, Warren. Admiro <a
href="https://youtu.be/EOLfzevQgGk?si=A0fn1ROC2hvD6JI0">tu enfoque de
inversión</a> y cómo buscas empresas subvaloradas con sólidos fundamentos.
Pero últimamente, he estado explorando algo un poco diferente: la inversión en
whisky. He oído que las botellas raras pueden ser una inversión lucrativa.
</div>
</div>
<!-- Child comment 2-->
<div class="d-flex mt-4">
<div class="flex-shrink-0"><img class="rounded-circle" src="images/Buffet.jpg"
alt="..." /></div>
<div class="ms-3">
<div class="fw-bold">Warren Buffet</div>
Interesante elección, LeBron. La diversificación es clave, y siempre hay
oportunidades en diferentes lugares, ¿por qué limitarse al whisky?
</div>
</div>
</div>
</div>
<!-- Single comment-->
<div class="d-flex">
<div class="flex-shrink-0"><img class="rounded-circle" src="images/Munger.jpg" alt="..." />
</div>
<div class="ms-3">
<div class="fw-bold">Charlie Munger</div>
La diversificación es buena, pero la sobre diversificación es para el inversor que no
sabe nada
</div>
</div>
</div>
</div>
</section>
</div>
<!-- Side widgets-->
<div class="col-lg-4">
<!-- Search widget-->
<div class="card mb-4">
<form action="<?= ROOT_URL ?>search.php" method="GET">
<div class="card-header">Search</div>
<div class="card-body">
<div class="input-group">
<input class="form-control" name="search" type="search" placeholder="Enter search term..."
aria-label="Enter search term..." aria-describedby="button-search" />
<button class="btn btn-primary" name="submit" id="button-search" type="submit">Go!</button>
</div>
</div>
</form>
</div>
<!-- Categories widget-->
<div class="card mb-4">
<div class="card-header">Categorias</div>
<div class="card-body">
<div class="row">
<div class="col-sm-6">
<ul class="list-unstyled mb-0">
<li><a href="#!">Estrategias</a></li>
<li><a href="#!">Diccionario</a></li>
<li><a href="#!">Noticias</a></li>
</ul>
</div>
<div class="col-sm-6">
<ul class="list-unstyled mb-0">
<li><a href="#!">Reviews</a></li>
<li><a href="#!">Finanzas Personales</a></li>
<li><a href="#!">Carteras de Inversión</a></li>
</ul>
</div>
</div>
</div>
</div>
<!-- Side widget-->
<div class="card mb-4">
<div class="card-header">¿Lo tuyo son los vídeos?</div>
<div class="card-body">En <a href="https://www.youtube.com/@crisd_">este canal</a> puedes encontrar todo
nuestro camino financiero, desde que empezamos con 0€ hasta nuestra situación actual.</div>
</div>
</div>
</div>
</div>
<?php include 'partials/footer.php';
?>`
New contributor
Cris D is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
8