Below, you’ll find a detailed guide on how to add the HTML Page Sitemap Shortcode to your WordPress website, including its parameters, examples, and PHP function code. Additionally, we’ll assist you with common issues that might cause the HTML Page Sitemap Plugin shortcode not to show or not to work correctly.
Before starting, here is an overview of the HTML Page Sitemap Plugin and the shortcodes it provides:
"HTML Page Sitemap is a dynamic WordPress plugin that generates a well-structured, easy-to-navigate sitemap, enhancing website usability and SEO.”
- [html-sitemap]
HTML Page Sitemap [html-sitemap] Shortcode
The ‘html-sitemap’ shortcode is a powerful tool for generating dynamic HTML sitemaps. It allows customization of sitemap display with various attributes like class, id, and ordered list type. It takes ‘class’, ‘id’, and ‘ordered_list_type’ as parameters. If ‘child_of’ is set to ‘CURRENT’, it targets the current page, while ‘PARENT’ targets the parent page. It also allows changing unordered lists to ordered lists.
Shortcode: [html-sitemap]
Parameters
Here is a list of all possible html-sitemap shortcode parameters and attributes:
class
– Specifies the CSS class of the sitemap.id
– Gives a unique identifier to the sitemap.ordered_list_type
– Determines the type of ordered list used in the sitemap.echo
– Controls whether the sitemap is returned or echoed.title_li
– Defines the title that appears in the list of pages.link_before
– Text or HTML to put before each link in the list.child_of
– Sets the parent page from which to display the child pages.
Examples and Usage
Basic example – A simple usage of the shortcode to generate a HTML sitemap without any customizations.
[html-sitemap /]
For more advanced usage, you can include parameters to customize the output of the HTML sitemap. These parameters include ‘class’, ‘id’, and ‘ordered_list_type’.
Advanced examples
Adding a custom CSS class to the sitemap. The ‘class’ parameter allows you to specify a custom CSS class name for the sitemap. This can be useful for adding custom styles to the sitemap.
[html-sitemap class="my-custom-class" /]
Setting a specific ID for the sitemap. The ‘id’ parameter allows you to specify a unique ID for the sitemap. This can be useful for targeting the sitemap with JavaScript or for applying unique styles to the sitemap.
[html-sitemap id="my-custom-id" /]
Changing the list type of the sitemap. The ‘ordered_list_type’ parameter allows you to change the list type of the sitemap. This can be useful for changing the appearance of the sitemap. The options for this parameter include ‘1’ for numeric list, ‘i’ or ‘I’ for lower or upper roman numerals, and ‘a’ or ‘A’ for lower or upper alphabetic list.
[html-sitemap ordered_list_type="I" /]
Combining multiple parameters. You can also combine multiple parameters to further customize the output of the sitemap.
[html-sitemap class="my-custom-class" id="my-custom-id" ordered_list_type="I" /]
PHP Function Code
In case you have difficulties debugging what causing issues with [html-sitemap]
shortcode, check below the related PHP functions code.
Shortcode line:
add_shortcode('html-sitemap', 'html_sitemap_shortcode_handler'); // This is no longer recommended as any plugin that creates their own shortcode starting with 'html' will also get the handler call
Shortcode PHP function:
function html_sitemap_shortcode_handler( $args, $content = null )
{
if( is_feed() )
return '';
$argsLocal = array();
if( is_array($args) ) {
$argsLocal = $args;
}
$class_tag = '';
if( isset($argsLocal['class']) ) {
if( !empty($argsLocal['class']) )
$class_tag = $argsLocal['class'];
unset($argsLocal['class']);
}
$id_tag = '';
if( isset($argsLocal['id']) ) {
if( !empty($argsLocal['id']) )
$id_tag = $argsLocal['id'];
unset($argsLocal['id']);
}
$ordered_type = '';
if( isset($argsLocal['ordered_list_type']) ) {
if( !empty($argsLocal['ordered_list_type'])) {
switch( $argsLocal['ordered_list_type'] ) {
case '1':
case 'i':
case 'I':
case 'a':
case 'A': { $ordered_type = $argsLocal['ordered_list_type']; }; break;
}
}
unset($argsLocal['ordered_list_type']);
}
$argsLocal['echo'] = 0;
$argsLocal['title_li'] = '';
if( isset($argsLocal['link_before']) ) {
unset($argsLocal['link_before']);
}
if( isset($argsLocal['link_before']) ) {
unset($argsLocal['link_befoe']);
}
if( isset($argsLocal['child_of']) && $argsLocal['child_of'] == 'CURRENT' ) {
$argsLocal['child_of'] = get_the_ID();
} else if( isset($argsLocal['child_of']) && $argsLocal['child_of'] == 'PARENT' ) {
$post = &get_post( get_the_ID() );
if( $post->post_parent )
$argsLocal['child_of'] = $post->post_parent;
else
unset( $argsLocal['child_of'] );
}
$html = wp_list_pages($argsLocal);
// Remove the classes added by WordPress
$html = preg_replace('/( class="[^"]+")/is', '', $html);
if( !empty($ordered_type) ) {
// swap the type ul with ol and set the type="$ordered_type"
$html = preg_replace('/(<ul)/is', '<ol type="'. esc_attr($ordered_type) .'"', $html);
$html = preg_replace('/(<\/ul)/is', '</ol', $html);
}
if( empty($ordered_type) ) {
$prefix = '<ul';
} else {
$prefix = '<ol type="'. esc_attr($ordered_type) .'"';
}
if( !empty($id_tag) ) {
$prefix .= ' id="'. esc_attr($id_tag) .'"';
}
if( !empty($class_tag) ) {
$prefix .= ' class="'. esc_attr($class_tag) .'"';
}
$prefix .= '>';
if( empty($ordered_type) ) {
return $prefix . $html .'</ul>';
}
return $prefix . $html .'</ol>';
}
Code file location:
html-sitemap/html-sitemap/html-sitemap.php
Conclusion
Now that you’ve learned how to embed the HTML Page Sitemap Plugin shortcode, understood the parameters, and seen code examples, it’s easy to use and debug any issue that might cause it to ‘not work’. If you still have difficulties with it, don’t hesitate to leave a comment below.
Leave a Reply