Skip to content

Instantly share code, notes, and snippets.

@SalvatoreNoschese
Last active June 30, 2024 17:57
Show Gist options
  • Save SalvatoreNoschese/1d8a4a1b3c90291e0f3fc060f06e5c78 to your computer and use it in GitHub Desktop.
Save SalvatoreNoschese/1d8a4a1b3c90291e0f3fc060f06e5c78 to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: Custom Sitemap Shortcode
Description: Generates an HTML sitemap using a shortcode: [custom_sitemap]
Author: Salvatore Noschese
Version: 1.0
*/
defined('ABSPATH') || exit; // deny direct access
// Register the shortcode
add_shortcode('custom_sitemap', 'custom_sitemap_shortcode');
function custom_sitemap_shortcode() {
if (!is_page()) { return '[*custom_sitemap*]'; }
ob_start(); ?>
<div class="custom-sitemap-shortcode">
<hr />
<h2><?php echo esc_html__('Pages', 'default') . ' (' . esc_html(wp_count_posts('page')->publish - 1) . ')'; ?></h2>
<ul>
<?php wp_list_pages('title_li=&sort_column=post_title&sort_order=ASC&exclude=' . get_the_ID()); ?>
</ul>
<hr />
<h2><?php echo esc_html__('Posts', 'default') . ' (' . esc_html(wp_count_posts('post')->publish) . ')'; ?></h2>
<ul>
<?php $posts = get_posts('post_type=post&posts_per_page=-1&orderby=date&order=DESC');
foreach ($posts as $post): ?>
<li><a href="<?php echo get_permalink($post->ID); ?>" title="<?php echo esc_attr(get_the_title($post->ID)); ?>"><?php echo esc_html(get_the_title($post->ID)); ?></a> <em>(<?php echo esc_html(get_the_date(get_option('date_format'), $post->ID)); ?>)</em></li>
<?php endforeach; ?>
</ul>
<?php function display_tagcatcloud($taxonomy, $title) { ?>
<hr />
<h2><?php echo esc_html($title) . ' (' . esc_html(wp_count_terms($taxonomy)) . ')'; ?></h2>
<ul>
<li><?php wp_tag_cloud('taxonomy=' . $taxonomy . '&smallest=8&largest=22&unit=pt&number=0&orderby=name&order=ASC&format=flat&show_count=true'); ?></li>
</ul>
<?php }
display_tagcatcloud('category', esc_html__('Categories', 'default'));
display_tagcatcloud('post_tag', esc_html__('Tags', 'default'));
if (class_exists('WPSEO_Options') && WPSEO_Options::get('enable_xml_sitemap')): ?>
<hr />
<h3 class="has-text-align-center"><p><a href="<?php echo esc_url(WPSEO_Sitemaps_Router::get_base_url('sitemap_index.xml')); ?>" target="_blank">&lt; <?php echo esc_html__('See the XML sitemap.', 'wordpress-seo'); ?> &gt;</a></p></h3>
<?php endif; ?>
</div>
<?php return ob_get_clean();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment